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.c400
1 files changed, 199 insertions, 201 deletions
diff --git a/src/util/common_logging.c b/src/util/common_logging.c
index 1d3843cfd..0b7d06bab 100644
--- a/src/util/common_logging.c
+++ b/src/util/common_logging.c
@@ -276,22 +276,21 @@ resize_logdefs ()
276 */ 276 */
277static void 277static void
278add_definition (char *component, char *file, char *function, int from_line, 278add_definition (char *component, char *file, char *function, int from_line,
279 int to_line, int level, int force) 279 int to_line, int level, int force)
280{ 280{
281 if (logdefs_size == logdefs_len) 281 if (logdefs_size == logdefs_len)
282 resize_logdefs (); 282 resize_logdefs ();
283 struct LogDef n; 283 struct LogDef n;
284
284 memset (&n, 0, sizeof (n)); 285 memset (&n, 0, sizeof (n));
285 if (strlen (component) > 0 && component[0] != '*') 286 if (strlen (component) > 0 && component[0] != '*')
286 n.component = GNUNET_strdup (component); 287 n.component = GNUNET_strdup (component);
287 if (strlen (file) > 0 && file[0] != '*') 288 if (strlen (file) > 0 && file[0] != '*')
288 { 289 {
289 n.file = GNUNET_strdup (file); 290 n.file = GNUNET_strdup (file);
290 n.strlen_file = strlen (file); 291 n.strlen_file = strlen (file);
291 } 292 }
292 if ( (NULL != function) && 293 if ((NULL != function) && (strlen (function) > 0) && (function[0] != '*'))
293 (strlen (function) > 0) &&
294 (function[0] != '*') )
295 n.function = GNUNET_strdup (function); 294 n.function = GNUNET_strdup (function);
296 n.from_line = from_line; 295 n.from_line = from_line;
297 n.to_line = to_line; 296 n.to_line = to_line;
@@ -316,7 +315,7 @@ add_definition (char *component, char *file, char *function, int from_line,
316 */ 315 */
317int 316int
318GNUNET_get_log_call_status (int caller_level, const char *comp, 317GNUNET_get_log_call_status (int caller_level, const char *comp,
319 const char *file, const char *function, int line) 318 const char *file, const char *function, int line)
320{ 319{
321 struct LogDef *ld; 320 struct LogDef *ld;
322 int i; 321 int i;
@@ -337,20 +336,22 @@ GNUNET_get_log_call_status (int caller_level, const char *comp,
337 force_only = min_level >= 0; 336 force_only = min_level >= 0;
338 strlen_file = strlen (file); 337 strlen_file = strlen (file);
339 for (i = 0; i < logdefs_len; i++) 338 for (i = 0; i < logdefs_len; i++)
339 {
340 ld = &logdefs[i];
341 if ((!force_only || ld->force) &&
342 (line >= ld->from_line && line <= ld->to_line) && (ld->component == NULL
343 || strcmp (comp,
344 ld->component)
345 == 0) &&
346 (ld->file == NULL ||
347 (ld->strlen_file <= strlen_file &&
348 strcmp (&file[strlen_file - ld->strlen_file], ld->file) == 0)) &&
349 (ld->function == NULL || strcmp (function, ld->function) == 0))
340 { 350 {
341 ld = &logdefs[i]; 351 /* We're finished */
342 if ((!force_only || ld->force) && 352 return caller_level <= ld->level;
343 (line >= ld->from_line && line <= ld->to_line) &&
344 (ld->component == NULL || strcmp (comp, ld->component) == 0) &&
345 (ld->file == NULL ||
346 (ld->strlen_file <= strlen_file &&
347 strcmp (&file[strlen_file - ld->strlen_file], ld->file) == 0)) &&
348 (ld->function == NULL || strcmp (function, ld->function) == 0))
349 {
350 /* We're finished */
351 return caller_level <= ld->level;
352 }
353 } 353 }
354 }
354 /* No matches - use global level, if defined */ 355 /* No matches - use global level, if defined */
355 if (min_level >= 0) 356 if (min_level >= 0)
356 return caller_level <= min_level; 357 return caller_level <= min_level;
@@ -400,6 +401,7 @@ parse_definitions (const char *constname, int force)
400 int from_line, to_line; 401 int from_line, to_line;
401 int counter = 0; 402 int counter = 0;
402 int keep_looking = 1; 403 int keep_looking = 1;
404
403 tmp = getenv (constname); 405 tmp = getenv (constname);
404 if (tmp == NULL) 406 if (tmp == NULL)
405 return 0; 407 return 0;
@@ -408,85 +410,84 @@ parse_definitions (const char *constname, int force)
408 from_line = 0; 410 from_line = 0;
409 to_line = INT_MAX; 411 to_line = INT_MAX;
410 for (p = def, state = 0, start = def; keep_looking; p++) 412 for (p = def, state = 0, start = def; keep_looking; p++)
413 {
414 switch (p[0])
411 { 415 {
412 switch (p[0]) 416 case ';': /* found a field separator */
413 { 417 p[0] = '\0';
414 case ';': /* found a field separator */ 418 switch (state)
415 p[0] = '\0'; 419 {
416 switch (state) 420 case 0: /* within a component name */
417 { 421 comp = start;
418 case 0: /* within a component name */ 422 break;
419 comp = start; 423 case 1: /* within a file name */
420 break; 424 file = start;
421 case 1: /* within a file name */ 425 break;
422 file = start; 426 case 2: /* within a function name */
423 break; 427 /* after a file name there must be a function name */
424 case 2: /* within a function name */ 428 function = start;
425 /* after a file name there must be a function name */ 429 break;
426 function = start; 430 case 3: /* within a from-to line range */
427 break; 431 if (strlen (start) > 0)
428 case 3: /* within a from-to line range */ 432 {
429 if (strlen (start) > 0) 433 errno = 0;
430 { 434 from_line = strtol (start, &t, 10);
431 errno = 0; 435 if (errno != 0 || from_line < 0)
432 from_line = strtol (start, &t, 10); 436 {
433 if (errno != 0 || from_line < 0) 437 free (def);
434 { 438 return counter;
435 free (def); 439 }
436 return counter; 440 if (t < p && t[0] == '-')
437 } 441 {
438 if (t < p && t[0] == '-') 442 errno = 0;
439 { 443 start = t + 1;
440 errno = 0; 444 to_line = strtol (start, &t, 10);
441 start = t + 1; 445 if (errno != 0 || to_line < 0 || t != p)
442 to_line = strtol (start, &t, 10); 446 {
443 if (errno != 0 || to_line < 0 || t != p) 447 free (def);
444 { 448 return counter;
445 free (def); 449 }
446 return counter; 450 }
447 } 451 else /* one number means "match this line only" */
448 } 452 to_line = from_line;
449 else /* one number means "match this line only" */ 453 }
450 to_line = from_line; 454 else /* default to 0-max */
451 } 455 {
452 else /* default to 0-max */ 456 from_line = 0;
453 { 457 to_line = INT_MAX;
454 from_line = 0; 458 }
455 to_line = INT_MAX; 459 break;
456 } 460 }
457 break; 461 start = p + 1;
458 } 462 state += 1;
459 start = p + 1; 463 break;
460 state += 1; 464 case '\0': /* found EOL */
461 break; 465 keep_looking = 0;
462 case '\0': /* found EOL */ 466 /* fall through to '/' */
463 keep_looking = 0; 467 case '/': /* found a definition separator */
464 /* fall through to '/' */ 468 switch (state)
465 case '/': /* found a definition separator */ 469 {
466 switch (state) 470 case 4: /* within a log level */
467 { 471 p[0] = '\0';
468 case 4: /* within a log level */ 472 state = 0;
469 p[0] = '\0'; 473 level = get_type ((const char *) start);
470 state = 0; 474 if (level == GNUNET_ERROR_TYPE_INVALID ||
471 level = get_type ((const char *) start); 475 level == GNUNET_ERROR_TYPE_UNSPECIFIED)
472 if (level == GNUNET_ERROR_TYPE_INVALID 476 {
473 || level == GNUNET_ERROR_TYPE_UNSPECIFIED) 477 free (def);
474 { 478 return counter;
475 free (def); 479 }
476 return counter; 480 add_definition (comp, file, function, from_line, to_line, level, force);
477 } 481 counter += 1;
478 add_definition (comp, file, function, from_line, to_line, level, 482 start = p + 1;
479 force); 483 break;
480 counter += 1; 484 default:
481 start = p + 1; 485 break;
482 break; 486 }
483 default: 487 default:
484 break; 488 break;
485 }
486 default:
487 break;
488 }
489 } 489 }
490 }
490 free (def); 491 free (def);
491 return counter; 492 return counter;
492} 493}
@@ -502,7 +503,7 @@ parse_all_definitions ()
502 gnunet_log_parsed = GNUNET_YES; 503 gnunet_log_parsed = GNUNET_YES;
503 if (gnunet_force_log_parsed == GNUNET_NO) 504 if (gnunet_force_log_parsed == GNUNET_NO)
504 gnunet_force_log_present = 505 gnunet_force_log_present =
505 parse_definitions ("GNUNET_FORCE_LOG", 1) > 0 ? GNUNET_YES : GNUNET_NO; 506 parse_definitions ("GNUNET_FORCE_LOG", 1) > 0 ? GNUNET_YES : GNUNET_NO;
506 gnunet_force_log_parsed = GNUNET_YES; 507 gnunet_force_log_parsed = GNUNET_YES;
507} 508}
508#endif 509#endif
@@ -546,16 +547,15 @@ GNUNET_log_setup (const char *comp, const char *loglevel, const char *logfile)
546 dirwarn = (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn)); 547 dirwarn = (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn));
547 altlog = FOPEN (fn, "a"); 548 altlog = FOPEN (fn, "a");
548 if (altlog == NULL) 549 if (altlog == NULL)
549 { 550 {
550 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "fopen", fn); 551 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "fopen", fn);
551 if (dirwarn) 552 if (dirwarn)
552 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 553 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
553 _ 554 _("Failed to create or access directory for log file `%s'\n"),
554 ("Failed to create or access directory for log file `%s'\n"), 555 fn);
555 fn); 556 GNUNET_free (fn);
556 GNUNET_free (fn); 557 return GNUNET_SYSERR;
557 return GNUNET_SYSERR; 558 }
558 }
559 GNUNET_free (fn); 559 GNUNET_free (fn);
560 if (GNUNET_stderr != NULL) 560 if (GNUNET_stderr != NULL)
561 fclose (GNUNET_stderr); 561 fclose (GNUNET_stderr);
@@ -596,11 +596,11 @@ GNUNET_logger_remove (GNUNET_Logger logger, void *logger_cls)
596 prev = NULL; 596 prev = NULL;
597 pos = loggers; 597 pos = loggers;
598 while ((pos != NULL) && 598 while ((pos != NULL) &&
599 ((pos->logger != logger) || (pos->logger_cls != logger_cls))) 599 ((pos->logger != logger) || (pos->logger_cls != logger_cls)))
600 { 600 {
601 prev = pos; 601 prev = pos;
602 pos = pos->next; 602 pos = pos->next;
603 } 603 }
604 GNUNET_assert (pos != NULL); 604 GNUNET_assert (pos != NULL);
605 if (prev == NULL) 605 if (prev == NULL)
606 loggers = pos->next; 606 loggers = pos->next;
@@ -620,22 +620,22 @@ GNUNET_logger_remove (GNUNET_Logger logger, void *logger_cls)
620 */ 620 */
621static void 621static void
622output_message (enum GNUNET_ErrorType kind, const char *comp, 622output_message (enum GNUNET_ErrorType kind, const char *comp,
623 const char *datestr, const char *msg) 623 const char *datestr, const char *msg)
624{ 624{
625 struct CustomLogger *pos; 625 struct CustomLogger *pos;
626 626
627 if (GNUNET_stderr != NULL) 627 if (GNUNET_stderr != NULL)
628 { 628 {
629 fprintf (GNUNET_stderr, "%s %s %s %s", datestr, comp, 629 fprintf (GNUNET_stderr, "%s %s %s %s", datestr, comp,
630 GNUNET_error_type_to_string (kind), msg); 630 GNUNET_error_type_to_string (kind), msg);
631 fflush (GNUNET_stderr); 631 fflush (GNUNET_stderr);
632 } 632 }
633 pos = loggers; 633 pos = loggers;
634 while (pos != NULL) 634 while (pos != NULL)
635 { 635 {
636 pos->logger (pos->logger_cls, kind, comp, datestr, msg); 636 pos->logger (pos->logger_cls, kind, comp, datestr, msg);
637 pos = pos->next; 637 pos = pos->next;
638 } 638 }
639} 639}
640 640
641 641
@@ -661,16 +661,15 @@ flush_bulk (const char *datestr)
661 else if (last != last_bulk) 661 else if (last != last_bulk)
662 last--; 662 last--;
663 if (last[0] == '\n') 663 if (last[0] == '\n')
664 { 664 {
665 rev = 1; 665 rev = 1;
666 last[0] = '\0'; 666 last[0] = '\0';
667 } 667 }
668 ft = 668 ft = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration
669 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration 669 (last_bulk_time));
670 (last_bulk_time));
671 snprintf (msg, sizeof (msg), 670 snprintf (msg, sizeof (msg),
672 _("Message `%.*s' repeated %u times in the last %s\n"), 671 _("Message `%.*s' repeated %u times in the last %s\n"),
673 BULK_TRACK_SIZE, last_bulk, last_bulk_repeat, ft); 672 BULK_TRACK_SIZE, last_bulk, last_bulk_repeat, ft);
674 GNUNET_free (ft); 673 GNUNET_free (ft);
675 if (rev == 1) 674 if (rev == 1)
676 last[0] = '\n'; 675 last[0] = '\n';
@@ -690,14 +689,14 @@ void
690GNUNET_log_skip (unsigned int n, int check_reset) 689GNUNET_log_skip (unsigned int n, int check_reset)
691{ 690{
692 if (n == 0) 691 if (n == 0)
693 { 692 {
694 int ok; 693 int ok;
695 694
696 ok = (0 == skip_log); 695 ok = (0 == skip_log);
697 skip_log = 0; 696 skip_log = 0;
698 if (check_reset) 697 if (check_reset)
699 GNUNET_assert (ok); 698 GNUNET_assert (ok);
700 } 699 }
701 else 700 else
702 skip_log += n; 701 skip_log += n;
703} 702}
@@ -736,34 +735,33 @@ mylog (enum GNUNET_ErrorType kind, const char *comp, const char *message,
736 tmptr = localtime (&timetmp); 735 tmptr = localtime (&timetmp);
737 gettimeofday (&timeofday, NULL); 736 gettimeofday (&timeofday, NULL);
738 if (NULL != tmptr) 737 if (NULL != tmptr)
739 { 738 {
740#ifdef WINDOWS 739#ifdef WINDOWS
741 LARGE_INTEGER pc; 740 LARGE_INTEGER pc;
742 741
743 pc.QuadPart = 0; 742 pc.QuadPart = 0;
744 QueryPerformanceCounter (&pc); 743 QueryPerformanceCounter (&pc);
745 strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%020llu", tmptr); 744 strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%020llu", tmptr);
746 snprintf (date, sizeof (date), date2, 745 snprintf (date, sizeof (date), date2,
747 (long long) (pc.QuadPart / 746 (long long) (pc.QuadPart /
748 (performance_frequency.QuadPart / 1000))); 747 (performance_frequency.QuadPart / 1000)));
749#else 748#else
750 strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%06u", tmptr); 749 strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%06u", tmptr);
751 snprintf (date, sizeof (date), date2, timeofday.tv_usec); 750 snprintf (date, sizeof (date), date2, timeofday.tv_usec);
752#endif 751#endif
753 } 752 }
754 else 753 else
755 strcpy (date, "localtime error"); 754 strcpy (date, "localtime error");
756 if ((0 != (kind & GNUNET_ERROR_TYPE_BULK)) 755 if ((0 != (kind & GNUNET_ERROR_TYPE_BULK)) &&
757 && (last_bulk_time.abs_value != 0) 756 (last_bulk_time.abs_value != 0) &&
758 && (0 == strncmp (buf, last_bulk, sizeof (last_bulk)))) 757 (0 == strncmp (buf, last_bulk, sizeof (last_bulk))))
759 { 758 {
760 last_bulk_repeat++; 759 last_bulk_repeat++;
761 if ((GNUNET_TIME_absolute_get_duration (last_bulk_time).rel_value > 760 if ((GNUNET_TIME_absolute_get_duration (last_bulk_time).rel_value >
762 BULK_DELAY_THRESHOLD) 761 BULK_DELAY_THRESHOLD) || (last_bulk_repeat > BULK_REPEAT_THRESHOLD))
763 || (last_bulk_repeat > BULK_REPEAT_THRESHOLD)) 762 flush_bulk (date);
764 flush_bulk (date); 763 return;
765 return; 764 }
766 }
767 flush_bulk (date); 765 flush_bulk (date);
768 strncpy (last_bulk, buf, sizeof (last_bulk)); 766 strncpy (last_bulk, buf, sizeof (last_bulk));
769 last_bulk_repeat = 0; 767 last_bulk_repeat = 0;
@@ -804,7 +802,7 @@ GNUNET_log_nocheck (enum GNUNET_ErrorType kind, const char *message, ...)
804 */ 802 */
805void 803void
806GNUNET_log_from_nocheck (enum GNUNET_ErrorType kind, const char *comp, 804GNUNET_log_from_nocheck (enum GNUNET_ErrorType kind, const char *comp,
807 const char *message, ...) 805 const char *message, ...)
808{ 806{
809 va_list va; 807 va_list va;
810 char comp_w_pid[128]; 808 char comp_w_pid[128];
@@ -922,44 +920,44 @@ GNUNET_a2s (const struct sockaddr *addr, socklen_t addrlen)
922 if (addr == NULL) 920 if (addr == NULL)
923 return _("unknown address"); 921 return _("unknown address");
924 switch (addr->sa_family) 922 switch (addr->sa_family)
925 { 923 {
926 case AF_INET: 924 case AF_INET:
927 if (addrlen != sizeof (struct sockaddr_in)) 925 if (addrlen != sizeof (struct sockaddr_in))
928 return "<invalid v4 address>"; 926 return "<invalid v4 address>";
929 v4 = (const struct sockaddr_in *) addr; 927 v4 = (const struct sockaddr_in *) addr;
930 inet_ntop (AF_INET, &v4->sin_addr, buf, INET_ADDRSTRLEN); 928 inet_ntop (AF_INET, &v4->sin_addr, buf, INET_ADDRSTRLEN);
931 if (0 == ntohs (v4->sin_port)) 929 if (0 == ntohs (v4->sin_port))
932 return buf;
933 strcat (buf, ":");
934 GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v4->sin_port));
935 strcat (buf, b2);
936 return buf;
937 case AF_INET6:
938 if (addrlen != sizeof (struct sockaddr_in6))
939 return "<invalid v4 address>";
940 v6 = (const struct sockaddr_in6 *) addr;
941 buf[0] = '[';
942 inet_ntop (AF_INET6, &v6->sin6_addr, &buf[1], INET6_ADDRSTRLEN);
943 if (0 == ntohs (v6->sin6_port))
944 return &buf[1];
945 strcat (buf, "]:");
946 GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v6->sin6_port));
947 strcat (buf, b2);
948 return buf;
949 case AF_UNIX:
950 if (addrlen <= sizeof (sa_family_t))
951 return "<unbound UNIX client>";
952 un = (const struct sockaddr_un *) addr;
953 off = 0;
954 if (un->sun_path[0] == '\0')
955 off++;
956 snprintf (buf, sizeof (buf), "%s%.*s", (off == 1) ? "@" : "",
957 (int) (addrlen - sizeof (sa_family_t) - 1 - off),
958 &un->sun_path[off]);
959 return buf; 930 return buf;
960 default: 931 strcat (buf, ":");
961 return _("invalid address"); 932 GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v4->sin_port));
962 } 933 strcat (buf, b2);
934 return buf;
935 case AF_INET6:
936 if (addrlen != sizeof (struct sockaddr_in6))
937 return "<invalid v4 address>";
938 v6 = (const struct sockaddr_in6 *) addr;
939 buf[0] = '[';
940 inet_ntop (AF_INET6, &v6->sin6_addr, &buf[1], INET6_ADDRSTRLEN);
941 if (0 == ntohs (v6->sin6_port))
942 return &buf[1];
943 strcat (buf, "]:");
944 GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v6->sin6_port));
945 strcat (buf, b2);
946 return buf;
947 case AF_UNIX:
948 if (addrlen <= sizeof (sa_family_t))
949 return "<unbound UNIX client>";
950 un = (const struct sockaddr_un *) addr;
951 off = 0;
952 if (un->sun_path[0] == '\0')
953 off++;
954 snprintf (buf, sizeof (buf), "%s%.*s", (off == 1) ? "@" : "",
955 (int) (addrlen - sizeof (sa_family_t) - 1 - off),
956 &un->sun_path[off]);
957 return buf;
958 default:
959 return _("invalid address");
960 }
963} 961}
964 962
965 963