Commit 5cfbfd8a545f3beedaeacc831dc5ec4b6fe6d636
1 parent
0a66b0a0
Exists in
master
and in
2 other branches
Adding property to set a session based log file.
Showing
5 changed files
with
87 additions
and
4 deletions
Show diff stats
src/core/log.c
| ... | ... | @@ -30,6 +30,8 @@ |
| 30 | 30 | * |
| 31 | 31 | */ |
| 32 | 32 | |
| 33 | +#include <config.h> | |
| 34 | + | |
| 33 | 35 | #ifdef WIN32 |
| 34 | 36 | #include <winsock2.h> |
| 35 | 37 | #include <windows.h> |
| ... | ... | @@ -49,6 +51,56 @@ static void (*loghandler)(H3270 *session, const char *module, int rc, const char |
| 49 | 51 | |
| 50 | 52 | /*---[ Implementacao ]--------------------------------------------------------------------------------------*/ |
| 51 | 53 | |
| 54 | +static void logfile(H3270 *session, const char *module, int rc, const char *fmt, va_list arg_ptr) { | |
| 55 | + | |
| 56 | + FILE *f = fopen(session->logfile, "a"); | |
| 57 | + | |
| 58 | + if(f) { | |
| 59 | + | |
| 60 | + time_t ltime = time(0); | |
| 61 | + | |
| 62 | + char timestamp[80]; | |
| 63 | +#ifdef HAVE_LOCALTIME_R | |
| 64 | + struct tm tm; | |
| 65 | + strftime(timestamp, 79, "%x %X", localtime_r(<ime,&tm)); | |
| 66 | +#else | |
| 67 | + strftime(timestamp, 79, "%x %X", localtime(<ime)); | |
| 68 | +#endif // HAVE_LOCALTIME_R | |
| 69 | + | |
| 70 | + fprintf(f,"%s %s\t",timestamp,module); | |
| 71 | + vfprintf(f,fmt,arg_ptr); | |
| 72 | + fprintf(f,"\n"); | |
| 73 | + | |
| 74 | + fclose(f); | |
| 75 | + | |
| 76 | + } else { | |
| 77 | + | |
| 78 | + loghandler(session,module,rc,fmt,arg_ptr); | |
| 79 | + | |
| 80 | + } | |
| 81 | + | |
| 82 | +} | |
| 83 | + | |
| 84 | +LIB3270_EXPORT const char * lib3270_get_log_filename(H3270 * hSession) { | |
| 85 | + return hSession->logfile; | |
| 86 | +} | |
| 87 | + | |
| 88 | +LIB3270_EXPORT int lib3270_set_log_filename(H3270 * hSession, const char *filename) { | |
| 89 | + | |
| 90 | + if(hSession->logfile) { | |
| 91 | + lib3270_free(hSession->logfile); | |
| 92 | + } | |
| 93 | + | |
| 94 | + if(filename && *filename) { | |
| 95 | + hSession->logfile = lib3270_strdup(filename); | |
| 96 | + } else { | |
| 97 | + hSession->logfile = NULL; | |
| 98 | + } | |
| 99 | + | |
| 100 | + return 0; | |
| 101 | + | |
| 102 | +} | |
| 103 | + | |
| 52 | 104 | LIB3270_EXPORT void lib3270_set_log_handler(void (*handler)(H3270 *, const char *, int, const char *, va_list)) { |
| 53 | 105 | loghandler = handler ? handler : default_log_writer; |
| 54 | 106 | } |
| ... | ... | @@ -56,7 +108,13 @@ LIB3270_EXPORT void lib3270_set_log_handler(void (*handler)(H3270 *, const char |
| 56 | 108 | LIB3270_EXPORT int lib3270_write_log(H3270 *session, const char *module, const char *fmt, ...) { |
| 57 | 109 | va_list arg_ptr; |
| 58 | 110 | va_start(arg_ptr, fmt); |
| 59 | - loghandler(session,module ? module : LIB3270_STRINGIZE_VALUE_OF(PRODUCT_NAME),0,fmt,arg_ptr); | |
| 111 | + | |
| 112 | + if(session->logfile) { | |
| 113 | + logfile(session,module ? module : LIB3270_STRINGIZE_VALUE_OF(PRODUCT_NAME),0,fmt,arg_ptr); | |
| 114 | + } else { | |
| 115 | + loghandler(session,module ? module : LIB3270_STRINGIZE_VALUE_OF(PRODUCT_NAME),0,fmt,arg_ptr); | |
| 116 | + } | |
| 117 | + | |
| 60 | 118 | va_end(arg_ptr); |
| 61 | 119 | return 0; |
| 62 | 120 | } |
| ... | ... | @@ -64,12 +122,22 @@ LIB3270_EXPORT int lib3270_write_log(H3270 *session, const char *module, const c |
| 64 | 122 | LIB3270_EXPORT int lib3270_write_rc(H3270 *session, const char *module, int rc, const char *fmt, ...) { |
| 65 | 123 | va_list arg_ptr; |
| 66 | 124 | va_start(arg_ptr, fmt); |
| 67 | - loghandler(session,module ? module : LIB3270_STRINGIZE_VALUE_OF(PRODUCT_NAME),rc,fmt,arg_ptr); | |
| 125 | + | |
| 126 | + if(session->logfile) { | |
| 127 | + logfile(session,module ? module : LIB3270_STRINGIZE_VALUE_OF(PRODUCT_NAME),rc,fmt,arg_ptr); | |
| 128 | + } else { | |
| 129 | + loghandler(session,module ? module : LIB3270_STRINGIZE_VALUE_OF(PRODUCT_NAME),rc,fmt,arg_ptr); | |
| 130 | + } | |
| 131 | + | |
| 68 | 132 | va_end(arg_ptr); |
| 69 | 133 | return rc; |
| 70 | 134 | } |
| 71 | 135 | |
| 72 | 136 | LIB3270_EXPORT void lib3270_write_va_log(H3270 *session, const char *module, const char *fmt, va_list arg) { |
| 73 | - loghandler(session,module ? module : LIB3270_STRINGIZE_VALUE_OF(PRODUCT_NAME),0,fmt,arg); | |
| 137 | + if(session->logfile) { | |
| 138 | + logfile(session,module ? module : LIB3270_STRINGIZE_VALUE_OF(PRODUCT_NAME),0,fmt,arg); | |
| 139 | + } else { | |
| 140 | + loghandler(session,module ? module : LIB3270_STRINGIZE_VALUE_OF(PRODUCT_NAME),0,fmt,arg); | |
| 141 | + } | |
| 74 | 142 | } |
| 75 | 143 | ... | ... |
src/core/properties/string.c
| ... | ... | @@ -183,6 +183,14 @@ LIB3270_EXPORT const LIB3270_STRING_PROPERTY * lib3270_get_string_properties_lis |
| 183 | 183 | }, |
| 184 | 184 | |
| 185 | 185 | { |
| 186 | + .name = "logfile", // Property name. | |
| 187 | + .group = LIB3270_ACTION_GROUP_NONE, // Property group. | |
| 188 | + .description = N_( "The log file name"), // Property description. | |
| 189 | + .get = lib3270_get_log_filename, // Get value. | |
| 190 | + .set = lib3270_set_log_filename // Set value. | |
| 191 | + }, | |
| 192 | + | |
| 193 | + { | |
| 186 | 194 | .name = NULL, |
| 187 | 195 | .description = NULL, |
| 188 | 196 | .get = NULL, | ... | ... |
src/core/session.c
| ... | ... | @@ -147,7 +147,8 @@ void lib3270_session_free(H3270 *h) { |
| 147 | 147 | // Release inputs; |
| 148 | 148 | lib3270_linked_list_free(&h->input.list); |
| 149 | 149 | |
| 150 | - trace("Releasing session %p",h); | |
| 150 | + // Release logfile | |
| 151 | + release_pointer(h->logfile); | |
| 151 | 152 | lib3270_free(h); |
| 152 | 153 | |
| 153 | 154 | } | ... | ... |
src/include/internals.h
src/include/lib3270/log.h
| ... | ... | @@ -57,6 +57,9 @@ LIB3270_EXPORT int lib3270_write_log(H3270 *session, const char *module, const |
| 57 | 57 | LIB3270_EXPORT int lib3270_write_rc(H3270 *session, const char *module, int rc, const char *fmt, ...) LIB3270_GNUC_FORMAT(4,5); |
| 58 | 58 | LIB3270_EXPORT void lib3270_write_va_log(H3270 *session, const char *module, const char *fmt, va_list arg); |
| 59 | 59 | |
| 60 | +LIB3270_EXPORT int lib3270_set_log_filename(H3270 * hSession, const char *name); | |
| 61 | +LIB3270_EXPORT const char * lib3270_get_log_filename(H3270 * hSession); | |
| 62 | + | |
| 60 | 63 | /** |
| 61 | 64 | * @brief Send logs to system log (if available) |
| 62 | 65 | * | ... | ... |