diff options
Diffstat (limited to 'src/util/common_logging.c')
-rw-r--r-- | src/util/common_logging.c | 207 |
1 files changed, 102 insertions, 105 deletions
diff --git a/src/util/common_logging.c b/src/util/common_logging.c index 463936741..37f630854 100644 --- a/src/util/common_logging.c +++ b/src/util/common_logging.c | |||
@@ -107,7 +107,7 @@ static unsigned int last_bulk_repeat; | |||
107 | /** | 107 | /** |
108 | * Component when the last bulk was logged. Will be 0-terminated. | 108 | * Component when the last bulk was logged. Will be 0-terminated. |
109 | */ | 109 | */ |
110 | static char last_bulk_comp[COMP_TRACK_SIZE+1]; | 110 | static char last_bulk_comp[COMP_TRACK_SIZE + 1]; |
111 | 111 | ||
112 | /** | 112 | /** |
113 | * Running component. | 113 | * Running component. |
@@ -187,10 +187,7 @@ GNUNET_log_setup (const char *comp, const char *loglevel, const char *logfile) | |||
187 | QueryPerformanceFrequency (&performance_frequency); | 187 | QueryPerformanceFrequency (&performance_frequency); |
188 | #endif | 188 | #endif |
189 | GNUNET_free_non_null (component); | 189 | GNUNET_free_non_null (component); |
190 | GNUNET_asprintf (&component, | 190 | GNUNET_asprintf (&component, "%s-%d", comp, getpid ()); |
191 | "%s-%d", | ||
192 | comp, | ||
193 | getpid()); | ||
194 | env_loglevel = getenv ("GNUNET_LOGLEVEL"); | 191 | env_loglevel = getenv ("GNUNET_LOGLEVEL"); |
195 | if (env_loglevel != NULL) | 192 | if (env_loglevel != NULL) |
196 | env_minlevel = get_type (env_loglevel); | 193 | env_minlevel = get_type (env_loglevel); |
@@ -205,20 +202,20 @@ GNUNET_log_setup (const char *comp, const char *loglevel, const char *logfile) | |||
205 | if (logfile == NULL) | 202 | if (logfile == NULL) |
206 | return GNUNET_OK; | 203 | return GNUNET_OK; |
207 | fn = GNUNET_STRINGS_filename_expand (logfile); | 204 | fn = GNUNET_STRINGS_filename_expand (logfile); |
208 | if (NULL == fn) | 205 | if (NULL == fn) |
209 | return GNUNET_SYSERR; | 206 | return GNUNET_SYSERR; |
210 | dirwarn = (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn)); | 207 | dirwarn = (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn)); |
211 | altlog = FOPEN (fn, "a"); | 208 | altlog = FOPEN (fn, "a"); |
212 | if (altlog == NULL) | 209 | if (altlog == NULL) |
213 | { | 210 | { |
214 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "fopen", fn); | 211 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "fopen", fn); |
215 | if (dirwarn) | 212 | if (dirwarn) |
216 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 213 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
217 | _("Failed to create or access directory for log file `%s'\n"), | 214 | _("Failed to create or access directory for log file `%s'\n"), |
218 | fn); | 215 | fn); |
219 | GNUNET_free (fn); | 216 | GNUNET_free (fn); |
220 | return GNUNET_SYSERR; | 217 | return GNUNET_SYSERR; |
221 | } | 218 | } |
222 | GNUNET_free (fn); | 219 | GNUNET_free (fn); |
223 | if (GNUNET_stderr != NULL) | 220 | if (GNUNET_stderr != NULL) |
224 | fclose (GNUNET_stderr); | 221 | fclose (GNUNET_stderr); |
@@ -260,10 +257,10 @@ GNUNET_logger_remove (GNUNET_Logger logger, void *logger_cls) | |||
260 | pos = loggers; | 257 | pos = loggers; |
261 | while ((pos != NULL) && | 258 | while ((pos != NULL) && |
262 | ((pos->logger != logger) || (pos->logger_cls != logger_cls))) | 259 | ((pos->logger != logger) || (pos->logger_cls != logger_cls))) |
263 | { | 260 | { |
264 | prev = pos; | 261 | prev = pos; |
265 | pos = pos->next; | 262 | pos = pos->next; |
266 | } | 263 | } |
267 | GNUNET_assert (pos != NULL); | 264 | GNUNET_assert (pos != NULL); |
268 | if (prev == NULL) | 265 | if (prev == NULL) |
269 | loggers = pos->next; | 266 | loggers = pos->next; |
@@ -286,18 +283,19 @@ output_message (enum GNUNET_ErrorType kind, | |||
286 | const char *comp, const char *datestr, const char *msg) | 283 | const char *comp, const char *datestr, const char *msg) |
287 | { | 284 | { |
288 | struct CustomLogger *pos; | 285 | struct CustomLogger *pos; |
286 | |||
289 | if (GNUNET_stderr != NULL) | 287 | if (GNUNET_stderr != NULL) |
290 | { | 288 | { |
291 | fprintf (GNUNET_stderr, "%s %s %s %s", datestr, comp, | 289 | fprintf (GNUNET_stderr, "%s %s %s %s", datestr, comp, |
292 | GNUNET_error_type_to_string (kind), msg); | 290 | GNUNET_error_type_to_string (kind), msg); |
293 | fflush (GNUNET_stderr); | 291 | fflush (GNUNET_stderr); |
294 | } | 292 | } |
295 | pos = loggers; | 293 | pos = loggers; |
296 | while (pos != NULL) | 294 | while (pos != NULL) |
297 | { | 295 | { |
298 | pos->logger (pos->logger_cls, kind, comp, datestr, msg); | 296 | pos->logger (pos->logger_cls, kind, comp, datestr, msg); |
299 | pos = pos->next; | 297 | pos = pos->next; |
300 | } | 298 | } |
301 | } | 299 | } |
302 | 300 | ||
303 | 301 | ||
@@ -323,13 +321,12 @@ flush_bulk (const char *datestr) | |||
323 | else if (last != last_bulk) | 321 | else if (last != last_bulk) |
324 | last--; | 322 | last--; |
325 | if (last[0] == '\n') | 323 | if (last[0] == '\n') |
326 | { | 324 | { |
327 | rev = 1; | 325 | rev = 1; |
328 | last[0] = '\0'; | 326 | last[0] = '\0'; |
329 | } | 327 | } |
330 | ft = | 328 | ft = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration |
331 | GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration | 329 | (last_bulk_time)); |
332 | (last_bulk_time)); | ||
333 | snprintf (msg, sizeof (msg), | 330 | snprintf (msg, sizeof (msg), |
334 | _("Message `%.*s' repeated %u times in the last %s\n"), | 331 | _("Message `%.*s' repeated %u times in the last %s\n"), |
335 | BULK_TRACK_SIZE, last_bulk, last_bulk_repeat, ft); | 332 | BULK_TRACK_SIZE, last_bulk, last_bulk_repeat, ft); |
@@ -352,14 +349,14 @@ void | |||
352 | GNUNET_log_skip (unsigned int n, int check_reset) | 349 | GNUNET_log_skip (unsigned int n, int check_reset) |
353 | { | 350 | { |
354 | if (n == 0) | 351 | if (n == 0) |
355 | { | 352 | { |
356 | int ok; | 353 | int ok; |
357 | 354 | ||
358 | ok = (0 == skip_log); | 355 | ok = (0 == skip_log); |
359 | skip_log = 0; | 356 | skip_log = 0; |
360 | if (check_reset) | 357 | if (check_reset) |
361 | GNUNET_assert (ok); | 358 | GNUNET_assert (ok); |
362 | } | 359 | } |
363 | else | 360 | else |
364 | skip_log += n; | 361 | skip_log += n; |
365 | } | 362 | } |
@@ -387,10 +384,10 @@ mylog (enum GNUNET_ErrorType kind, | |||
387 | va_list vacp; | 384 | va_list vacp; |
388 | 385 | ||
389 | if (skip_log > 0) | 386 | if (skip_log > 0) |
390 | { | 387 | { |
391 | skip_log--; | 388 | skip_log--; |
392 | return; | 389 | return; |
393 | } | 390 | } |
394 | if ((kind & (~GNUNET_ERROR_TYPE_BULK)) > min_level) | 391 | if ((kind & (~GNUNET_ERROR_TYPE_BULK)) > min_level) |
395 | return; | 392 | return; |
396 | va_copy (vacp, va); | 393 | va_copy (vacp, va); |
@@ -403,15 +400,18 @@ mylog (enum GNUNET_ErrorType kind, | |||
403 | time (&timetmp); | 400 | time (&timetmp); |
404 | memset (date, 0, DATE_STR_SIZE); | 401 | memset (date, 0, DATE_STR_SIZE); |
405 | tmptr = localtime (&timetmp); | 402 | tmptr = localtime (&timetmp); |
406 | gettimeofday(&timeofday, NULL); | 403 | gettimeofday (&timeofday, NULL); |
407 | if (NULL != tmptr) | 404 | if (NULL != tmptr) |
408 | { | 405 | { |
409 | #ifdef WINDOWS | 406 | #ifdef WINDOWS |
410 | LARGE_INTEGER pc; | 407 | LARGE_INTEGER pc; |
408 | |||
411 | pc.QuadPart = 0; | 409 | pc.QuadPart = 0; |
412 | QueryPerformanceCounter (&pc); | 410 | QueryPerformanceCounter (&pc); |
413 | strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%020llu", tmptr); | 411 | strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%020llu", tmptr); |
414 | snprintf (date, sizeof (date), date2, (long long) (pc.QuadPart / (performance_frequency.QuadPart / 1000))); | 412 | snprintf (date, sizeof (date), date2, |
413 | (long long) (pc.QuadPart / | ||
414 | (performance_frequency.QuadPart / 1000))); | ||
415 | #else | 415 | #else |
416 | strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%06u", tmptr); | 416 | strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%06u", tmptr); |
417 | snprintf (date, sizeof (date), date2, timeofday.tv_usec); | 417 | snprintf (date, sizeof (date), date2, timeofday.tv_usec); |
@@ -422,15 +422,14 @@ mylog (enum GNUNET_ErrorType kind, | |||
422 | if ((0 != (kind & GNUNET_ERROR_TYPE_BULK)) && | 422 | if ((0 != (kind & GNUNET_ERROR_TYPE_BULK)) && |
423 | (last_bulk_time.abs_value != 0) && | 423 | (last_bulk_time.abs_value != 0) && |
424 | (0 == strncmp (buf, last_bulk, sizeof (last_bulk)))) | 424 | (0 == strncmp (buf, last_bulk, sizeof (last_bulk)))) |
425 | { | 425 | { |
426 | last_bulk_repeat++; | 426 | last_bulk_repeat++; |
427 | if ((GNUNET_TIME_absolute_get_duration (last_bulk_time).rel_value > | 427 | if ((GNUNET_TIME_absolute_get_duration (last_bulk_time).rel_value > |
428 | BULK_DELAY_THRESHOLD) | 428 | BULK_DELAY_THRESHOLD) || (last_bulk_repeat > BULK_REPEAT_THRESHOLD)) |
429 | || (last_bulk_repeat > BULK_REPEAT_THRESHOLD)) | 429 | flush_bulk (date); |
430 | flush_bulk (date); | 430 | free (buf); |
431 | free (buf); | 431 | return; |
432 | return; | 432 | } |
433 | } | ||
434 | flush_bulk (date); | 433 | flush_bulk (date); |
435 | strncpy (last_bulk, buf, sizeof (last_bulk)); | 434 | strncpy (last_bulk, buf, sizeof (last_bulk)); |
436 | last_bulk_repeat = 0; | 435 | last_bulk_repeat = 0; |
@@ -453,6 +452,7 @@ void | |||
453 | GNUNET_log (enum GNUNET_ErrorType kind, const char *message, ...) | 452 | GNUNET_log (enum GNUNET_ErrorType kind, const char *message, ...) |
454 | { | 453 | { |
455 | va_list va; | 454 | va_list va; |
455 | |||
456 | va_start (va, message); | 456 | va_start (va, message); |
457 | mylog (kind, component, message, va); | 457 | mylog (kind, component, message, va); |
458 | va_end (va); | 458 | va_end (va); |
@@ -476,11 +476,7 @@ GNUNET_log_from (enum GNUNET_ErrorType kind, | |||
476 | char comp_w_pid[128]; | 476 | char comp_w_pid[128]; |
477 | 477 | ||
478 | va_start (va, message); | 478 | va_start (va, message); |
479 | GNUNET_snprintf (comp_w_pid, | 479 | GNUNET_snprintf (comp_w_pid, sizeof (comp_w_pid), "%s-%d", comp, getpid ()); |
480 | sizeof (comp_w_pid), | ||
481 | "%s-%d", | ||
482 | comp, | ||
483 | getpid()); | ||
484 | mylog (kind, comp_w_pid, message, va); | 480 | mylog (kind, comp_w_pid, message, va); |
485 | va_end (va); | 481 | va_end (va); |
486 | } | 482 | } |
@@ -539,7 +535,7 @@ GNUNET_h2s_full (const GNUNET_HashCode * hc) | |||
539 | static struct GNUNET_CRYPTO_HashAsciiEncoded ret; | 535 | static struct GNUNET_CRYPTO_HashAsciiEncoded ret; |
540 | 536 | ||
541 | GNUNET_CRYPTO_hash_to_enc (hc, &ret); | 537 | GNUNET_CRYPTO_hash_to_enc (hc, &ret); |
542 | ret.encoding[sizeof(ret)-1] = '\0'; | 538 | ret.encoding[sizeof (ret) - 1] = '\0'; |
543 | return (const char *) ret.encoding; | 539 | return (const char *) ret.encoding; |
544 | } | 540 | } |
545 | 541 | ||
@@ -587,46 +583,47 @@ GNUNET_a2s (const struct sockaddr *addr, socklen_t addrlen) | |||
587 | if (addr == NULL) | 583 | if (addr == NULL) |
588 | return _("unknown address"); | 584 | return _("unknown address"); |
589 | switch (addr->sa_family) | 585 | switch (addr->sa_family) |
590 | { | 586 | { |
591 | case AF_INET: | 587 | case AF_INET: |
592 | if (addrlen != sizeof (struct sockaddr_in)) | 588 | if (addrlen != sizeof (struct sockaddr_in)) |
593 | return "<invalid v4 address>"; | 589 | return "<invalid v4 address>"; |
594 | v4 = (const struct sockaddr_in *) addr; | 590 | v4 = (const struct sockaddr_in *) addr; |
595 | inet_ntop (AF_INET, &v4->sin_addr, buf, INET_ADDRSTRLEN); | 591 | inet_ntop (AF_INET, &v4->sin_addr, buf, INET_ADDRSTRLEN); |
596 | if (0 == ntohs (v4->sin_port)) | 592 | if (0 == ntohs (v4->sin_port)) |
597 | return buf; | ||
598 | strcat (buf, ":"); | ||
599 | GNUNET_snprintf (b2, sizeof(b2), "%u", ntohs (v4->sin_port)); | ||
600 | strcat (buf, b2); | ||
601 | return buf; | ||
602 | case AF_INET6: | ||
603 | if (addrlen != sizeof (struct sockaddr_in6)) | ||
604 | return "<invalid v4 address>"; | ||
605 | v6 = (const struct sockaddr_in6 *) addr; | ||
606 | buf[0] = '['; | ||
607 | inet_ntop (AF_INET6, &v6->sin6_addr, &buf[1], INET6_ADDRSTRLEN); | ||
608 | if (0 == ntohs (v6->sin6_port)) | ||
609 | return &buf[1]; | ||
610 | strcat (buf, "]:"); | ||
611 | GNUNET_snprintf (b2, sizeof(b2), "%u", ntohs (v6->sin6_port)); | ||
612 | strcat (buf, b2); | ||
613 | return buf; | ||
614 | case AF_UNIX: | ||
615 | if (addrlen <= sizeof (sa_family_t)) | ||
616 | return "<unbound UNIX client>"; | ||
617 | un = (const struct sockaddr_un*) addr; | ||
618 | off = 0; | ||
619 | if (un->sun_path[0] == '\0') off++; | ||
620 | snprintf (buf, | ||
621 | sizeof (buf), | ||
622 | "%s%.*s", | ||
623 | (off == 1) ? "@" : "", | ||
624 | (int) (addrlen - sizeof (sa_family_t) - 1 - off), | ||
625 | &un->sun_path[off]); | ||
626 | return buf; | 593 | return buf; |
627 | default: | 594 | strcat (buf, ":"); |
628 | return _("invalid address"); | 595 | GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v4->sin_port)); |
629 | } | 596 | strcat (buf, b2); |
597 | return buf; | ||
598 | case AF_INET6: | ||
599 | if (addrlen != sizeof (struct sockaddr_in6)) | ||
600 | return "<invalid v4 address>"; | ||
601 | v6 = (const struct sockaddr_in6 *) addr; | ||
602 | buf[0] = '['; | ||
603 | inet_ntop (AF_INET6, &v6->sin6_addr, &buf[1], INET6_ADDRSTRLEN); | ||
604 | if (0 == ntohs (v6->sin6_port)) | ||
605 | return &buf[1]; | ||
606 | strcat (buf, "]:"); | ||
607 | GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v6->sin6_port)); | ||
608 | strcat (buf, b2); | ||
609 | return buf; | ||
610 | case AF_UNIX: | ||
611 | if (addrlen <= sizeof (sa_family_t)) | ||
612 | return "<unbound UNIX client>"; | ||
613 | un = (const struct sockaddr_un *) addr; | ||
614 | off = 0; | ||
615 | if (un->sun_path[0] == '\0') | ||
616 | off++; | ||
617 | snprintf (buf, | ||
618 | sizeof (buf), | ||
619 | "%s%.*s", | ||
620 | (off == 1) ? "@" : "", | ||
621 | (int) (addrlen - sizeof (sa_family_t) - 1 - off), | ||
622 | &un->sun_path[off]); | ||
623 | return buf; | ||
624 | default: | ||
625 | return _("invalid address"); | ||
626 | } | ||
630 | } | 627 | } |
631 | 628 | ||
632 | 629 | ||