aboutsummaryrefslogtreecommitdiff
path: root/src/util/common_logging.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/common_logging.c')
-rw-r--r--src/util/common_logging.c405
1 files changed, 207 insertions, 198 deletions
diff --git a/src/util/common_logging.c b/src/util/common_logging.c
index ccee8e028..fd32036d0 100644
--- a/src/util/common_logging.c
+++ b/src/util/common_logging.c
@@ -251,6 +251,7 @@ get_type (const char *log)
251 return GNUNET_ERROR_TYPE_NONE; 251 return GNUNET_ERROR_TYPE_NONE;
252 return GNUNET_ERROR_TYPE_INVALID; 252 return GNUNET_ERROR_TYPE_INVALID;
253} 253}
254
254#if !defined(GNUNET_CULL_LOGGING) 255#if !defined(GNUNET_CULL_LOGGING)
255/** 256/**
256 * Utility function - reallocates logdefs array to be twice as large. 257 * Utility function - reallocates logdefs array to be twice as large.
@@ -258,8 +259,8 @@ get_type (const char *log)
258static void 259static void
259resize_logdefs () 260resize_logdefs ()
260{ 261{
261 logdefs_size = (logdefs_size + 1) * 2; 262 logdefs_size = (logdefs_size + 1) * 2;
262 logdefs = GNUNET_realloc (logdefs, logdefs_size * sizeof (struct LogDef)); 263 logdefs = GNUNET_realloc (logdefs, logdefs_size * sizeof (struct LogDef));
263} 264}
264 265
265/** 266/**
@@ -274,7 +275,8 @@ resize_logdefs ()
274 * @param force see struct LogDef 275 * @param force see struct LogDef
275 */ 276 */
276static void 277static void
277add_definition (char *component, char *file, char *function, int from_line, int to_line, int level, int force) 278add_definition (char *component, char *file, char *function, int from_line,
279 int to_line, int level, int force)
278{ 280{
279 if (logdefs_size == logdefs_len) 281 if (logdefs_size == logdefs_len)
280 resize_logdefs (); 282 resize_logdefs ();
@@ -283,10 +285,10 @@ add_definition (char *component, char *file, char *function, int from_line, int
283 if (strlen (component) > 0 && component[0] != '*') 285 if (strlen (component) > 0 && component[0] != '*')
284 n.component = strdup (component); 286 n.component = strdup (component);
285 if (strlen (file) > 0 && file[0] != '*') 287 if (strlen (file) > 0 && file[0] != '*')
286 { 288 {
287 n.file = strdup (file); 289 n.file = strdup (file);
288 n.strlen_file = strlen (file); 290 n.strlen_file = strlen (file);
289 } 291 }
290 if (strlen (function) > 0 && function[0] != '*') 292 if (strlen (function) > 0 && function[0] != '*')
291 n.function = strdup (function); 293 n.function = strdup (function);
292 n.from_line = from_line; 294 n.from_line = from_line;
@@ -310,8 +312,9 @@ add_definition (char *component, char *file, char *function, int from_line, int
310 * @param line line at which the call is made, usually __LINE__ 312 * @param line line at which the call is made, usually __LINE__
311 * @return 0 to disallow the call, 1 to allow it 313 * @return 0 to disallow the call, 1 to allow it
312 */ 314 */
313int 315int
314GNUNET_get_log_call_status (int caller_level, const char *comp, const char *file, const char *function, int line) 316GNUNET_get_log_call_status (int caller_level, const char *comp,
317 const char *file, const char *function, int line)
315{ 318{
316 struct LogDef *ld; 319 struct LogDef *ld;
317 int i; 320 int i;
@@ -332,21 +335,20 @@ GNUNET_get_log_call_status (int caller_level, const char *comp, const char *file
332 force_only = min_level >= 0; 335 force_only = min_level >= 0;
333 strlen_file = strlen (file); 336 strlen_file = strlen (file);
334 for (i = 0; i < logdefs_len; i++) 337 for (i = 0; i < logdefs_len; i++)
335 {
336 ld = &logdefs[i];
337 if ((!force_only || ld->force) &&
338 (line >= ld->from_line && line <= ld->to_line) &&
339 (ld->component == NULL || strcmp (comp, ld->component) == 0) &&
340 (ld->file == NULL ||
341 (ld->strlen_file <= strlen_file &&
342 strcmp (&file[strlen_file - ld->strlen_file], ld->file) == 0)) &&
343 (ld->function == NULL || strcmp (function, ld->function) == 0)
344 )
345 { 338 {
346 /* We're finished */ 339 ld = &logdefs[i];
347 return caller_level <= ld->level; 340 if ((!force_only || ld->force) &&
341 (line >= ld->from_line && line <= ld->to_line) &&
342 (ld->component == NULL || strcmp (comp, ld->component) == 0) &&
343 (ld->file == NULL ||
344 (ld->strlen_file <= strlen_file &&
345 strcmp (&file[strlen_file - ld->strlen_file], ld->file) == 0)) &&
346 (ld->function == NULL || strcmp (function, ld->function) == 0))
347 {
348 /* We're finished */
349 return caller_level <= ld->level;
350 }
348 } 351 }
349 }
350 /* No matches - use global level, if defined */ 352 /* No matches - use global level, if defined */
351 if (min_level >= 0) 353 if (min_level >= 0)
352 return caller_level <= min_level; 354 return caller_level <= min_level;
@@ -404,83 +406,85 @@ parse_definitions (const char *constname, int force)
404 from_line = 0; 406 from_line = 0;
405 to_line = INT_MAX; 407 to_line = INT_MAX;
406 for (p = def, state = 0, start = def; keep_looking; p++) 408 for (p = def, state = 0, start = def; keep_looking; p++)
407 {
408 switch (p[0])
409 { 409 {
410 case ';': /* found a field separator */ 410 switch (p[0])
411 p[0] = '\0'; 411 {
412 switch (state) 412 case ';': /* found a field separator */
413 { 413 p[0] = '\0';
414 case 0: /* within a component name */ 414 switch (state)
415 comp = start; 415 {
416 break; 416 case 0: /* within a component name */
417 case 1: /* within a file name */ 417 comp = start;
418 file = start; 418 break;
419 break; 419 case 1: /* within a file name */
420 case 2: /* within a function name */ 420 file = start;
421 /* after a file name there must be a function name */ 421 break;
422 function = start; 422 case 2: /* within a function name */
423 break; 423 /* after a file name there must be a function name */
424 case 3: /* within a from-to line range */ 424 function = start;
425 if (strlen (start) > 0) 425 break;
426 { 426 case 3: /* within a from-to line range */
427 errno = 0; 427 if (strlen (start) > 0)
428 from_line = strtol (start, &t, 10); 428 {
429 if (errno != 0 || from_line < 0) 429 errno = 0;
430 { 430 from_line = strtol (start, &t, 10);
431 free (def); 431 if (errno != 0 || from_line < 0)
432 return counter; 432 {
433 } 433 free (def);
434 if (t < p && t[0] == '-') 434 return counter;
435 { 435 }
436 errno = 0; 436 if (t < p && t[0] == '-')
437 start = t + 1; 437 {
438 to_line = strtol (start, &t, 10); 438 errno = 0;
439 if (errno != 0 || to_line < 0 || t != p) 439 start = t + 1;
440 { 440 to_line = strtol (start, &t, 10);
441 free (def); 441 if (errno != 0 || to_line < 0 || t != p)
442 return counter; 442 {
443 } 443 free (def);
444 } 444 return counter;
445 else /* one number means "match this line only" */ 445 }
446 to_line = from_line; 446 }
447 } 447 else /* one number means "match this line only" */
448 else /* default to 0-max */ 448 to_line = from_line;
449 { 449 }
450 from_line = 0; 450 else /* default to 0-max */
451 to_line = INT_MAX; 451 {
452 } 452 from_line = 0;
453 break; 453 to_line = INT_MAX;
454 } 454 }
455 start = p + 1; 455 break;
456 state += 1; 456 }
457 break; 457 start = p + 1;
458 case '\0': /* found EOL */ 458 state += 1;
459 keep_looking = 0; 459 break;
460 /* fall through to '/' */ 460 case '\0': /* found EOL */
461 case '/': /* found a definition separator */ 461 keep_looking = 0;
462 switch (state) 462 /* fall through to '/' */
463 { 463 case '/': /* found a definition separator */
464 case 4: /* within a log level */ 464 switch (state)
465 p[0] = '\0'; 465 {
466 state = 0; 466 case 4: /* within a log level */
467 level = get_type ((const char *) start); 467 p[0] = '\0';
468 if (level == GNUNET_ERROR_TYPE_INVALID || level == GNUNET_ERROR_TYPE_UNSPECIFIED) 468 state = 0;
469 { 469 level = get_type ((const char *) start);
470 free (def); 470 if (level == GNUNET_ERROR_TYPE_INVALID
471 return counter; 471 || level == GNUNET_ERROR_TYPE_UNSPECIFIED)
472 } 472 {
473 add_definition (comp, file, function, from_line, to_line, level, force); 473 free (def);
474 counter += 1; 474 return counter;
475 start = p + 1; 475 }
476 break; 476 add_definition (comp, file, function, from_line, to_line, level,
477 default: 477 force);
478 break; 478 counter += 1;
479 } 479 start = p + 1;
480 default: 480 break;
481 break; 481 default:
482 break;
483 }
484 default:
485 break;
486 }
482 } 487 }
483 }
484 free (def); 488 free (def);
485 return counter; 489 return counter;
486} 490}
@@ -495,7 +499,8 @@ parse_all_definitions ()
495 parse_definitions ("GNUNET_LOG", 0); 499 parse_definitions ("GNUNET_LOG", 0);
496 gnunet_log_parsed = GNUNET_YES; 500 gnunet_log_parsed = GNUNET_YES;
497 if (gnunet_force_log_parsed == GNUNET_NO) 501 if (gnunet_force_log_parsed == GNUNET_NO)
498 gnunet_force_log_present = parse_definitions ("GNUNET_FORCE_LOG", 1) > 0 ? GNUNET_YES : GNUNET_NO; 502 gnunet_force_log_present =
503 parse_definitions ("GNUNET_FORCE_LOG", 1) > 0 ? GNUNET_YES : GNUNET_NO;
499 gnunet_force_log_parsed = GNUNET_YES; 504 gnunet_force_log_parsed = GNUNET_YES;
500} 505}
501#endif 506#endif
@@ -539,15 +544,16 @@ GNUNET_log_setup (const char *comp, const char *loglevel, const char *logfile)
539 dirwarn = (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn)); 544 dirwarn = (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn));
540 altlog = FOPEN (fn, "a"); 545 altlog = FOPEN (fn, "a");
541 if (altlog == NULL) 546 if (altlog == NULL)
542 { 547 {
543 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "fopen", fn); 548 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "fopen", fn);
544 if (dirwarn) 549 if (dirwarn)
545 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 550 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
546 _("Failed to create or access directory for log file `%s'\n"), 551 _
547 fn); 552 ("Failed to create or access directory for log file `%s'\n"),
548 GNUNET_free (fn); 553 fn);
549 return GNUNET_SYSERR; 554 GNUNET_free (fn);
550 } 555 return GNUNET_SYSERR;
556 }
551 GNUNET_free (fn); 557 GNUNET_free (fn);
552 if (GNUNET_stderr != NULL) 558 if (GNUNET_stderr != NULL)
553 fclose (GNUNET_stderr); 559 fclose (GNUNET_stderr);
@@ -588,11 +594,11 @@ GNUNET_logger_remove (GNUNET_Logger logger, void *logger_cls)
588 prev = NULL; 594 prev = NULL;
589 pos = loggers; 595 pos = loggers;
590 while ((pos != NULL) && 596 while ((pos != NULL) &&
591 ((pos->logger != logger) || (pos->logger_cls != logger_cls))) 597 ((pos->logger != logger) || (pos->logger_cls != logger_cls)))
592 { 598 {
593 prev = pos; 599 prev = pos;
594 pos = pos->next; 600 pos = pos->next;
595 } 601 }
596 GNUNET_assert (pos != NULL); 602 GNUNET_assert (pos != NULL);
597 if (prev == NULL) 603 if (prev == NULL)
598 loggers = pos->next; 604 loggers = pos->next;
@@ -612,22 +618,22 @@ GNUNET_logger_remove (GNUNET_Logger logger, void *logger_cls)
612 */ 618 */
613static void 619static void
614output_message (enum GNUNET_ErrorType kind, const char *comp, 620output_message (enum GNUNET_ErrorType kind, const char *comp,
615 const char *datestr, const char *msg) 621 const char *datestr, const char *msg)
616{ 622{
617 struct CustomLogger *pos; 623 struct CustomLogger *pos;
618 624
619 if (GNUNET_stderr != NULL) 625 if (GNUNET_stderr != NULL)
620 { 626 {
621 fprintf (GNUNET_stderr, "%s %s %s %s", datestr, comp, 627 fprintf (GNUNET_stderr, "%s %s %s %s", datestr, comp,
622 GNUNET_error_type_to_string (kind), msg); 628 GNUNET_error_type_to_string (kind), msg);
623 fflush (GNUNET_stderr); 629 fflush (GNUNET_stderr);
624 } 630 }
625 pos = loggers; 631 pos = loggers;
626 while (pos != NULL) 632 while (pos != NULL)
627 { 633 {
628 pos->logger (pos->logger_cls, kind, comp, datestr, msg); 634 pos->logger (pos->logger_cls, kind, comp, datestr, msg);
629 pos = pos->next; 635 pos = pos->next;
630 } 636 }
631} 637}
632 638
633 639
@@ -653,15 +659,16 @@ flush_bulk (const char *datestr)
653 else if (last != last_bulk) 659 else if (last != last_bulk)
654 last--; 660 last--;
655 if (last[0] == '\n') 661 if (last[0] == '\n')
656 { 662 {
657 rev = 1; 663 rev = 1;
658 last[0] = '\0'; 664 last[0] = '\0';
659 } 665 }
660 ft = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration 666 ft =
661 (last_bulk_time)); 667 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration
668 (last_bulk_time));
662 snprintf (msg, sizeof (msg), 669 snprintf (msg, sizeof (msg),
663 _("Message `%.*s' repeated %u times in the last %s\n"), 670 _("Message `%.*s' repeated %u times in the last %s\n"),
664 BULK_TRACK_SIZE, last_bulk, last_bulk_repeat, ft); 671 BULK_TRACK_SIZE, last_bulk, last_bulk_repeat, ft);
665 GNUNET_free (ft); 672 GNUNET_free (ft);
666 if (rev == 1) 673 if (rev == 1)
667 last[0] = '\n'; 674 last[0] = '\n';
@@ -681,14 +688,14 @@ void
681GNUNET_log_skip (unsigned int n, int check_reset) 688GNUNET_log_skip (unsigned int n, int check_reset)
682{ 689{
683 if (n == 0) 690 if (n == 0)
684 { 691 {
685 int ok; 692 int ok;
686 693
687 ok = (0 == skip_log); 694 ok = (0 == skip_log);
688 skip_log = 0; 695 skip_log = 0;
689 if (check_reset) 696 if (check_reset)
690 GNUNET_assert (ok); 697 GNUNET_assert (ok);
691 } 698 }
692 else 699 else
693 skip_log += n; 700 skip_log += n;
694} 701}
@@ -720,40 +727,42 @@ mylog (enum GNUNET_ErrorType kind, const char *comp, const char *message,
720 va_end (vacp); 727 va_end (vacp);
721 buf = malloc (size); 728 buf = malloc (size);
722 if (buf == NULL) 729 if (buf == NULL)
723 return; /* oops */ 730 return; /* oops */
724 VSNPRINTF (buf, size, message, va); 731 VSNPRINTF (buf, size, message, va);
725 time (&timetmp); 732 time (&timetmp);
726 memset (date, 0, DATE_STR_SIZE); 733 memset (date, 0, DATE_STR_SIZE);
727 tmptr = localtime (&timetmp); 734 tmptr = localtime (&timetmp);
728 gettimeofday (&timeofday, NULL); 735 gettimeofday (&timeofday, NULL);
729 if (NULL != tmptr) 736 if (NULL != tmptr)
730 { 737 {
731#ifdef WINDOWS 738#ifdef WINDOWS
732 LARGE_INTEGER pc; 739 LARGE_INTEGER pc;
733 740
734 pc.QuadPart = 0; 741 pc.QuadPart = 0;
735 QueryPerformanceCounter (&pc); 742 QueryPerformanceCounter (&pc);
736 strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%020llu", tmptr); 743 strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%020llu", tmptr);
737 snprintf (date, sizeof (date), date2, 744 snprintf (date, sizeof (date), date2,
738 (long long) (pc.QuadPart / 745 (long long) (pc.QuadPart /
739 (performance_frequency.QuadPart / 1000))); 746 (performance_frequency.QuadPart / 1000)));
740#else 747#else
741 strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%06u", tmptr); 748 strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%06u", tmptr);
742 snprintf (date, sizeof (date), date2, timeofday.tv_usec); 749 snprintf (date, sizeof (date), date2, timeofday.tv_usec);
743#endif 750#endif
744 } 751 }
745 else 752 else
746 strcpy (date, "localtime error"); 753 strcpy (date, "localtime error");
747 if ((0 != (kind & GNUNET_ERROR_TYPE_BULK)) && (last_bulk_time.abs_value != 0) 754 if ((0 != (kind & GNUNET_ERROR_TYPE_BULK))
755 && (last_bulk_time.abs_value != 0)
748 && (0 == strncmp (buf, last_bulk, sizeof (last_bulk)))) 756 && (0 == strncmp (buf, last_bulk, sizeof (last_bulk))))
749 { 757 {
750 last_bulk_repeat++; 758 last_bulk_repeat++;
751 if ((GNUNET_TIME_absolute_get_duration (last_bulk_time).rel_value > 759 if ((GNUNET_TIME_absolute_get_duration (last_bulk_time).rel_value >
752 BULK_DELAY_THRESHOLD) || (last_bulk_repeat > BULK_REPEAT_THRESHOLD)) 760 BULK_DELAY_THRESHOLD)
753 flush_bulk (date); 761 || (last_bulk_repeat > BULK_REPEAT_THRESHOLD))
754 free (buf); 762 flush_bulk (date);
755 return; 763 free (buf);
756 } 764 return;
765 }
757 flush_bulk (date); 766 flush_bulk (date);
758 strncpy (last_bulk, buf, sizeof (last_bulk)); 767 strncpy (last_bulk, buf, sizeof (last_bulk));
759 last_bulk_repeat = 0; 768 last_bulk_repeat = 0;
@@ -794,7 +803,7 @@ GNUNET_log_nocheck (enum GNUNET_ErrorType kind, const char *message, ...)
794 */ 803 */
795void 804void
796GNUNET_log_from_nocheck (enum GNUNET_ErrorType kind, const char *comp, 805GNUNET_log_from_nocheck (enum GNUNET_ErrorType kind, const char *comp,
797 const char *message, ...) 806 const char *message, ...)
798{ 807{
799 va_list va; 808 va_list va;
800 char comp_w_pid[128]; 809 char comp_w_pid[128];
@@ -912,44 +921,44 @@ GNUNET_a2s (const struct sockaddr *addr, socklen_t addrlen)
912 if (addr == NULL) 921 if (addr == NULL)
913 return _("unknown address"); 922 return _("unknown address");
914 switch (addr->sa_family) 923 switch (addr->sa_family)
915 { 924 {
916 case AF_INET: 925 case AF_INET:
917 if (addrlen != sizeof (struct sockaddr_in)) 926 if (addrlen != sizeof (struct sockaddr_in))
918 return "<invalid v4 address>"; 927 return "<invalid v4 address>";
919 v4 = (const struct sockaddr_in *) addr; 928 v4 = (const struct sockaddr_in *) addr;
920 inet_ntop (AF_INET, &v4->sin_addr, buf, INET_ADDRSTRLEN); 929 inet_ntop (AF_INET, &v4->sin_addr, buf, INET_ADDRSTRLEN);
921 if (0 == ntohs (v4->sin_port)) 930 if (0 == ntohs (v4->sin_port))
931 return buf;
932 strcat (buf, ":");
933 GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v4->sin_port));
934 strcat (buf, b2);
935 return buf;
936 case AF_INET6:
937 if (addrlen != sizeof (struct sockaddr_in6))
938 return "<invalid v4 address>";
939 v6 = (const struct sockaddr_in6 *) addr;
940 buf[0] = '[';
941 inet_ntop (AF_INET6, &v6->sin6_addr, &buf[1], INET6_ADDRSTRLEN);
942 if (0 == ntohs (v6->sin6_port))
943 return &buf[1];
944 strcat (buf, "]:");
945 GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v6->sin6_port));
946 strcat (buf, b2);
922 return buf; 947 return buf;
923 strcat (buf, ":"); 948 case AF_UNIX:
924 GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v4->sin_port)); 949 if (addrlen <= sizeof (sa_family_t))
925 strcat (buf, b2); 950 return "<unbound UNIX client>";
926 return buf; 951 un = (const struct sockaddr_un *) addr;
927 case AF_INET6: 952 off = 0;
928 if (addrlen != sizeof (struct sockaddr_in6)) 953 if (un->sun_path[0] == '\0')
929 return "<invalid v4 address>"; 954 off++;
930 v6 = (const struct sockaddr_in6 *) addr; 955 snprintf (buf, sizeof (buf), "%s%.*s", (off == 1) ? "@" : "",
931 buf[0] = '['; 956 (int) (addrlen - sizeof (sa_family_t) - 1 - off),
932 inet_ntop (AF_INET6, &v6->sin6_addr, &buf[1], INET6_ADDRSTRLEN); 957 &un->sun_path[off]);
933 if (0 == ntohs (v6->sin6_port)) 958 return buf;
934 return &buf[1]; 959 default:
935 strcat (buf, "]:"); 960 return _("invalid address");
936 GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v6->sin6_port)); 961 }
937 strcat (buf, b2);
938 return buf;
939 case AF_UNIX:
940 if (addrlen <= sizeof (sa_family_t))
941 return "<unbound UNIX client>";
942 un = (const struct sockaddr_un *) addr;
943 off = 0;
944 if (un->sun_path[0] == '\0')
945 off++;
946 snprintf (buf, sizeof (buf), "%s%.*s", (off == 1) ? "@" : "",
947 (int) (addrlen - sizeof (sa_family_t) - 1 - off),
948 &un->sun_path[off]);
949 return buf;
950 default:
951 return _("invalid address");
952 }
953} 962}
954 963
955 964