Commit b17c9e88e2f9e3904562ab37ec53d3dbf9eb9e42

Authored by Perry Werneck
1 parent 55fe0b61
Exists in master and in 2 other branches develop, macos

Working on log/trace files.

src/core/linux/log.c
... ... @@ -42,20 +42,16 @@
42 42  
43 43 int use_syslog = 0;
44 44  
45   -int default_log_writer(H3270 GNUC_UNUSED(*session), const char *module, int GNUC_UNUSED(rc), const char *fmt, va_list arg_ptr) {
  45 +int default_log_writer(H3270 GNUC_UNUSED(*session), void GNUC_UNUSED(*userdata), const char *module, int GNUC_UNUSED(rc), const char *message) {
46 46 #ifdef HAVE_SYSLOG
47 47 if(use_syslog) {
48   - vsyslog(LOG_INFO, fmt, arg_ptr);
  48 + syslog(LOG_INFO, "%s: %s", module, message);
49 49 } else {
50   - printf("%s:\t",module);
51   - vprintf(fmt,arg_ptr);
52   - printf("\n");
  50 + printf("%s %s\n", module, message);
53 51 fflush(stdout);
54 52 }
55 53 #else
56   - printf("%s:\t",module);
57   - vprintf(fmt,arg_ptr);
58   - printf("\n");
  54 + printf("%s %s\n", module, message);
59 55 fflush(stdout);
60 56 #endif
61 57 return 0;
... ...
src/core/log.c
... ... @@ -51,8 +51,12 @@ LIB3270_LOG_HANDLER loghandler = default_log_writer;
51 51  
52 52 /*---[ Implementacao ]--------------------------------------------------------------------------------------*/
53 53  
54   -static void write_log(const H3270 *session, const char *module, int rc, const char *fmt, va_list arg_ptr) {
  54 +static void write_log(const H3270 *session, const char *module, int rc, const char *fmt, va_list args) {
55 55  
  56 + // 'mount' message.
  57 + char *message = lib3270_vsprintf(fmt,args);
  58 +
  59 + // Write log
56 60 if(session) {
57 61  
58 62 if(session->log.file) {
... ... @@ -72,9 +76,7 @@ static void write_log(const H3270 *session, const char *module, int rc, const ch
72 76 strftime(timestamp, 79, "%x %X", localtime(&ltime));
73 77 #endif // HAVE_LOCALTIME_R
74 78  
75   - fprintf(f,"%s %s\t",timestamp,module);
76   - vfprintf(f,fmt,arg_ptr);
77   - fprintf(f,"\n");
  79 + fprintf(f,"%s %s\t%s\n",timestamp,module,message);
78 80  
79 81 fclose(f);
80 82  
... ... @@ -82,14 +84,16 @@ static void write_log(const H3270 *session, const char *module, int rc, const ch
82 84  
83 85 }
84 86  
85   - session->log.handler(session,module ? module : LIB3270_STRINGIZE_VALUE_OF(PRODUCT_NAME),rc,fmt,arg_ptr);
  87 + session->log.handler(session,session->log.userdata,module ? module : LIB3270_STRINGIZE_VALUE_OF(PRODUCT_NAME),rc,message);
86 88  
87 89 } else {
88 90  
89   - loghandler(session, (module ? module : LIB3270_STRINGIZE_VALUE_OF(PRODUCT_NAME)),rc,fmt,arg_ptr);
  91 + loghandler(session, NULL, (module ? module : LIB3270_STRINGIZE_VALUE_OF(PRODUCT_NAME)),rc,message);
90 92  
91 93 }
92 94  
  95 + lib3270_free(message);
  96 +
93 97 }
94 98  
95 99 LIB3270_EXPORT const char * lib3270_get_log_filename(const H3270 * hSession) {
... ...
src/core/session.c
... ... @@ -199,8 +199,8 @@ static int load(H3270 *session, const char GNUC_UNUSED(*filename)) {
199 199 return errno = ENOTSUP;
200 200 }
201 201  
202   -static int def_trace(const H3270 GNUC_UNUSED(*session), void GNUC_UNUSED(*userdata), const char *fmt, va_list args) {
203   - vfprintf(stdout,fmt,args);
  202 +static int def_trace(const H3270 GNUC_UNUSED(*session), void GNUC_UNUSED(*userdata), const char *message) {
  203 + printf("%s",message);
204 204 fflush(stdout);
205 205 return 0;
206 206 }
... ...
src/core/trace_ds.c
... ... @@ -73,6 +73,53 @@
73 73 /* Statics */
74 74 static void wtrace(H3270 *session, const char *fmt, ...);
75 75  
  76 +static void write_trace(const H3270 *session, const char *fmt, va_list args) {
  77 +
  78 + // 'mount' message.
  79 + char *message = lib3270_vsprintf(fmt,args);
  80 +
  81 + if(session->trace.file) {
  82 +
  83 + // Has log file. Use it if possible.
  84 + FILE *f = fopen(session->trace.file, "a");
  85 +
  86 + if(f) {
  87 +
  88 + time_t ltime = time(0);
  89 +
  90 + char timestamp[80];
  91 +#ifdef HAVE_LOCALTIME_R
  92 + struct tm tm;
  93 + strftime(timestamp, 79, "%x %X", localtime_r(&ltime,&tm));
  94 +#else
  95 + strftime(timestamp, 79, "%x %X", localtime(&ltime));
  96 +#endif // HAVE_LOCALTIME_R
  97 +
  98 + fprintf(f,"%s %s\n",timestamp,message);
  99 +
  100 + fclose(f);
  101 +
  102 + }
  103 +
  104 + }
  105 +
  106 + session->trace.handler(session,session->trace.userdata,message);
  107 +
  108 + lib3270_free(message);
  109 +
  110 +}
  111 +
  112 +/**
  113 + * @brief Write to the trace file.
  114 + */
  115 +static void wtrace(H3270 *session, const char *fmt, ...) {
  116 + va_list args;
  117 + va_start(args, fmt);
  118 + write_trace(session, fmt, args);
  119 + va_end(args);
  120 +}
  121 +
  122 +
76 123 /* display a (row,col) */
77 124 const char * rcba(H3270 *hSession, int baddr) {
78 125 static char buf[48];
... ... @@ -161,7 +208,7 @@ void trace_dsn(H3270 *session, const char *fmt, ...) {
161 208  
162 209 /* print out message */
163 210 va_start(args, fmt);
164   - session->trace.handler(session,session->trace.userdata,fmt, args);
  211 + write_trace(session,fmt,args);
165 212 va_end(args);
166 213 }
167 214  
... ... @@ -176,18 +223,7 @@ void trace_ssl(H3270 *session, const char *fmt, ...) {
176 223  
177 224 /* print out message */
178 225 va_start(args, fmt);
179   - session->trace.handler(session,session->trace.userdata,fmt, args);
180   - va_end(args);
181   -}
182   -
183   -
184   -/**
185   - * @brief Write to the trace file.
186   - */
187   -static void wtrace(H3270 *session, const char *fmt, ...) {
188   - va_list args;
189   - va_start(args, fmt);
190   - session->trace.handler(session,session->trace.userdata,fmt, args);
  226 + write_trace(session, fmt, args);
191 227 va_end(args);
192 228 }
193 229  
... ... @@ -195,7 +231,7 @@ LIB3270_EXPORT void lib3270_write_trace(H3270 *session, const char *fmt, ...) {
195 231 va_list args;
196 232  
197 233 va_start(args, fmt);
198   - session->trace.handler(session,session->trace.userdata,fmt, args);
  234 + write_trace(session, fmt, args);
199 235 va_end(args);
200 236 }
201 237  
... ... @@ -206,7 +242,7 @@ LIB3270_EXPORT void lib3270_write_dstrace(H3270 *session, const char *fmt, ...)
206 242 return;
207 243  
208 244 va_start(args, fmt);
209   - session->trace.handler(session,session->trace.userdata,fmt, args);
  245 + write_trace(session, fmt, args);
210 246 va_end(args);
211 247 }
212 248  
... ... @@ -217,7 +253,7 @@ LIB3270_EXPORT void lib3270_write_nettrace(H3270 *session, const char *fmt, ...)
217 253 return;
218 254  
219 255 va_start(args, fmt);
220   - session->trace.handler(session,session->trace.userdata,fmt, args);
  256 + write_trace(session, fmt, args);
221 257 va_end(args);
222 258 }
223 259  
... ... @@ -228,7 +264,7 @@ LIB3270_EXPORT void lib3270_write_screen_trace(H3270 *session, const char *fmt,
228 264 return;
229 265  
230 266 va_start(args, fmt);
231   - session->trace.handler(session,session->trace.userdata,fmt, args);
  267 + write_trace(session, fmt, args);
232 268 va_end(args);
233 269 }
234 270  
... ... @@ -239,7 +275,7 @@ LIB3270_EXPORT void lib3270_write_event_trace(H3270 *session, const char *fmt, .
239 275 return;
240 276  
241 277 va_start(args, fmt);
242   - session->trace.handler(session,session->trace.userdata,fmt, args);
  278 + write_trace(session, fmt, args);
243 279 va_end(args);
244 280 }
245 281  
... ... @@ -250,7 +286,7 @@ LIB3270_EXPORT void lib3270_trace_event(H3270 *session, const char *fmt, ...) {
250 286 return;
251 287  
252 288 va_start(args, fmt);
253   - session->trace.handler(session,session->trace.userdata,fmt, args);
  289 + write_trace(session, fmt, args);
254 290 va_end(args);
255 291 }
256 292  
... ...
src/core/windows/log.c
... ... @@ -41,10 +41,7 @@
41 41  
42 42 /*---[ Implement ]------------------------------------------------------------------------------------------*/
43 43  
44   -int default_log_writer(H3270 GNUC_UNUSED(*session), const char *module, int rc, const char *fmt, va_list arg_ptr) {
45   - lib3270_autoptr(char) msg = lib3270_vsprintf(fmt,arg_ptr);
46   -
47   - debug("%s",msg);
  44 +int default_log_writer(H3270 GNUC_UNUSED(*session), const char *module, int rc, const char *msg) {
48 45  
49 46 if(hEventLog) {
50 47 lib3270_autoptr(char) username = lib3270_get_user_name();
... ...
src/include/internals.h
... ... @@ -635,6 +635,7 @@ struct _h3270 {
635 635 struct {
636 636 char *file; ///< @brief Log file name (if set).
637 637 LIB3270_LOG_HANDLER handler;
  638 + void *userdata;
638 639 } log;
639 640  
640 641 struct {
... ... @@ -739,7 +740,7 @@ LIB3270_INTERNAL void clear_chr(H3270 *hSession, int baddr);
739 740 LIB3270_INTERNAL unsigned char get_field_attribute(H3270 *session, int baddr);
740 741  
741 742 /// @brief Default log writer.
742   -LIB3270_INTERNAL int default_log_writer(H3270 *session, const char *module, int rc, const char *fmt, va_list arg_ptr);
  743 +LIB3270_INTERNAL int default_log_writer(H3270 *session, void *dunno, const char *module, int rc, const char *message);
743 744  
744 745 /// @brief The active log handler.
745 746 LIB3270_INTERNAL LIB3270_LOG_HANDLER loghandler;
... ...
src/include/lib3270/log.h
... ... @@ -52,7 +52,7 @@
52 52 extern "C" {
53 53 #endif
54 54  
55   -typedef int (*LIB3270_LOG_HANDLER)(const H3270 *, const char *, int, const char *, va_list);
  55 +typedef int (*LIB3270_LOG_HANDLER)(const H3270 *, void *, const char *, int, const char *);
56 56  
57 57 LIB3270_EXPORT void lib3270_set_log_handler(H3270 *session, const LIB3270_LOG_HANDLER loghandler);
58 58 LIB3270_EXPORT int lib3270_set_log_filename(H3270 * hSession, const char *name);
... ...
src/include/lib3270/trace.h
... ... @@ -45,7 +45,7 @@ extern &quot;C&quot; {
45 45 #define LIB3270_AS_PRINTF(a,b) __attribute__((format(printf, a, b)))
46 46 #endif
47 47  
48   -typedef int (*LIB3270_TRACE_HANDLER)(const H3270 *, void *, const char *, va_list);
  48 +typedef int (*LIB3270_TRACE_HANDLER)(const H3270 *, void *, const char *);
49 49  
50 50 /**
51 51 * @brief Set trace filename.
... ...
src/testprogram/testprogram.c
... ... @@ -50,16 +50,6 @@
50 50  
51 51 const char *trace_file = "test.trace";
52 52  
53   -static int write_trace(const H3270 GNUC_UNUSED(*session), void GNUC_UNUSED(*userdata), const char *fmt, va_list args) {
54   - FILE *out = fopen(trace_file,"a");
55   - if(out) {
56   -
57   - vfprintf(out,fmt,args);
58   - fclose(out);
59   - }
60   - return 0;
61   -}
62   -
63 53 static void online_group_state_changed(H3270 GNUC_UNUSED(*hSession), void GNUC_UNUSED(*dunno)) {
64 54 printf("\n\n%s\n\n",__FUNCTION__);
65 55 }
... ... @@ -138,8 +128,7 @@ int main(int argc, char *argv[]) {
138 128 return 0;
139 129  
140 130 case 't':
141   - trace_file = optarg;
142   - lib3270_set_trace_handler(h,write_trace,NULL);
  131 + lib3270_set_trace_filename(h,optarg);
143 132 lib3270_set_toggle(h,LIB3270_TOGGLE_DS_TRACE,1);
144 133 break;
145 134 }
... ...