diff options
author | Christian Grothoff <christian@grothoff.org> | 2012-06-22 22:35:10 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2012-06-22 22:35:10 +0000 |
commit | 669d77b3d4d3aa5ddca24ad6f35db745aea62688 (patch) | |
tree | 59c96b24ca6248160e668d991cc02e0b521af1d1 /src/util/common_logging.c | |
parent | 2018d484bb120ae63843146522d7afd1923d9d00 (diff) | |
download | gnunet-669d77b3d4d3aa5ddca24ad6f35db745aea62688.tar.gz gnunet-669d77b3d4d3aa5ddca24ad6f35db745aea62688.zip |
Diffstat (limited to 'src/util/common_logging.c')
-rw-r--r-- | src/util/common_logging.c | 302 |
1 files changed, 195 insertions, 107 deletions
diff --git a/src/util/common_logging.c b/src/util/common_logging.c index 82878a02a..08aca02ef 100644 --- a/src/util/common_logging.c +++ b/src/util/common_logging.c | |||
@@ -64,6 +64,19 @@ | |||
64 | #define DATE_STR_SIZE 64 | 64 | #define DATE_STR_SIZE 64 |
65 | 65 | ||
66 | /** | 66 | /** |
67 | * How many log files to keep? | ||
68 | */ | ||
69 | #define ROTATION_KEEP 3 | ||
70 | |||
71 | #ifndef PATH_MAX | ||
72 | /** | ||
73 | * Assumed maximum path length (for the log file name). | ||
74 | */ | ||
75 | #define PATH_MAX 4096 | ||
76 | #endif | ||
77 | |||
78 | |||
79 | /** | ||
67 | * Linked list of active loggers. | 80 | * Linked list of active loggers. |
68 | */ | 81 | */ |
69 | struct CustomLogger | 82 | struct CustomLogger |
@@ -122,6 +135,11 @@ static char *component; | |||
122 | static char *component_nopid; | 135 | static char *component_nopid; |
123 | 136 | ||
124 | /** | 137 | /** |
138 | * Format string describing the name of the log file. | ||
139 | */ | ||
140 | static char *log_file_name; | ||
141 | |||
142 | /** | ||
125 | * Minimum log level. | 143 | * Minimum log level. |
126 | */ | 144 | */ |
127 | static enum GNUNET_ErrorType min_level; | 145 | static enum GNUNET_ErrorType min_level; |
@@ -190,40 +208,41 @@ struct LogDef | |||
190 | /** | 208 | /** |
191 | * Dynamic array of logging definitions | 209 | * Dynamic array of logging definitions |
192 | */ | 210 | */ |
193 | struct LogDef *logdefs = NULL; | 211 | static struct LogDef *logdefs; |
194 | 212 | ||
195 | /** | 213 | /** |
196 | * Allocated size of logdefs array (in units) | 214 | * Allocated size of logdefs array (in units) |
197 | */ | 215 | */ |
198 | int logdefs_size = 0; | 216 | static int logdefs_size; |
199 | 217 | ||
200 | /** | 218 | /** |
201 | * The number of units used in logdefs array. | 219 | * The number of units used in logdefs array. |
202 | */ | 220 | */ |
203 | int logdefs_len = 0; | 221 | static int logdefs_len; |
204 | 222 | ||
205 | /** | 223 | /** |
206 | * GNUNET_YES if GNUNET_LOG environment variable is already parsed. | 224 | * GNUNET_YES if GNUNET_LOG environment variable is already parsed. |
207 | */ | 225 | */ |
208 | int gnunet_log_parsed = GNUNET_NO; | 226 | static int gnunet_log_parsed; |
209 | 227 | ||
210 | /** | 228 | /** |
211 | * GNUNET_YES if GNUNET_FORCE_LOG environment variable is already parsed. | 229 | * GNUNET_YES if GNUNET_FORCE_LOG environment variable is already parsed. |
212 | */ | 230 | */ |
213 | int gnunet_force_log_parsed = GNUNET_NO; | 231 | static int gnunet_force_log_parsed; |
214 | 232 | ||
215 | /** | 233 | /** |
216 | * GNUNET_YES if at least one definition with forced == 1 is available. | 234 | * GNUNET_YES if at least one definition with forced == 1 is available. |
217 | */ | 235 | */ |
218 | int gnunet_force_log_present = GNUNET_NO; | 236 | static int gnunet_force_log_present; |
219 | 237 | ||
220 | #ifdef WINDOWS | 238 | #ifdef WINDOWS |
221 | /** | 239 | /** |
222 | * Contains the number of performance counts per second. | 240 | * Contains the number of performance counts per second. |
223 | */ | 241 | */ |
224 | LARGE_INTEGER performance_frequency; | 242 | static LARGE_INTEGER performance_frequency; |
225 | #endif | 243 | #endif |
226 | 244 | ||
245 | |||
227 | /** | 246 | /** |
228 | * Convert a textual description of a loglevel | 247 | * Convert a textual description of a loglevel |
229 | * to the respective GNUNET_GE_KIND. | 248 | * to the respective GNUNET_GE_KIND. |
@@ -234,7 +253,7 @@ LARGE_INTEGER performance_frequency; | |||
234 | static enum GNUNET_ErrorType | 253 | static enum GNUNET_ErrorType |
235 | get_type (const char *log) | 254 | get_type (const char *log) |
236 | { | 255 | { |
237 | if (log == NULL) | 256 | if (NULL == log) |
238 | return GNUNET_ERROR_TYPE_UNSPECIFIED; | 257 | return GNUNET_ERROR_TYPE_UNSPECIFIED; |
239 | if (0 == strcasecmp (log, _("DEBUG"))) | 258 | if (0 == strcasecmp (log, _("DEBUG"))) |
240 | return GNUNET_ERROR_TYPE_DEBUG; | 259 | return GNUNET_ERROR_TYPE_DEBUG; |
@@ -249,6 +268,7 @@ get_type (const char *log) | |||
249 | return GNUNET_ERROR_TYPE_INVALID; | 268 | return GNUNET_ERROR_TYPE_INVALID; |
250 | } | 269 | } |
251 | 270 | ||
271 | |||
252 | #if !defined(GNUNET_CULL_LOGGING) | 272 | #if !defined(GNUNET_CULL_LOGGING) |
253 | /** | 273 | /** |
254 | * Utility function - reallocates logdefs array to be twice as large. | 274 | * Utility function - reallocates logdefs array to be twice as large. |
@@ -275,6 +295,99 @@ GNUNET_abort () | |||
275 | 295 | ||
276 | 296 | ||
277 | /** | 297 | /** |
298 | * Rotate logs, deleting the oldest log. | ||
299 | * | ||
300 | * @param new_name new name to add to the rotation | ||
301 | */ | ||
302 | static void | ||
303 | log_rotate (const char *new_name) | ||
304 | { | ||
305 | static char *rotation[ROTATION_KEEP]; | ||
306 | static unsigned int rotation_off; | ||
307 | char *discard; | ||
308 | |||
309 | if ('\0' == *new_name) | ||
310 | return; /* not a real log file name */ | ||
311 | discard = rotation[rotation_off % ROTATION_KEEP]; | ||
312 | if (NULL != discard) | ||
313 | { | ||
314 | /* Note: can't log errors during logging (recursion!), so this | ||
315 | operation MUST silently fail... */ | ||
316 | (void) UNLINK (discard); | ||
317 | GNUNET_free (discard); | ||
318 | } | ||
319 | rotation[rotation_off % ROTATION_KEEP] = GNUNET_strdup (new_name); | ||
320 | rotation_off++; | ||
321 | } | ||
322 | |||
323 | |||
324 | /** | ||
325 | * Setup the log file. | ||
326 | * | ||
327 | * @param tm timestamp for which we should setup logging | ||
328 | * @return GNUNET_OK on success, GNUNET_SYSERR on error | ||
329 | */ | ||
330 | static int | ||
331 | setup_log_file (const struct tm *tm) | ||
332 | { | ||
333 | static char last_fn[PATH_MAX + 1]; | ||
334 | char fn[PATH_MAX + 1]; | ||
335 | int dirwarn; | ||
336 | int altlog_fd; | ||
337 | int dup_return; | ||
338 | FILE *altlog; | ||
339 | |||
340 | if (0 == strftime (fn, sizeof (fn), log_file_name, tm)) | ||
341 | return GNUNET_SYSERR; | ||
342 | if (0 == strcmp (fn, last_fn)) | ||
343 | return GNUNET_OK; /* no change */ | ||
344 | log_rotate (last_fn); | ||
345 | strcpy (last_fn, fn); | ||
346 | dirwarn = (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn)); | ||
347 | #if WINDOWS | ||
348 | altlog_fd = OPEN (fn, O_APPEND | | ||
349 | O_BINARY | | ||
350 | O_WRONLY | O_CREAT, | ||
351 | _S_IREAD | _S_IWRITE); | ||
352 | #else | ||
353 | altlog_fd = OPEN (fn, O_APPEND | | ||
354 | O_WRONLY | O_CREAT, | ||
355 | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); | ||
356 | #endif | ||
357 | if (-1 != altlog_fd) | ||
358 | { | ||
359 | if (NULL != GNUNET_stderr) | ||
360 | fclose (GNUNET_stderr); | ||
361 | dup_return = dup2 (altlog_fd, 2); | ||
362 | (void) close (altlog_fd); | ||
363 | if (-1 != dup_return) | ||
364 | { | ||
365 | altlog = fdopen (2, "ab"); | ||
366 | if (NULL == altlog) | ||
367 | { | ||
368 | (void) close (2); | ||
369 | altlog_fd = -1; | ||
370 | } | ||
371 | } | ||
372 | else | ||
373 | { | ||
374 | altlog_fd = -1; | ||
375 | } | ||
376 | } | ||
377 | if (-1 == altlog_fd) | ||
378 | { | ||
379 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", fn); | ||
380 | if (dirwarn) | ||
381 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
382 | _("Failed to create or access directory for log file `%s'\n"), | ||
383 | fn); | ||
384 | return GNUNET_SYSERR; | ||
385 | } | ||
386 | GNUNET_stderr = altlog; | ||
387 | return GNUNET_OK; | ||
388 | } | ||
389 | |||
390 | /** | ||
278 | * Utility function - adds a parsed definition to logdefs array. | 391 | * Utility function - adds a parsed definition to logdefs array. |
279 | * | 392 | * |
280 | * @param component see struct LogDef, can't be NULL | 393 | * @param component see struct LogDef, can't be NULL |
@@ -296,25 +409,25 @@ add_definition (char *component, char *file, char *function, int from_line, | |||
296 | if (logdefs_size == logdefs_len) | 409 | if (logdefs_size == logdefs_len) |
297 | resize_logdefs (); | 410 | resize_logdefs (); |
298 | memset (&n, 0, sizeof (n)); | 411 | memset (&n, 0, sizeof (n)); |
299 | if (strlen (component) == 0) | 412 | if (0 == strlen (component)) |
300 | component = (char *) ".*"; | 413 | component = (char *) ".*"; |
301 | r = regcomp (&n.component_regex, (const char *) component, REG_NOSUB); | 414 | r = regcomp (&n.component_regex, (const char *) component, REG_NOSUB); |
302 | if (r != 0) | 415 | if (0 != r) |
303 | { | 416 | { |
304 | return r; | 417 | return r; |
305 | } | 418 | } |
306 | if (strlen (file) == 0) | 419 | if (0 == strlen (file)) |
307 | file = (char *) ".*"; | 420 | file = (char *) ".*"; |
308 | r = regcomp (&n.file_regex, (const char *) file, REG_NOSUB); | 421 | r = regcomp (&n.file_regex, (const char *) file, REG_NOSUB); |
309 | if (r != 0) | 422 | if (0 != r) |
310 | { | 423 | { |
311 | regfree (&n.component_regex); | 424 | regfree (&n.component_regex); |
312 | return r; | 425 | return r; |
313 | } | 426 | } |
314 | if ((NULL == function) || (strlen (function) == 0)) | 427 | if ((NULL == function) || (0 == strlen (function))) |
315 | function = (char *) ".*"; | 428 | function = (char *) ".*"; |
316 | r = regcomp (&n.function_regex, (const char *) function, REG_NOSUB); | 429 | r = regcomp (&n.function_regex, (const char *) function, REG_NOSUB); |
317 | if (r != 0) | 430 | if (0 != r) |
318 | { | 431 | { |
319 | regfree (&n.component_regex); | 432 | regfree (&n.component_regex); |
320 | regfree (&n.file_regex); | 433 | regfree (&n.file_regex); |
@@ -350,14 +463,14 @@ GNUNET_get_log_call_status (int caller_level, const char *comp, | |||
350 | int i; | 463 | int i; |
351 | int force_only; | 464 | int force_only; |
352 | 465 | ||
353 | if (comp == NULL) | 466 | if (NULL == comp) |
354 | /* Use default component */ | 467 | /* Use default component */ |
355 | comp = component_nopid; | 468 | comp = component_nopid; |
356 | 469 | ||
357 | /* We have no definitions to override globally configured log level, | 470 | /* We have no definitions to override globally configured log level, |
358 | * so just use it right away. | 471 | * so just use it right away. |
359 | */ | 472 | */ |
360 | if (min_level >= 0 && gnunet_force_log_present == GNUNET_NO) | 473 | if ( (min_level >= 0) && (GNUNET_NO == gnunet_force_log_present) ) |
361 | return caller_level <= min_level; | 474 | return caller_level <= min_level; |
362 | 475 | ||
363 | /* Only look for forced definitions? */ | 476 | /* Only look for forced definitions? */ |
@@ -365,11 +478,11 @@ GNUNET_get_log_call_status (int caller_level, const char *comp, | |||
365 | for (i = 0; i < logdefs_len; i++) | 478 | for (i = 0; i < logdefs_len; i++) |
366 | { | 479 | { |
367 | ld = &logdefs[i]; | 480 | ld = &logdefs[i]; |
368 | if ((!force_only || ld->force) && | 481 | if (( (!force_only) || ld->force) && |
369 | (line >= ld->from_line && line <= ld->to_line) && | 482 | (line >= ld->from_line && line <= ld->to_line) && |
370 | (regexec (&ld->component_regex, comp, 0, NULL, 0) == 0) && | 483 | (0 == regexec (&ld->component_regex, comp, 0, NULL, 0)) && |
371 | (regexec (&ld->file_regex, file, 0, NULL, 0) == 0) && | 484 | (0 == regexec (&ld->file_regex, file, 0, NULL, 0)) && |
372 | (regexec (&ld->function_regex, function, 0, NULL, 0) == 0)) | 485 | (0 == regexec (&ld->function_regex, function, 0, NULL, 0))) |
373 | { | 486 | { |
374 | /* We're finished */ | 487 | /* We're finished */ |
375 | return caller_level <= ld->level; | 488 | return caller_level <= ld->level; |
@@ -426,7 +539,7 @@ parse_definitions (const char *constname, int force) | |||
426 | int keep_looking = 1; | 539 | int keep_looking = 1; |
427 | 540 | ||
428 | tmp = getenv (constname); | 541 | tmp = getenv (constname); |
429 | if (tmp == NULL) | 542 | if (NULL == tmp) |
430 | return 0; | 543 | return 0; |
431 | def = GNUNET_strdup (tmp); | 544 | def = GNUNET_strdup (tmp); |
432 | from_line = 0; | 545 | from_line = 0; |
@@ -454,17 +567,17 @@ parse_definitions (const char *constname, int force) | |||
454 | { | 567 | { |
455 | errno = 0; | 568 | errno = 0; |
456 | from_line = strtol (start, &t, 10); | 569 | from_line = strtol (start, &t, 10); |
457 | if (errno != 0 || from_line < 0) | 570 | if ( (0 != errno) || (from_line < 0) ) |
458 | { | 571 | { |
459 | GNUNET_free (def); | 572 | GNUNET_free (def); |
460 | return counter; | 573 | return counter; |
461 | } | 574 | } |
462 | if (t < p && t[0] == '-') | 575 | if ( (t < p) && ('-' == t[0]) ) |
463 | { | 576 | { |
464 | errno = 0; | 577 | errno = 0; |
465 | start = t + 1; | 578 | start = t + 1; |
466 | to_line = strtol (start, &t, 10); | 579 | to_line = strtol (start, &t, 10); |
467 | if (errno != 0 || to_line < 0 || t != p) | 580 | if ( (0 != errno) || (to_line < 0) || (t != p) ) |
468 | { | 581 | { |
469 | GNUNET_free (def); | 582 | GNUNET_free (def); |
470 | return counter; | 583 | return counter; |
@@ -481,7 +594,7 @@ parse_definitions (const char *constname, int force) | |||
481 | break; | 594 | break; |
482 | } | 595 | } |
483 | start = p + 1; | 596 | start = p + 1; |
484 | state += 1; | 597 | state++; |
485 | break; | 598 | break; |
486 | case '\0': /* found EOL */ | 599 | case '\0': /* found EOL */ |
487 | keep_looking = 0; | 600 | keep_looking = 0; |
@@ -493,15 +606,15 @@ parse_definitions (const char *constname, int force) | |||
493 | p[0] = '\0'; | 606 | p[0] = '\0'; |
494 | state = 0; | 607 | state = 0; |
495 | level = get_type ((const char *) start); | 608 | level = get_type ((const char *) start); |
496 | if (level == GNUNET_ERROR_TYPE_INVALID || | 609 | if ( (GNUNET_ERROR_TYPE_INVALID == level) || |
497 | level == GNUNET_ERROR_TYPE_UNSPECIFIED || | 610 | (GNUNET_ERROR_TYPE_UNSPECIFIED == level) || |
498 | 0 != add_definition (comp, file, function, from_line, to_line, | 611 | (0 != add_definition (comp, file, function, from_line, to_line, |
499 | level, force)) | 612 | level, force)) ) |
500 | { | 613 | { |
501 | GNUNET_free (def); | 614 | GNUNET_free (def); |
502 | return counter; | 615 | return counter; |
503 | } | 616 | } |
504 | counter += 1; | 617 | counter++; |
505 | start = p + 1; | 618 | start = p + 1; |
506 | break; | 619 | break; |
507 | default: | 620 | default: |
@@ -515,16 +628,17 @@ parse_definitions (const char *constname, int force) | |||
515 | return counter; | 628 | return counter; |
516 | } | 629 | } |
517 | 630 | ||
631 | |||
518 | /** | 632 | /** |
519 | * Utility function - parses GNUNET_LOG and GNUNET_FORCE_LOG. | 633 | * Utility function - parses GNUNET_LOG and GNUNET_FORCE_LOG. |
520 | */ | 634 | */ |
521 | static void | 635 | static void |
522 | parse_all_definitions () | 636 | parse_all_definitions () |
523 | { | 637 | { |
524 | if (gnunet_log_parsed == GNUNET_NO) | 638 | if (GNUNET_NO == gnunet_log_parsed) |
525 | parse_definitions ("GNUNET_LOG", 0); | 639 | parse_definitions ("GNUNET_LOG", 0); |
526 | gnunet_log_parsed = GNUNET_YES; | 640 | gnunet_log_parsed = GNUNET_YES; |
527 | if (gnunet_force_log_parsed == GNUNET_NO) | 641 | if (GNUNET_NO == gnunet_force_log_parsed) |
528 | gnunet_force_log_present = | 642 | gnunet_force_log_present = |
529 | parse_definitions ("GNUNET_FORCE_LOG", 1) > 0 ? GNUNET_YES : GNUNET_NO; | 643 | parse_definitions ("GNUNET_FORCE_LOG", 1) > 0 ? GNUNET_YES : GNUNET_NO; |
530 | gnunet_force_log_parsed = GNUNET_YES; | 644 | gnunet_force_log_parsed = GNUNET_YES; |
@@ -543,11 +657,9 @@ parse_all_definitions () | |||
543 | int | 657 | int |
544 | GNUNET_log_setup (const char *comp, const char *loglevel, const char *logfile) | 658 | GNUNET_log_setup (const char *comp, const char *loglevel, const char *logfile) |
545 | { | 659 | { |
546 | FILE *altlog; | 660 | const char *env_logfile; |
547 | int dirwarn; | 661 | const struct tm *tm; |
548 | char *fn; | 662 | time_t t; |
549 | const char *env_logfile = NULL; | ||
550 | int altlog_fd; | ||
551 | 663 | ||
552 | min_level = get_type (loglevel); | 664 | min_level = get_type (loglevel); |
553 | #if !defined(GNUNET_CULL_LOGGING) | 665 | #if !defined(GNUNET_CULL_LOGGING) |
@@ -562,61 +674,20 @@ GNUNET_log_setup (const char *comp, const char *loglevel, const char *logfile) | |||
562 | component_nopid = GNUNET_strdup (comp); | 674 | component_nopid = GNUNET_strdup (comp); |
563 | 675 | ||
564 | env_logfile = getenv ("GNUNET_FORCE_LOGFILE"); | 676 | env_logfile = getenv ("GNUNET_FORCE_LOGFILE"); |
565 | if ((env_logfile != NULL) && (strlen (env_logfile) > 0)) | 677 | if ((NULL != env_logfile) && (strlen (env_logfile) > 0)) |
566 | logfile = env_logfile; | 678 | logfile = env_logfile; |
567 | 679 | if (NULL == logfile) | |
568 | if (logfile == NULL) | ||
569 | return GNUNET_OK; | 680 | return GNUNET_OK; |
570 | fn = GNUNET_STRINGS_filename_expand (logfile); | 681 | GNUNET_free_non_null (log_file_name); |
571 | if (NULL == fn) | 682 | log_file_name = GNUNET_STRINGS_filename_expand (logfile); |
572 | return GNUNET_SYSERR; | 683 | if (NULL == log_file_name) |
573 | dirwarn = (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn)); | ||
574 | #if WINDOWS | ||
575 | altlog_fd = OPEN (fn, O_APPEND | | ||
576 | O_BINARY | | ||
577 | O_WRONLY | O_CREAT, | ||
578 | _S_IREAD | _S_IWRITE); | ||
579 | #else | ||
580 | altlog_fd = OPEN (fn, O_APPEND | | ||
581 | O_WRONLY | O_CREAT, | ||
582 | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); | ||
583 | #endif | ||
584 | if (altlog_fd != -1) | ||
585 | { | ||
586 | int dup_return; | ||
587 | if (GNUNET_stderr != NULL) | ||
588 | fclose (GNUNET_stderr); | ||
589 | dup_return = dup2 (altlog_fd, 2); | ||
590 | (void) close (altlog_fd); | ||
591 | if (dup_return != -1) | ||
592 | { | ||
593 | altlog = fdopen (2, "ab"); | ||
594 | if (altlog == NULL) | ||
595 | { | ||
596 | (void) close (2); | ||
597 | altlog_fd = -1; | ||
598 | } | ||
599 | } | ||
600 | else | ||
601 | { | ||
602 | altlog_fd = -1; | ||
603 | } | ||
604 | } | ||
605 | if (altlog_fd == -1) | ||
606 | { | ||
607 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", fn); | ||
608 | if (dirwarn) | ||
609 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
610 | _("Failed to create or access directory for log file `%s'\n"), | ||
611 | fn); | ||
612 | GNUNET_free (fn); | ||
613 | return GNUNET_SYSERR; | 684 | return GNUNET_SYSERR; |
614 | } | 685 | t = time (NULL); |
615 | GNUNET_free (fn); | 686 | tm = gmtime (&t); |
616 | GNUNET_stderr = altlog; | 687 | return setup_log_file (tm); |
617 | return GNUNET_OK; | ||
618 | } | 688 | } |
619 | 689 | ||
690 | |||
620 | /** | 691 | /** |
621 | * Add a custom logger. | 692 | * Add a custom logger. |
622 | * | 693 | * |
@@ -635,6 +706,7 @@ GNUNET_logger_add (GNUNET_Logger logger, void *logger_cls) | |||
635 | loggers = entry; | 706 | loggers = entry; |
636 | } | 707 | } |
637 | 708 | ||
709 | |||
638 | /** | 710 | /** |
639 | * Remove a custom logger. | 711 | * Remove a custom logger. |
640 | * | 712 | * |
@@ -678,7 +750,7 @@ output_message (enum GNUNET_ErrorType kind, const char *comp, | |||
678 | { | 750 | { |
679 | struct CustomLogger *pos; | 751 | struct CustomLogger *pos; |
680 | 752 | ||
681 | if (GNUNET_stderr != NULL) | 753 | if (NULL != GNUNET_stderr) |
682 | { | 754 | { |
683 | FPRINTF (GNUNET_stderr, "%s %s %s %s", datestr, comp, | 755 | FPRINTF (GNUNET_stderr, "%s %s %s %s", datestr, comp, |
684 | GNUNET_error_type_to_string (kind), msg); | 756 | GNUNET_error_type_to_string (kind), msg); |
@@ -742,17 +814,19 @@ flush_bulk (const char *datestr) | |||
742 | void | 814 | void |
743 | GNUNET_log_skip (unsigned int n, int check_reset) | 815 | GNUNET_log_skip (unsigned int n, int check_reset) |
744 | { | 816 | { |
745 | if (n == 0) | 817 | int ok; |
746 | { | ||
747 | int ok; | ||
748 | 818 | ||
819 | if (0 == n) | ||
820 | { | ||
749 | ok = (0 == skip_log); | 821 | ok = (0 == skip_log); |
750 | skip_log = 0; | 822 | skip_log = 0; |
751 | if (check_reset) | 823 | if (check_reset) |
752 | GNUNET_assert (ok); | 824 | GNUNET_assert (ok); |
753 | } | 825 | } |
754 | else | 826 | else |
827 | { | ||
755 | skip_log += n; | 828 | skip_log += n; |
829 | } | ||
756 | } | 830 | } |
757 | 831 | ||
758 | 832 | ||
@@ -770,8 +844,6 @@ mylog (enum GNUNET_ErrorType kind, const char *comp, const char *message, | |||
770 | { | 844 | { |
771 | char date[DATE_STR_SIZE]; | 845 | char date[DATE_STR_SIZE]; |
772 | char date2[DATE_STR_SIZE]; | 846 | char date2[DATE_STR_SIZE]; |
773 | time_t timetmp; | ||
774 | struct timeval timeofday; | ||
775 | struct tm *tmptr; | 847 | struct tm *tmptr; |
776 | size_t size; | 848 | size_t size; |
777 | va_list vacp; | 849 | va_list vacp; |
@@ -780,32 +852,45 @@ mylog (enum GNUNET_ErrorType kind, const char *comp, const char *message, | |||
780 | size = VSNPRINTF (NULL, 0, message, vacp) + 1; | 852 | size = VSNPRINTF (NULL, 0, message, vacp) + 1; |
781 | GNUNET_assert (0 != size); | 853 | GNUNET_assert (0 != size); |
782 | va_end (vacp); | 854 | va_end (vacp); |
855 | memset (date, 0, DATE_STR_SIZE); | ||
783 | { | 856 | { |
784 | char buf[size]; | 857 | char buf[size]; |
858 | #ifdef WINDOWS | ||
859 | LARGE_INTEGER pc; | ||
860 | time_t timetmp; | ||
785 | 861 | ||
786 | VSNPRINTF (buf, size, message, va); | ||
787 | time (&timetmp); | 862 | time (&timetmp); |
788 | memset (date, 0, DATE_STR_SIZE); | ||
789 | tmptr = localtime (&timetmp); | 863 | tmptr = localtime (&timetmp); |
790 | gettimeofday (&timeofday, NULL); | 864 | pc.QuadPart = 0; |
791 | if (NULL != tmptr) | 865 | QueryPerformanceCounter (&pc); |
866 | if (NULL == tmptr) | ||
867 | { | ||
868 | strcpy (date, "localtime error"); | ||
869 | } | ||
870 | else | ||
792 | { | 871 | { |
793 | #ifdef WINDOWS | ||
794 | LARGE_INTEGER pc; | ||
795 | |||
796 | pc.QuadPart = 0; | ||
797 | QueryPerformanceCounter (&pc); | ||
798 | strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%020llu", tmptr); | 872 | strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%020llu", tmptr); |
799 | snprintf (date, sizeof (date), date2, | 873 | snprintf (date, sizeof (date), date2, |
800 | (long long) (pc.QuadPart / | 874 | (long long) (pc.QuadPart / |
801 | (performance_frequency.QuadPart / 1000))); | 875 | (performance_frequency.QuadPart / 1000))); |
876 | } | ||
802 | #else | 877 | #else |
878 | struct timeval timeofday; | ||
879 | |||
880 | gettimeofday (&timeofday, NULL); | ||
881 | tmptr = localtime (&timeofday.tv_sec); | ||
882 | if (NULL == tmptr) | ||
883 | { | ||
884 | strcpy (date, "localtime error"); | ||
885 | } | ||
886 | else | ||
887 | { | ||
803 | strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%06u", tmptr); | 888 | strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%06u", tmptr); |
804 | snprintf (date, sizeof (date), date2, timeofday.tv_usec); | 889 | snprintf (date, sizeof (date), date2, timeofday.tv_usec); |
805 | #endif | ||
806 | } | 890 | } |
807 | else | 891 | #endif |
808 | strcpy (date, "localtime error"); | 892 | VSNPRINTF (buf, size, message, va); |
893 | (void) setup_log_file (tmptr); | ||
809 | if ((0 != (kind & GNUNET_ERROR_TYPE_BULK)) && | 894 | if ((0 != (kind & GNUNET_ERROR_TYPE_BULK)) && |
810 | (last_bulk_time.abs_value != 0) && | 895 | (last_bulk_time.abs_value != 0) && |
811 | (0 == strncmp (buf, last_bulk, sizeof (last_bulk)))) | 896 | (0 == strncmp (buf, last_bulk, sizeof (last_bulk)))) |
@@ -912,6 +997,7 @@ GNUNET_h2s (const struct GNUNET_HashCode * hc) | |||
912 | return (const char *) ret.encoding; | 997 | return (const char *) ret.encoding; |
913 | } | 998 | } |
914 | 999 | ||
1000 | |||
915 | /** | 1001 | /** |
916 | * Convert a hash to a string (for printing debug messages). | 1002 | * Convert a hash to a string (for printing debug messages). |
917 | * This is one of the very few calls in the entire API that is | 1003 | * This is one of the very few calls in the entire API that is |
@@ -930,6 +1016,7 @@ GNUNET_h2s_full (const struct GNUNET_HashCode * hc) | |||
930 | return (const char *) ret.encoding; | 1016 | return (const char *) ret.encoding; |
931 | } | 1017 | } |
932 | 1018 | ||
1019 | |||
933 | /** | 1020 | /** |
934 | * Convert a peer identity to a string (for printing debug messages). | 1021 | * Convert a peer identity to a string (for printing debug messages). |
935 | * This is one of the very few calls in the entire API that is | 1022 | * This is one of the very few calls in the entire API that is |
@@ -949,6 +1036,7 @@ GNUNET_i2s (const struct GNUNET_PeerIdentity *pid) | |||
949 | return (const char *) ret.encoding; | 1036 | return (const char *) ret.encoding; |
950 | } | 1037 | } |
951 | 1038 | ||
1039 | |||
952 | /** | 1040 | /** |
953 | * Convert a peer identity to a string (for printing debug messages). | 1041 | * Convert a peer identity to a string (for printing debug messages). |
954 | * This is one of the very few calls in the entire API that is | 1042 | * This is one of the very few calls in the entire API that is |