2 This file is part of GNUnet.
3 Copyright (C) 2006-2013 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file util/common_logging.c
23 * @brief error handling API
24 * @author Christian Grothoff
27 #include "gnunet_crypto_lib.h"
28 #include "gnunet_disk_lib.h"
29 #include "gnunet_strings_lib.h"
34 * After how many milliseconds do we always print
35 * that "message X was repeated N times"? Use 12h.
37 #define BULK_DELAY_THRESHOLD (12 * 60 * 60 * 1000LL * 1000LL)
40 * After how many repetitions do we always print
41 * that "message X was repeated N times"? (even if
42 * we have not yet reached the delay threshold)
44 #define BULK_REPEAT_THRESHOLD 1000
47 * How many characters do we use for matching of
50 #define BULK_TRACK_SIZE 256
53 * How many characters do we use for matching of
56 #define COMP_TRACK_SIZE 32
59 * How many characters can a date/time string
62 #define DATE_STR_SIZE 64
65 * How many log files to keep?
67 #define ROTATION_KEEP 3
71 * Assumed maximum path length (for the log file name).
78 * Linked list of active loggers.
83 * This is a linked list.
85 struct CustomLogger *next;
99 * The last "bulk" error message that we have been logging.
100 * Note that this message maybe truncated to the first BULK_TRACK_SIZE
101 * characters, in which case it is NOT 0-terminated!
103 static char last_bulk[BULK_TRACK_SIZE];
106 * Type of the last bulk message.
108 static enum GNUNET_ErrorType last_bulk_kind;
111 * Time of the last bulk error message (0 for none)
113 static struct GNUNET_TIME_Absolute last_bulk_time;
116 * Number of times that bulk message has been repeated since.
118 static unsigned int last_bulk_repeat;
121 * Component when the last bulk was logged. Will be 0-terminated.
123 static char last_bulk_comp[COMP_TRACK_SIZE + 1];
128 static char *component;
131 * Running component (without pid).
133 static char *component_nopid;
136 * Format string describing the name of the log file.
138 static char *log_file_name;
143 static enum GNUNET_ErrorType min_level;
146 * Linked list of our custom loggres.
148 static struct CustomLogger *loggers;
151 * Number of log calls to ignore.
153 static int skip_log = 0;
156 * File descriptor to use for "stderr", or NULL for none.
158 static FILE *GNUNET_stderr;
161 * Represents a single logging definition
166 * Component name regex
168 regex_t component_regex;
176 * Function name regex
178 regex_t function_regex;
181 * Lowest line at which this definition matches.
182 * Defaults to 0. Must be <= to_line.
187 * Highest line at which this definition matches.
188 * Defaults to INT_MAX. Must be >= from_line.
193 * Maximal log level allowed for calls that match this definition.
194 * Calls with higher log level will be disabled.
200 * 1 if this definition comes from GNUNET_FORCE_LOG, which means that it
201 * overrides any configuration options. 0 otherwise.
207 * Dynamic array of logging definitions
209 static struct LogDef *logdefs;
212 * Allocated size of logdefs array (in units)
214 static int logdefs_size;
217 * The number of units used in logdefs array.
219 static int logdefs_len;
222 * GNUNET_YES if GNUNET_LOG environment variable is already parsed.
224 static int gnunet_log_parsed;
227 * GNUNET_YES if GNUNET_FORCE_LOG environment variable is already parsed.
229 static int gnunet_force_log_parsed;
232 * GNUNET_YES if at least one definition with forced == 1 is available.
234 static int gnunet_force_log_present;
238 * Contains the number of performance counts per second.
240 static LARGE_INTEGER performance_frequency;
245 * Convert a textual description of a loglevel
246 * to the respective GNUNET_GE_KIND.
248 * @param log loglevel to parse
249 * @return GNUNET_GE_INVALID if log does not parse
251 static enum GNUNET_ErrorType
252 get_type (const char *log)
255 return GNUNET_ERROR_TYPE_UNSPECIFIED;
256 if (0 == strcasecmp (log, _("DEBUG")))
257 return GNUNET_ERROR_TYPE_DEBUG;
258 if (0 == strcasecmp (log, _("INFO")))
259 return GNUNET_ERROR_TYPE_MESSAGE;
260 if (0 == strcasecmp (log, _("MESSAGE")))
261 return GNUNET_ERROR_TYPE_INFO;
262 if (0 == strcasecmp (log, _("WARNING")))
263 return GNUNET_ERROR_TYPE_WARNING;
264 if (0 == strcasecmp (log, _("ERROR")))
265 return GNUNET_ERROR_TYPE_ERROR;
266 if (0 == strcasecmp (log, _("NONE")))
267 return GNUNET_ERROR_TYPE_NONE;
268 return GNUNET_ERROR_TYPE_INVALID;
272 #if !defined(GNUNET_CULL_LOGGING)
274 * Utility function - reallocates logdefs array to be twice as large.
279 logdefs_size = (logdefs_size + 1) * 2;
280 logdefs = GNUNET_realloc (logdefs, logdefs_size * sizeof (struct LogDef));
285 * Abort the process, generate a core dump if possible.
298 * Rotate logs, deleting the oldest log.
300 * @param new_name new name to add to the rotation
303 log_rotate (const char *new_name)
305 static char *rotation[ROTATION_KEEP];
306 static unsigned int rotation_off;
309 if ('\0' == *new_name)
310 return; /* not a real log file name */
311 discard = rotation[rotation_off % ROTATION_KEEP];
314 /* Note: can't log errors during logging (recursion!), so this
315 operation MUST silently fail... */
316 (void) UNLINK (discard);
317 GNUNET_free (discard);
319 rotation[rotation_off % ROTATION_KEEP] = GNUNET_strdup (new_name);
325 * Setup the log file.
327 * @param tm timestamp for which we should setup logging
328 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
331 setup_log_file (const struct tm *tm)
333 static char last_fn[PATH_MAX + 1];
334 char fn[PATH_MAX + 1];
340 if (NULL == log_file_name)
341 return GNUNET_SYSERR;
342 if (0 == strftime (fn, sizeof (fn), log_file_name, tm))
343 return GNUNET_SYSERR;
344 leftsquare = strrchr (fn, '[');
345 if ( (NULL != leftsquare) && (']' == leftsquare[1]) )
347 char *logfile_copy = GNUNET_strdup (fn);
349 logfile_copy[leftsquare - fn] = '\0';
350 logfile_copy[leftsquare - fn + 1] = '\0';
356 &logfile_copy[leftsquare - fn + 2]);
357 GNUNET_free (logfile_copy);
359 if (0 == strcmp (fn, last_fn))
360 return GNUNET_OK; /* no change */
361 log_rotate (last_fn);
362 strcpy (last_fn, fn);
364 GNUNET_DISK_directory_create_for_file (fn))
367 "Failed to create directory for `%s': %s\n",
370 return GNUNET_SYSERR;
373 altlog_fd = OPEN (fn, O_APPEND |
376 _S_IREAD | _S_IWRITE);
378 altlog_fd = OPEN (fn, O_APPEND |
380 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
384 if (NULL != GNUNET_stderr)
385 fclose (GNUNET_stderr);
386 dup_return = dup2 (altlog_fd, 2);
387 (void) close (altlog_fd);
388 if (-1 != dup_return)
390 altlog = fdopen (2, "ab");
404 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", fn);
405 return GNUNET_SYSERR;
407 GNUNET_stderr = altlog;
413 * Utility function - adds a parsed definition to logdefs array.
415 * @param component see struct LogDef, can't be NULL
416 * @param file see struct LogDef, can't be NULL
417 * @param function see struct LogDef, can't be NULL
418 * @param from_line see struct LogDef
419 * @param to_line see struct LogDef
420 * @param level see struct LogDef, must be >= 0
421 * @param force see struct LogDef
422 * @return 0 on success, regex-specific error otherwise
425 add_definition (const char *component,
427 const char *function,
436 if (logdefs_size == logdefs_len)
438 memset (&n, 0, sizeof (n));
439 if (0 == strlen (component))
440 component = (char *) ".*";
441 r = regcomp (&n.component_regex, (const char *) component, REG_NOSUB);
446 if (0 == strlen (file))
447 file = (char *) ".*";
448 r = regcomp (&n.file_regex, (const char *) file, REG_NOSUB);
451 regfree (&n.component_regex);
454 if ((NULL == function) || (0 == strlen (function)))
455 function = (char *) ".*";
456 r = regcomp (&n.function_regex, (const char *) function, REG_NOSUB);
459 regfree (&n.component_regex);
460 regfree (&n.file_regex);
463 n.from_line = from_line;
467 logdefs[logdefs_len++] = n;
473 * Decides whether a particular logging call should or should not be allowed
474 * to be made. Used internally by GNUNET_log*()
476 * @param caller_level loglevel the caller wants to use
477 * @param comp component name the caller uses (NULL means that global
478 * component name is used)
479 * @param file file name containing the logging call, usually __FILE__
480 * @param function function which tries to make a logging call,
481 * usually __FUNCTION__
482 * @param line line at which the call is made, usually __LINE__
483 * @return 0 to disallow the call, 1 to allow it
486 GNUNET_get_log_call_status (int caller_level,
489 const char *function,
497 /* Use default component */
498 comp = component_nopid;
500 /* We have no definitions to override globally configured log level,
501 * so just use it right away.
503 if ( (min_level >= 0) && (GNUNET_NO == gnunet_force_log_present) )
504 return caller_level <= min_level;
506 /* Only look for forced definitions? */
507 force_only = min_level >= 0;
508 for (i = 0; i < logdefs_len; i++)
511 if (( (!force_only) || ld->force) &&
512 (line >= ld->from_line && line <= ld->to_line) &&
513 (0 == regexec (&ld->component_regex, comp, 0, NULL, 0)) &&
514 (0 == regexec (&ld->file_regex, file, 0, NULL, 0)) &&
515 (0 == regexec (&ld->function_regex, function, 0, NULL, 0)))
518 return caller_level <= ld->level;
521 /* No matches - use global level, if defined */
523 return caller_level <= min_level;
524 /* All programs/services previously defaulted to WARNING.
525 * Now *we* default to WARNING, and THEY default to NULL.
526 * Or rather we default to MESSAGE, since things aren't always bad.
528 return caller_level <= GNUNET_ERROR_TYPE_MESSAGE;
533 * Utility function - parses a definition
536 * component;file;function;from_line-to_line;level[/component...]
537 * All entries are mandatory, but may be empty.
538 * Empty entries for component, file and function are treated as
539 * "matches anything".
540 * Empty line entry is treated as "from 0 to INT_MAX"
541 * Line entry with only one line is treated as "this line only"
542 * Entry for level MUST NOT be empty.
543 * Entries for component, file and function that consist of a
544 * single character "*" are treated (at the moment) the same way
545 * empty entries are treated (wildcard matching is not implemented (yet?)).
546 * file entry is matched to the end of __FILE__. That is, it might be
547 * a base name, or a base name with leading directory names (some compilers
548 * define __FILE__ to absolute file path).
550 * @param constname name of the environment variable from which to get the
551 * string to be parsed
552 * @param force 1 if definitions found in constname are to be forced
553 * @return number of added definitions
556 parse_definitions (const char *constname, int force)
562 char *function = NULL;
568 int from_line, to_line;
570 int keep_looking = 1;
572 tmp = getenv (constname);
575 def = GNUNET_strdup (tmp);
578 for (p = def, state = 0, start = def; keep_looking; p++)
582 case ';': /* found a field separator */
586 case 0: /* within a component name */
589 case 1: /* within a file name */
592 case 2: /* within a function name */
593 /* after a file name there must be a function name */
596 case 3: /* within a from-to line range */
597 if (strlen (start) > 0)
600 from_line = strtol (start, &t, 10);
601 if ( (0 != errno) || (from_line < 0) )
606 if ( (t < p) && ('-' == t[0]) )
610 to_line = strtol (start, &t, 10);
611 if ( (0 != errno) || (to_line < 0) || (t != p) )
617 else /* one number means "match this line only" */
620 else /* default to 0-max */
630 case '\0': /* found EOL */
632 /* fall through to '/' */
633 case '/': /* found a definition separator */
636 case 4: /* within a log level */
639 level = get_type ((const char *) start);
640 if ( (GNUNET_ERROR_TYPE_INVALID == level) ||
641 (GNUNET_ERROR_TYPE_UNSPECIFIED == level) ||
642 (0 != add_definition (comp, file, function, from_line, to_line,
664 * Utility function - parses GNUNET_LOG and GNUNET_FORCE_LOG.
667 parse_all_definitions ()
669 if (GNUNET_NO == gnunet_log_parsed)
670 parse_definitions ("GNUNET_LOG", 0);
671 gnunet_log_parsed = GNUNET_YES;
672 if (GNUNET_NO == gnunet_force_log_parsed)
673 gnunet_force_log_present =
674 parse_definitions ("GNUNET_FORCE_LOG", 1) > 0 ? GNUNET_YES : GNUNET_NO;
675 gnunet_force_log_parsed = GNUNET_YES;
683 * @param comp default component to use
684 * @param loglevel what types of messages should be logged
685 * @param logfile which file to write log messages to (can be NULL)
686 * @return #GNUNET_OK on success
689 GNUNET_log_setup (const char *comp,
690 const char *loglevel,
693 const char *env_logfile;
697 min_level = get_type (loglevel);
698 #if !defined(GNUNET_CULL_LOGGING)
699 parse_all_definitions ();
702 QueryPerformanceFrequency (&performance_frequency);
704 GNUNET_free_non_null (component);
705 GNUNET_asprintf (&component, "%s-%d", comp, getpid ());
706 GNUNET_free_non_null (component_nopid);
707 component_nopid = GNUNET_strdup (comp);
709 env_logfile = getenv ("GNUNET_FORCE_LOGFILE");
710 if ((NULL != env_logfile) && (strlen (env_logfile) > 0))
711 logfile = env_logfile;
714 GNUNET_free_non_null (log_file_name);
715 log_file_name = GNUNET_STRINGS_filename_expand (logfile);
716 if (NULL == log_file_name)
717 return GNUNET_SYSERR;
720 return setup_log_file (tm);
725 * Add a custom logger. Note that installing any custom logger
726 * will disable the standard logger. When multiple custom loggers
727 * are installed, all will be called. The standard logger will
728 * only be used if no custom loggers are present.
730 * @param logger log function
731 * @param logger_cls closure for @a logger
734 GNUNET_logger_add (GNUNET_Logger logger,
737 struct CustomLogger *entry;
739 entry = GNUNET_new (struct CustomLogger);
740 entry->logger = logger;
741 entry->logger_cls = logger_cls;
742 entry->next = loggers;
748 * Remove a custom logger.
750 * @param logger log function
751 * @param logger_cls closure for @a logger
754 GNUNET_logger_remove (GNUNET_Logger logger,
757 struct CustomLogger *pos;
758 struct CustomLogger *prev;
762 while ((NULL != pos) &&
763 ((pos->logger != logger) || (pos->logger_cls != logger_cls)))
768 GNUNET_assert (NULL != pos);
772 prev->next = pos->next;
777 CRITICAL_SECTION output_message_cs;
782 * Actually output the log message.
784 * @param kind how severe was the issue
785 * @param comp component responsible
786 * @param datestr current date/time
787 * @param msg the actual message
790 output_message (enum GNUNET_ErrorType kind,
795 struct CustomLogger *pos;
798 EnterCriticalSection (&output_message_cs);
800 /* only use the standard logger if no custom loggers are present */
801 if ( (NULL != GNUNET_stderr) &&
804 FPRINTF (GNUNET_stderr,
808 GNUNET_error_type_to_string (kind),
810 fflush (GNUNET_stderr);
815 pos->logger (pos->logger_cls, kind, comp, datestr, msg);
819 LeaveCriticalSection (&output_message_cs);
825 * Flush an existing bulk report to the output.
827 * @param datestr our current timestamp
830 flush_bulk (const char *datestr)
832 char msg[DATE_STR_SIZE + BULK_TRACK_SIZE + 256];
837 if ((0 == last_bulk_time.abs_value_us) || (0 == last_bulk_repeat))
840 last = memchr (last_bulk, '\0', BULK_TRACK_SIZE);
842 last = &last_bulk[BULK_TRACK_SIZE - 1];
843 else if (last != last_bulk)
850 ft = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration
851 (last_bulk_time), GNUNET_YES);
852 snprintf (msg, sizeof (msg),
853 _("Message `%.*s' repeated %u times in the last %s\n"),
854 BULK_TRACK_SIZE, last_bulk, last_bulk_repeat, ft);
857 output_message (last_bulk_kind, last_bulk_comp, datestr, msg);
858 last_bulk_time = GNUNET_TIME_absolute_get ();
859 last_bulk_repeat = 0;
864 * Ignore the next n calls to the log function.
866 * @param n number of log calls to ignore (could be negative)
867 * @param check_reset #GNUNET_YES to assert that the log skip counter is currently zero
870 GNUNET_log_skip (int n,
877 ok = (0 == skip_log);
890 * Get the number of log calls that are going to be skipped
892 * @return number of log calls to be ignored
895 GNUNET_get_log_skip ()
902 * Output a log message using the default mechanism.
904 * @param kind how severe was the issue
905 * @param comp component responsible
906 * @param message the actual message
907 * @param va arguments to the format string "message"
910 mylog (enum GNUNET_ErrorType kind,
915 char date[DATE_STR_SIZE];
916 char date2[DATE_STR_SIZE];
922 size = VSNPRINTF (NULL, 0, message, vacp) + 1;
923 GNUNET_assert (0 != size);
925 memset (date, 0, DATE_STR_SIZE);
933 offset = GNUNET_TIME_get_offset ();
935 timetmp += offset / 1000;
936 tmptr = localtime (&timetmp);
938 QueryPerformanceCounter (&pc);
941 strcpy (date, "localtime error");
945 strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%020llu", tmptr);
946 snprintf (date, sizeof (date), date2,
947 (long long) (pc.QuadPart /
948 (performance_frequency.QuadPart / 1000)));
951 struct timeval timeofday;
953 gettimeofday (&timeofday, NULL);
954 offset = GNUNET_TIME_get_offset ();
957 timeofday.tv_sec += offset / 1000LL;
958 timeofday.tv_usec += (offset % 1000LL) * 1000LL;
959 if (timeofday.tv_usec > 1000000LL)
961 timeofday.tv_usec -= 1000000LL;
967 timeofday.tv_sec += offset / 1000LL;
968 if (timeofday.tv_usec > - (offset % 1000LL) * 1000LL)
970 timeofday.tv_usec += (offset % 1000LL) * 1000LL;
974 timeofday.tv_usec += 1000000LL + (offset % 1000LL) * 1000LL;
978 tmptr = localtime (&timeofday.tv_sec);
981 strcpy (date, "localtime error");
985 strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%06u", tmptr);
986 snprintf (date, sizeof (date), date2, timeofday.tv_usec);
989 VSNPRINTF (buf, size, message, va);
991 (void) setup_log_file (tmptr);
992 if ((0 != (kind & GNUNET_ERROR_TYPE_BULK)) &&
993 (0 != last_bulk_time.abs_value_us) &&
994 (0 == strncmp (buf, last_bulk, sizeof (last_bulk))))
997 if ( (GNUNET_TIME_absolute_get_duration (last_bulk_time).rel_value_us >
998 BULK_DELAY_THRESHOLD) ||
999 (last_bulk_repeat > BULK_REPEAT_THRESHOLD) )
1004 strncpy (last_bulk, buf, sizeof (last_bulk));
1005 last_bulk_repeat = 0;
1006 last_bulk_kind = kind;
1007 last_bulk_time = GNUNET_TIME_absolute_get ();
1008 strncpy (last_bulk_comp, comp, COMP_TRACK_SIZE);
1009 output_message (kind, comp, date, buf);
1015 * Main log function.
1017 * @param kind how serious is the error?
1018 * @param message what is the message (format string)
1019 * @param ... arguments for format string
1022 GNUNET_log_nocheck (enum GNUNET_ErrorType kind,
1023 const char *message, ...)
1027 va_start (va, message);
1028 mylog (kind, component, message, va);
1034 * Log function that specifies an alternative component.
1035 * This function should be used by plugins.
1037 * @param kind how serious is the error?
1038 * @param comp component responsible for generating the message
1039 * @param message what is the message (format string)
1040 * @param ... arguments for format string
1043 GNUNET_log_from_nocheck (enum GNUNET_ErrorType kind, const char *comp,
1044 const char *message, ...)
1047 char comp_w_pid[128];
1050 comp = component_nopid;
1052 va_start (va, message);
1053 GNUNET_snprintf (comp_w_pid, sizeof (comp_w_pid), "%s-%d", comp, getpid ());
1054 mylog (kind, comp_w_pid, message, va);
1060 * Convert error type to string.
1062 * @param kind type to convert
1063 * @return string corresponding to the type
1066 GNUNET_error_type_to_string (enum GNUNET_ErrorType kind)
1068 if ((kind & GNUNET_ERROR_TYPE_ERROR) > 0)
1070 if ((kind & GNUNET_ERROR_TYPE_WARNING) > 0)
1071 return _("WARNING");
1072 if ((kind & GNUNET_ERROR_TYPE_MESSAGE) > 0)
1073 return _("MESSAGE");
1074 if ((kind & GNUNET_ERROR_TYPE_INFO) > 0)
1076 if ((kind & GNUNET_ERROR_TYPE_DEBUG) > 0)
1078 if ((kind & ~GNUNET_ERROR_TYPE_BULK) == 0)
1080 return _("INVALID");
1085 * Convert a hash to a string (for printing debug messages).
1086 * This is one of the very few calls in the entire API that is
1089 * @param hc the hash code
1090 * @return string form; will be overwritten by next call to GNUNET_h2s.
1093 GNUNET_h2s (const struct GNUNET_HashCode * hc)
1095 static struct GNUNET_CRYPTO_HashAsciiEncoded ret;
1097 GNUNET_CRYPTO_hash_to_enc (hc, &ret);
1098 ret.encoding[8] = '\0';
1099 return (const char *) ret.encoding;
1104 * Convert a hash to a string (for printing debug messages).
1105 * This is one of the very few calls in the entire API that is
1108 * @param hc the hash code
1109 * @return string form; will be overwritten by next call to GNUNET_h2s_full.
1112 GNUNET_h2s_full (const struct GNUNET_HashCode * hc)
1114 static struct GNUNET_CRYPTO_HashAsciiEncoded ret;
1116 GNUNET_CRYPTO_hash_to_enc (hc, &ret);
1117 ret.encoding[sizeof (ret) - 1] = '\0';
1118 return (const char *) ret.encoding;
1123 * Convert a peer identity to a string (for printing debug messages).
1124 * This is one of the very few calls in the entire API that is
1127 * @param pid the peer identity
1128 * @return string form of the pid; will be overwritten by next
1129 * call to #GNUNET_i2s.
1132 GNUNET_i2s (const struct GNUNET_PeerIdentity *pid)
1134 static char buf[256];
1137 ret = GNUNET_CRYPTO_eddsa_public_key_to_string (&pid->public_key);
1146 * Convert a peer identity to a string (for printing debug messages).
1147 * This is one of the very few calls in the entire API that is
1150 * @param pid the peer identity
1151 * @return string form of the pid; will be overwritten by next
1152 * call to #GNUNET_i2s_full.
1155 GNUNET_i2s_full (const struct GNUNET_PeerIdentity *pid)
1157 static char buf[256];
1160 ret = GNUNET_CRYPTO_eddsa_public_key_to_string (&pid->public_key);
1168 * Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string
1169 * (for printing debug messages). This is one of the very few calls
1170 * in the entire API that is NOT reentrant!
1172 * @param addr the address
1173 * @param addrlen the length of the address in @a addr
1174 * @return nicely formatted string for the address
1175 * will be overwritten by next call to #GNUNET_a2s.
1178 GNUNET_a2s (const struct sockaddr *addr,
1182 #define LEN GNUNET_MAX ((INET6_ADDRSTRLEN + 8), \
1183 (1 + sizeof (struct sockaddr_un) - sizeof (sa_family_t)))
1185 #define LEN (INET6_ADDRSTRLEN + 8)
1187 static char buf[LEN];
1190 const struct sockaddr_in *v4;
1191 const struct sockaddr_un *un;
1192 const struct sockaddr_in6 *v6;
1196 return _("unknown address");
1197 switch (addr->sa_family)
1200 if (addrlen != sizeof (struct sockaddr_in))
1201 return "<invalid v4 address>";
1202 v4 = (const struct sockaddr_in *) addr;
1203 inet_ntop (AF_INET, &v4->sin_addr, buf, INET_ADDRSTRLEN);
1204 if (0 == ntohs (v4->sin_port))
1207 GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v4->sin_port));
1211 if (addrlen != sizeof (struct sockaddr_in6))
1212 return "<invalid v4 address>";
1213 v6 = (const struct sockaddr_in6 *) addr;
1215 inet_ntop (AF_INET6, &v6->sin6_addr, &buf[1], INET6_ADDRSTRLEN);
1216 if (0 == ntohs (v6->sin6_port))
1219 GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v6->sin6_port));
1223 if (addrlen <= sizeof (sa_family_t))
1224 return "<unbound UNIX client>";
1225 un = (const struct sockaddr_un *) addr;
1227 if ('\0' == un->sun_path[0])
1229 memset (buf, 0, sizeof (buf));
1230 GNUNET_snprintf (buf,
1233 (1 == off) ? "@" : "",
1234 (int) (addrlen - sizeof (sa_family_t) - off),
1235 &un->sun_path[off]);
1238 return _("invalid address");
1244 * Log error message about missing configuration option.
1246 * @param kind log level
1247 * @param section section with missing option
1248 * @param option name of missing option
1251 GNUNET_log_config_missing (enum GNUNET_ErrorType kind,
1252 const char *section,
1256 _("Configuration fails to specify option `%s' in section `%s'!\n"),
1263 * Log error message about invalid configuration option value.
1265 * @param kind log level
1266 * @param section section with invalid option
1267 * @param option name of invalid option
1268 * @param required what is required that is invalid about the option
1271 GNUNET_log_config_invalid (enum GNUNET_ErrorType kind,
1272 const char *section,
1274 const char *required)
1277 _("Configuration specifies invalid value for option `%s' in section `%s': %s\n"),
1278 option, section, required);
1285 void __attribute__ ((constructor))
1286 GNUNET_util_cl_init ()
1288 GNUNET_stderr = stderr;
1290 GNInitWinEnv (NULL);
1293 if (!InitializeCriticalSectionAndSpinCount (&output_message_cs, 0x00000400))
1302 void __attribute__ ((destructor))
1303 GNUNET_util_cl_fini ()
1306 DeleteCriticalSection (&output_message_cs);
1309 GNShutdownWinEnv ();
1313 /* end of common_logging.c */