From 5cfbfd8a545f3beedaeacc831dc5ec4b6fe6d636 Mon Sep 17 00:00:00 2001 From: Perry Werneck Date: Mon, 16 Aug 2021 20:38:00 -0300 Subject: [PATCH] Adding property to set a session based log file. --- src/core/log.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- src/core/properties/string.c | 8 ++++++++ src/core/session.c | 3 ++- src/include/internals.h | 3 +++ src/include/lib3270/log.h | 3 +++ 5 files changed, 87 insertions(+), 4 deletions(-) diff --git a/src/core/log.c b/src/core/log.c index 89c1fee..db209ea 100644 --- a/src/core/log.c +++ b/src/core/log.c @@ -30,6 +30,8 @@ * */ +#include + #ifdef WIN32 #include #include @@ -49,6 +51,56 @@ static void (*loghandler)(H3270 *session, const char *module, int rc, const char /*---[ Implementacao ]--------------------------------------------------------------------------------------*/ +static void logfile(H3270 *session, const char *module, int rc, const char *fmt, va_list arg_ptr) { + + FILE *f = fopen(session->logfile, "a"); + + if(f) { + + time_t ltime = time(0); + + char timestamp[80]; +#ifdef HAVE_LOCALTIME_R + struct tm tm; + strftime(timestamp, 79, "%x %X", localtime_r(<ime,&tm)); +#else + strftime(timestamp, 79, "%x %X", localtime(<ime)); +#endif // HAVE_LOCALTIME_R + + fprintf(f,"%s %s\t",timestamp,module); + vfprintf(f,fmt,arg_ptr); + fprintf(f,"\n"); + + fclose(f); + + } else { + + loghandler(session,module,rc,fmt,arg_ptr); + + } + +} + +LIB3270_EXPORT const char * lib3270_get_log_filename(H3270 * hSession) { + return hSession->logfile; +} + +LIB3270_EXPORT int lib3270_set_log_filename(H3270 * hSession, const char *filename) { + + if(hSession->logfile) { + lib3270_free(hSession->logfile); + } + + if(filename && *filename) { + hSession->logfile = lib3270_strdup(filename); + } else { + hSession->logfile = NULL; + } + + return 0; + +} + LIB3270_EXPORT void lib3270_set_log_handler(void (*handler)(H3270 *, const char *, int, const char *, va_list)) { loghandler = handler ? handler : default_log_writer; } @@ -56,7 +108,13 @@ LIB3270_EXPORT void lib3270_set_log_handler(void (*handler)(H3270 *, const char LIB3270_EXPORT int lib3270_write_log(H3270 *session, const char *module, const char *fmt, ...) { va_list arg_ptr; va_start(arg_ptr, fmt); - loghandler(session,module ? module : LIB3270_STRINGIZE_VALUE_OF(PRODUCT_NAME),0,fmt,arg_ptr); + + if(session->logfile) { + logfile(session,module ? module : LIB3270_STRINGIZE_VALUE_OF(PRODUCT_NAME),0,fmt,arg_ptr); + } else { + loghandler(session,module ? module : LIB3270_STRINGIZE_VALUE_OF(PRODUCT_NAME),0,fmt,arg_ptr); + } + va_end(arg_ptr); return 0; } @@ -64,12 +122,22 @@ LIB3270_EXPORT int lib3270_write_log(H3270 *session, const char *module, const c LIB3270_EXPORT int lib3270_write_rc(H3270 *session, const char *module, int rc, const char *fmt, ...) { va_list arg_ptr; va_start(arg_ptr, fmt); - loghandler(session,module ? module : LIB3270_STRINGIZE_VALUE_OF(PRODUCT_NAME),rc,fmt,arg_ptr); + + if(session->logfile) { + logfile(session,module ? module : LIB3270_STRINGIZE_VALUE_OF(PRODUCT_NAME),rc,fmt,arg_ptr); + } else { + loghandler(session,module ? module : LIB3270_STRINGIZE_VALUE_OF(PRODUCT_NAME),rc,fmt,arg_ptr); + } + va_end(arg_ptr); return rc; } LIB3270_EXPORT void lib3270_write_va_log(H3270 *session, const char *module, const char *fmt, va_list arg) { - loghandler(session,module ? module : LIB3270_STRINGIZE_VALUE_OF(PRODUCT_NAME),0,fmt,arg); + if(session->logfile) { + logfile(session,module ? module : LIB3270_STRINGIZE_VALUE_OF(PRODUCT_NAME),0,fmt,arg); + } else { + loghandler(session,module ? module : LIB3270_STRINGIZE_VALUE_OF(PRODUCT_NAME),0,fmt,arg); + } } diff --git a/src/core/properties/string.c b/src/core/properties/string.c index d07e6d1..8bf171b 100644 --- a/src/core/properties/string.c +++ b/src/core/properties/string.c @@ -183,6 +183,14 @@ LIB3270_EXPORT const LIB3270_STRING_PROPERTY * lib3270_get_string_properties_lis }, { + .name = "logfile", // Property name. + .group = LIB3270_ACTION_GROUP_NONE, // Property group. + .description = N_( "The log file name"), // Property description. + .get = lib3270_get_log_filename, // Get value. + .set = lib3270_set_log_filename // Set value. + }, + + { .name = NULL, .description = NULL, .get = NULL, diff --git a/src/core/session.c b/src/core/session.c index 117d883..82741f7 100644 --- a/src/core/session.c +++ b/src/core/session.c @@ -147,7 +147,8 @@ void lib3270_session_free(H3270 *h) { // Release inputs; lib3270_linked_list_free(&h->input.list); - trace("Releasing session %p",h); + // Release logfile + release_pointer(h->logfile); lib3270_free(h); } diff --git a/src/include/internals.h b/src/include/internals.h index c7cfb22..4c9ed5b 100644 --- a/src/include/internals.h +++ b/src/include/internals.h @@ -653,6 +653,9 @@ struct _h3270 { unsigned int tasks; + /// @brief Log file name (if set) + char *logfile; + }; #define SELECTION_LEFT 0x01 diff --git a/src/include/lib3270/log.h b/src/include/lib3270/log.h index 86ece65..5a86aeb 100644 --- a/src/include/lib3270/log.h +++ b/src/include/lib3270/log.h @@ -57,6 +57,9 @@ LIB3270_EXPORT int lib3270_write_log(H3270 *session, const char *module, const LIB3270_EXPORT int lib3270_write_rc(H3270 *session, const char *module, int rc, const char *fmt, ...) LIB3270_GNUC_FORMAT(4,5); LIB3270_EXPORT void lib3270_write_va_log(H3270 *session, const char *module, const char *fmt, va_list arg); +LIB3270_EXPORT int lib3270_set_log_filename(H3270 * hSession, const char *name); +LIB3270_EXPORT const char * lib3270_get_log_filename(H3270 * hSession); + /** * @brief Send logs to system log (if available) * -- libgit2 0.21.2