diff --git a/src/core/linux/log.c b/src/core/linux/log.c index d83c6d4..221890c 100644 --- a/src/core/linux/log.c +++ b/src/core/linux/log.c @@ -42,7 +42,7 @@ int use_syslog = 0; -void default_log_writer(H3270 GNUC_UNUSED(*session), const char *module, int GNUC_UNUSED(rc), const char *fmt, va_list arg_ptr) { +int default_log_writer(H3270 GNUC_UNUSED(*session), const char *module, int GNUC_UNUSED(rc), const char *fmt, va_list arg_ptr) { #ifdef HAVE_SYSLOG if(use_syslog) { vsyslog(LOG_INFO, fmt, arg_ptr); @@ -58,6 +58,7 @@ void default_log_writer(H3270 GNUC_UNUSED(*session), const char *module, int GNU printf("\n"); fflush(stdout); #endif + return 0; } LIB3270_EXPORT int lib3270_set_syslog(int flag) { diff --git a/src/core/log.c b/src/core/log.c index 064d84c..2eef69c 100644 --- a/src/core/log.c +++ b/src/core/log.c @@ -47,98 +47,100 @@ /*---[ Constants ]------------------------------------------------------------------------------------------*/ -static void (*loghandler)(H3270 *session, const char *module, int rc, const char *fmt, va_list arg_ptr) = default_log_writer; +LIB3270_LOG_HANDLER loghandler = default_log_writer; /*---[ Implementacao ]--------------------------------------------------------------------------------------*/ -static void logfile(H3270 *session, const char *module, int rc, const char *fmt, va_list arg_ptr) { +static void write_log(const H3270 *session, const char *module, int rc, const char *fmt, va_list arg_ptr) { - FILE *f = fopen(session->file.log, "a"); + if(session) { - if(f) { + if(session->log.file) { - time_t ltime = time(0); + // Has log file. Use it if possible. + FILE *f = fopen(session->log.file, "a"); - 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 + if(f) { - fprintf(f,"%s %s\t",timestamp,module); - vfprintf(f,fmt,arg_ptr); - fprintf(f,"\n"); + time_t ltime = time(0); - fclose(f); + 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); + + } + + } + + session->log.handler(session,module ? module : LIB3270_STRINGIZE_VALUE_OF(PRODUCT_NAME),rc,fmt,arg_ptr); } else { - loghandler(session,module,rc,fmt,arg_ptr); + loghandler(session, (module ? module : LIB3270_STRINGIZE_VALUE_OF(PRODUCT_NAME)),rc,fmt,arg_ptr); } } LIB3270_EXPORT const char * lib3270_get_log_filename(const H3270 * hSession) { - return hSession->file.log; + return hSession->log.file; } LIB3270_EXPORT int lib3270_set_log_filename(H3270 * hSession, const char *filename) { - if(hSession->file.log) { - lib3270_free(hSession->file.log); - hSession->file.log = NULL; + if(!hSession) { + return EINVAL; } + if(hSession->log.file) { + lib3270_free(hSession->log.file); + } + + hSession->log.file = NULL; + if(filename && *filename) { - hSession->file.log = lib3270_strdup(filename); - } else { - hSession->file.log = NULL; + hSession->log.file = lib3270_strdup(filename); } 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; +LIB3270_EXPORT void lib3270_set_log_handler(H3270 *session, const LIB3270_LOG_HANDLER handler) { + if(session) { + session->log.handler = (handler ? handler : default_log_writer); + } else { + loghandler = (handler ? handler : default_log_writer); + } } -LIB3270_EXPORT int lib3270_write_log(H3270 *session, const char *module, const char *fmt, ...) { +LIB3270_EXPORT int lib3270_write_log(const H3270 *session, const char *module, const char *fmt, ...) { va_list arg_ptr; va_start(arg_ptr, fmt); - - if(session && session->file.log) { - 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); - } - + write_log(session,module ? module : LIB3270_STRINGIZE_VALUE_OF(PRODUCT_NAME),0,fmt,arg_ptr); va_end(arg_ptr); return 0; } -LIB3270_EXPORT int lib3270_write_rc(H3270 *session, const char *module, int rc, const char *fmt, ...) { +LIB3270_EXPORT int lib3270_write_rc(const H3270 *session, const char *module, int rc, const char *fmt, ...) { va_list arg_ptr; va_start(arg_ptr, fmt); - - if(session && session->file.log) { - 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); - } - + write_log(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) { - if(session && session->file.log) { - 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); - } +LIB3270_EXPORT void lib3270_write_va_log(const H3270 *session, const char *module, const char *fmt, va_list arg) { + write_log(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 2462364..db51391 100644 --- a/src/core/properties/string.c +++ b/src/core/properties/string.c @@ -36,6 +36,7 @@ #include #include #include +#include static const char * get_version(const H3270 GNUC_UNUSED(*hSession)) { return lib3270_get_version(); diff --git a/src/core/session.c b/src/core/session.c index 09f75de..d305f58 100644 --- a/src/core/session.c +++ b/src/core/session.c @@ -148,8 +148,8 @@ void lib3270_session_free(H3270 *h) { lib3270_linked_list_free(&h->input.list); // Release logfile - release_pointer(h->file.log); - release_pointer(h->file.trace); + release_pointer(h->log.file); + release_pointer(h->trace.file); lib3270_free(h); } @@ -199,9 +199,10 @@ static int load(H3270 *session, const char GNUC_UNUSED(*filename)) { return errno = ENOTSUP; } -static void def_trace(H3270 GNUC_UNUSED(*session), void GNUC_UNUSED(*userdata), const char *fmt, va_list args) { +static int def_trace(const H3270 GNUC_UNUSED(*session), void GNUC_UNUSED(*userdata), const char *fmt, va_list args) { vfprintf(stdout,fmt,args); fflush(stdout); + return 0; } static void update_ssl(H3270 GNUC_UNUSED(*session), LIB3270_SSL_STATE GNUC_UNUSED(state)) { @@ -279,6 +280,9 @@ static void lib3270_session_init(H3270 *hSession, const char *model, const char // Trace management. hSession->trace.handler = def_trace; + // Log management. + hSession->log.handler = loghandler; + // Set the defaults. hSession->extended = 1; hSession->typeahead = 1; @@ -489,7 +493,7 @@ LIB3270_EXPORT char lib3270_get_session_id(H3270 *hSession) { struct lib3270_session_callbacks * lib3270_get_session_callbacks(H3270 *hSession, const char *revision, unsigned short sz) { - #define REQUIRED_REVISION "20210619" + #define REQUIRED_REVISION "20210818" if(revision && strcasecmp(revision,REQUIRED_REVISION) < 0) { errno = EINVAL; diff --git a/src/core/trace_ds.c b/src/core/trace_ds.c index 5eee2be..ad4b776 100644 --- a/src/core/trace_ds.c +++ b/src/core/trace_ds.c @@ -348,21 +348,19 @@ void lib3270_trace_data(H3270 *hSession, const char *msg, const unsigned char *d } -LIB3270_EXPORT const char * lib3270_get_trace_filename(H3270 * hSession) { - return hSession->file.trace; +LIB3270_EXPORT const char * lib3270_get_trace_filename(const H3270 * hSession) { + return hSession->trace.file; } LIB3270_EXPORT int lib3270_set_trace_filename(H3270 * hSession, const char *filename) { - if(hSession->file.trace) { - lib3270_free(hSession->file.trace); - hSession->file.trace = NULL; + if(hSession->trace.file) { + lib3270_free(hSession->trace.file); } + hSession->trace.file = NULL; if(filename && *filename) { - hSession->file.trace = lib3270_strdup(filename); - } else { - hSession->file.trace = NULL; + hSession->trace.file = lib3270_strdup(filename); } return 0; diff --git a/src/core/windows/log.c b/src/core/windows/log.c index 3a6db83..4b9d08b 100644 --- a/src/core/windows/log.c +++ b/src/core/windows/log.c @@ -41,7 +41,7 @@ /*---[ Implement ]------------------------------------------------------------------------------------------*/ -void default_log_writer(H3270 GNUC_UNUSED(*session), const char *module, int rc, const char *fmt, va_list arg_ptr) { +int default_log_writer(H3270 GNUC_UNUSED(*session), const char *module, int rc, const char *fmt, va_list arg_ptr) { lib3270_autoptr(char) msg = lib3270_vsprintf(fmt,arg_ptr); debug("%s",msg); @@ -74,6 +74,7 @@ void default_log_writer(H3270 GNUC_UNUSED(*session), const char *module, int rc, } + return 0; } LIB3270_EXPORT int lib3270_set_syslog(int GNUC_UNUSED(flag)) { diff --git a/src/include/internals.h b/src/include/internals.h index 4b77129..6e4fa14 100644 --- a/src/include/internals.h +++ b/src/include/internals.h @@ -41,6 +41,8 @@ #include #include #include +#include +#include #if defined(HAVE_LDAP) && defined (HAVE_LIBSSL) #include @@ -625,11 +627,17 @@ struct _h3270 { // Trace methods. struct { - void (*handler)(H3270 *session, void *userdata, const char *fmt, va_list args); + char *file; ///< @brief Trace file name (if set). + LIB3270_TRACE_HANDLER handler; void *userdata; } trace; struct { + char *file; ///< @brief Log file name (if set). + LIB3270_LOG_HANDLER handler; + } log; + + struct { unsigned int host : 1; ///< @brief Non zero if host requires SSL. unsigned int download_crl : 1; ///< @brief Non zero to download CRL. LIB3270_SSL_STATE state; @@ -653,11 +661,6 @@ struct _h3270 { unsigned int tasks; - struct { - char *log; ///< @brief Log file name (if set). - char *trace; ///< @brief Trace file name (if set). - } file; - }; #define SELECTION_LEFT 0x01 @@ -736,7 +739,10 @@ LIB3270_INTERNAL void clear_chr(H3270 *hSession, int baddr); LIB3270_INTERNAL unsigned char get_field_attribute(H3270 *session, int baddr); /// @brief Default log writer. -LIB3270_INTERNAL void default_log_writer(H3270 *session, const char *module, int rc, const char *fmt, va_list arg_ptr); +LIB3270_INTERNAL int default_log_writer(H3270 *session, const char *module, int rc, const char *fmt, va_list arg_ptr); + +/// @brief The active log handler. +LIB3270_INTERNAL LIB3270_LOG_HANDLER loghandler; LIB3270_INTERNAL char * lib3270_get_user_name(); diff --git a/src/include/lib3270/log.h b/src/include/lib3270/log.h index baeccdc..a877281 100644 --- a/src/include/lib3270/log.h +++ b/src/include/lib3270/log.h @@ -52,16 +52,16 @@ extern "C" { #endif -LIB3270_EXPORT void lib3270_set_log_handler(void (*loghandler)(H3270 *, const char *, int, const char *, va_list)); -LIB3270_EXPORT int lib3270_write_log(H3270 *session, const char *module, const char *fmt, ...) LIB3270_GNUC_FORMAT(3,4); -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); +typedef int (*LIB3270_LOG_HANDLER)(const H3270 *, const char *, int, const char *, va_list); +LIB3270_EXPORT void lib3270_set_log_handler(H3270 *session, const LIB3270_LOG_HANDLER loghandler); LIB3270_EXPORT int lib3270_set_log_filename(H3270 * hSession, const char *name); LIB3270_EXPORT const char * lib3270_get_log_filename(const H3270 * hSession); -LIB3270_EXPORT int lib3270_set_trace_filename(H3270 * hSession, const char *name); -LIB3270_EXPORT const char * lib3270_get_trace_filename(const H3270 * hSession); +LIB3270_EXPORT int lib3270_write_log(const H3270 *session, const char *module, const char *fmt, ...) LIB3270_GNUC_FORMAT(3,4); +LIB3270_EXPORT int lib3270_write_rc(const H3270 *session, const char *module, int rc, const char *fmt, ...) LIB3270_GNUC_FORMAT(4,5); +LIB3270_EXPORT void lib3270_write_va_log(const H3270 *session, const char *module, const char *fmt, va_list arg); + /** * @brief Send logs to system log (if available) diff --git a/src/include/lib3270/trace.h b/src/include/lib3270/trace.h index c0d6da3..e2bbebb 100644 --- a/src/include/lib3270/trace.h +++ b/src/include/lib3270/trace.h @@ -45,7 +45,25 @@ extern "C" { #define LIB3270_AS_PRINTF(a,b) __attribute__((format(printf, a, b))) #endif -typedef void (*LIB3270_TRACE_HANDLER)(H3270 *, void *, const char *, va_list); +typedef int (*LIB3270_TRACE_HANDLER)(const H3270 *, void *, const char *, va_list); + +/** + * @brief Set trace filename. + * + * @param hSession TN3270 Session handle. + * @param name The trace file name (null to disable). + * + */ +LIB3270_EXPORT int lib3270_set_trace_filename(H3270 * hSession, const char *name); + +/** + * @brief Get trace file name. + * + * @param hSession TN3270 Session handle. + * @return The trace file name or NULL if disabled. + * + */ +LIB3270_EXPORT const char * lib3270_get_trace_filename(const H3270 * hSession); /** * @brief Set trace handle callback. diff --git a/src/testprogram/testprogram.c b/src/testprogram/testprogram.c index d463242..b13256e 100644 --- a/src/testprogram/testprogram.c +++ b/src/testprogram/testprogram.c @@ -50,13 +50,14 @@ const char *trace_file = "test.trace"; -static void write_trace(H3270 GNUC_UNUSED(*session), void GNUC_UNUSED(*userdata), const char *fmt, va_list args) { +static int write_trace(const H3270 GNUC_UNUSED(*session), void GNUC_UNUSED(*userdata), const char *fmt, va_list args) { FILE *out = fopen(trace_file,"a"); if(out) { vfprintf(out,fmt,args); fclose(out); } + return 0; } static void online_group_state_changed(H3270 GNUC_UNUSED(*hSession), void GNUC_UNUSED(*dunno)) { @@ -96,11 +97,11 @@ int main(int argc, char *argv[]) { { 0, 0, 0, 0} }; -// #pragma GCC diagnostic pop H3270 * h = lib3270_session_new(""); int rc = 0; + lib3270_set_log_filename(h,"testprogram.log"); lib3270_write_log(h,"TEST","Testprogram %s starts (%s)",argv[0],LIB3270_STRINGIZE_VALUE_OF(PRODUCT_NAME)); lib3270_autoptr(char) version_info = lib3270_get_version_info(); -- libgit2 0.21.2