2 This file is part of GNUnet.
3 Copyright (C) 2006-2013 Christian Grothoff (and other contributing authors)
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_strings_lib.h"
33 * After how many milliseconds do we always print
34 * that "message X was repeated N times"? Use 12h.
36 #define BULK_DELAY_THRESHOLD (12 * 60 * 60 * 1000LL * 1000LL)
39 * After how many repetitions do we always print
40 * that "message X was repeated N times"? (even if
41 * we have not yet reached the delay threshold)
43 #define BULK_REPEAT_THRESHOLD 1000
46 * How many characters do we use for matching of
49 #define BULK_TRACK_SIZE 256
52 * How many characters do we use for matching of
55 #define COMP_TRACK_SIZE 32
58 * How many characters can a date/time string
61 #define DATE_STR_SIZE 64
64 * How many log files to keep?
66 #define ROTATION_KEEP 3
70 * Assumed maximum path length (for the log file name).
77 * Linked list of active loggers.
82 * This is a linked list.
84 struct CustomLogger *next;
98 * The last "bulk" error message that we have been logging.
99 * Note that this message maybe truncated to the first BULK_TRACK_SIZE
100 * characters, in which case it is NOT 0-terminated!
102 static char last_bulk[BULK_TRACK_SIZE];
105 * Type of the last bulk message.
107 static enum GNUNET_ErrorType last_bulk_kind;
110 * Time of the last bulk error message (0 for none)
112 static struct GNUNET_TIME_Absolute last_bulk_time;
115 * Number of times that bulk message has been repeated since.
117 static unsigned int last_bulk_repeat;
120 * Component when the last bulk was logged. Will be 0-terminated.
122 static char last_bulk_comp[COMP_TRACK_SIZE + 1];
127 static char *component;
130 * Running component (without pid).
132 static char *component_nopid;
135 * Format string describing the name of the log file.
137 static char *log_file_name;
142 static enum GNUNET_ErrorType min_level;
145 * Linked list of our custom loggres.
147 static struct CustomLogger *loggers;
150 * Number of log calls to ignore.
152 static int skip_log = 0;
155 * File descriptor to use for "stderr", or NULL for none.
157 static FILE *GNUNET_stderr;
160 * Represents a single logging definition
165 * Component name regex
167 regex_t component_regex;
175 * Function name regex
177 regex_t function_regex;
180 * Lowest line at which this definition matches.
181 * Defaults to 0. Must be <= to_line.
186 * Highest line at which this definition matches.
187 * Defaults to INT_MAX. Must be >= from_line.
192 * Maximal log level allowed for calls that match this definition.
193 * Calls with higher log level will be disabled.
199 * 1 if this definition comes from GNUNET_FORCE_LOG, which means that it
200 * overrides any configuration options. 0 otherwise.
206 * Dynamic array of logging definitions
208 static struct LogDef *logdefs;
211 * Allocated size of logdefs array (in units)
213 static int logdefs_size;
216 * The number of units used in logdefs array.
218 static int logdefs_len;
221 * GNUNET_YES if GNUNET_LOG environment variable is already parsed.
223 static int gnunet_log_parsed;
226 * GNUNET_YES if GNUNET_FORCE_LOG environment variable is already parsed.
228 static int gnunet_force_log_parsed;
231 * GNUNET_YES if at least one definition with forced == 1 is available.
233 static int gnunet_force_log_present;
237 * Contains the number of performance counts per second.
239 static LARGE_INTEGER performance_frequency;
244 * Convert a textual description of a loglevel
245 * to the respective GNUNET_GE_KIND.
247 * @param log loglevel to parse
248 * @return GNUNET_GE_INVALID if log does not parse
250 static enum GNUNET_ErrorType
251 get_type (const char *log)
254 return GNUNET_ERROR_TYPE_UNSPECIFIED;
255 if (0 == strcasecmp (log, _("DEBUG")))
256 return GNUNET_ERROR_TYPE_DEBUG;
257 if (0 == strcasecmp (log, _("INFO")))
258 return GNUNET_ERROR_TYPE_INFO;
259 if (0 == strcasecmp (log, _("WARNING")))
260 return GNUNET_ERROR_TYPE_WARNING;
261 if (0 == strcasecmp (log, _("ERROR")))
262 return GNUNET_ERROR_TYPE_ERROR;
263 if (0 == strcasecmp (log, _("NONE")))
264 return GNUNET_ERROR_TYPE_NONE;
265 return GNUNET_ERROR_TYPE_INVALID;
269 #if !defined(GNUNET_CULL_LOGGING)
271 * Utility function - reallocates logdefs array to be twice as large.
276 logdefs_size = (logdefs_size + 1) * 2;
277 logdefs = GNUNET_realloc (logdefs, logdefs_size * sizeof (struct LogDef));
282 * Abort the process, generate a core dump if possible.
295 * Rotate logs, deleting the oldest log.
297 * @param new_name new name to add to the rotation
300 log_rotate (const char *new_name)
302 static char *rotation[ROTATION_KEEP];
303 static unsigned int rotation_off;
306 if ('\0' == *new_name)
307 return; /* not a real log file name */
308 discard = rotation[rotation_off % ROTATION_KEEP];
311 /* Note: can't log errors during logging (recursion!), so this
312 operation MUST silently fail... */
313 (void) UNLINK (discard);
314 GNUNET_free (discard);
316 rotation[rotation_off % ROTATION_KEEP] = GNUNET_strdup (new_name);
322 * Setup the log file.
324 * @param tm timestamp for which we should setup logging
325 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
328 setup_log_file (const struct tm *tm)
330 static char last_fn[PATH_MAX + 1];
331 char fn[PATH_MAX + 1];
337 if (NULL == log_file_name)
338 return GNUNET_SYSERR;
339 if (0 == strftime (fn, sizeof (fn), log_file_name, tm))
340 return GNUNET_SYSERR;
341 leftsquare = strrchr (fn, '[');
342 if ( (NULL != leftsquare) && (']' == leftsquare[1]) )
344 char *logfile_copy = GNUNET_strdup (fn);
346 logfile_copy[leftsquare - fn] = '\0';
347 logfile_copy[leftsquare - fn + 1] = '\0';
353 &logfile_copy[leftsquare - fn + 2]);
354 GNUNET_free (logfile_copy);
356 if (0 == strcmp (fn, last_fn))
357 return GNUNET_OK; /* no change */
358 log_rotate (last_fn);
359 strcpy (last_fn, fn);
361 altlog_fd = OPEN (fn, O_APPEND |
364 _S_IREAD | _S_IWRITE);
366 altlog_fd = OPEN (fn, O_APPEND |
368 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
372 if (NULL != GNUNET_stderr)
373 fclose (GNUNET_stderr);
374 dup_return = dup2 (altlog_fd, 2);
375 (void) close (altlog_fd);
376 if (-1 != dup_return)
378 altlog = fdopen (2, "ab");
392 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", fn);
393 return GNUNET_SYSERR;
395 GNUNET_stderr = altlog;
401 * Utility function - adds a parsed definition to logdefs array.
403 * @param component see struct LogDef, can't be NULL
404 * @param file see struct LogDef, can't be NULL
405 * @param function see struct LogDef, can't be NULL
406 * @param from_line see struct LogDef
407 * @param to_line see struct LogDef
408 * @param level see struct LogDef, must be >= 0
409 * @param force see struct LogDef
410 * @return 0 on success, regex-specific error otherwise
413 add_definition (const char *component,
415 const char *function,
424 if (logdefs_size == logdefs_len)
426 memset (&n, 0, sizeof (n));
427 if (0 == strlen (component))
428 component = (char *) ".*";
429 r = regcomp (&n.component_regex, (const char *) component, REG_NOSUB);
434 if (0 == strlen (file))
435 file = (char *) ".*";
436 r = regcomp (&n.file_regex, (const char *) file, REG_NOSUB);
439 regfree (&n.component_regex);
442 if ((NULL == function) || (0 == strlen (function)))
443 function = (char *) ".*";
444 r = regcomp (&n.function_regex, (const char *) function, REG_NOSUB);
447 regfree (&n.component_regex);
448 regfree (&n.file_regex);
451 n.from_line = from_line;
455 logdefs[logdefs_len++] = n;
461 * Decides whether a particular logging call should or should not be allowed
462 * to be made. Used internally by GNUNET_log*()
464 * @param caller_level loglevel the caller wants to use
465 * @param comp component name the caller uses (NULL means that global
466 * component name is used)
467 * @param file file name containing the logging call, usually __FILE__
468 * @param function function which tries to make a logging call,
469 * usually __FUNCTION__
470 * @param line line at which the call is made, usually __LINE__
471 * @return 0 to disallow the call, 1 to allow it
474 GNUNET_get_log_call_status (int caller_level,
477 const char *function,
485 /* Use default component */
486 comp = component_nopid;
488 /* We have no definitions to override globally configured log level,
489 * so just use it right away.
491 if ( (min_level >= 0) && (GNUNET_NO == gnunet_force_log_present) )
492 return caller_level <= min_level;
494 /* Only look for forced definitions? */
495 force_only = min_level >= 0;
496 for (i = 0; i < logdefs_len; i++)
499 if (( (!force_only) || ld->force) &&
500 (line >= ld->from_line && line <= ld->to_line) &&
501 (0 == regexec (&ld->component_regex, comp, 0, NULL, 0)) &&
502 (0 == regexec (&ld->file_regex, file, 0, NULL, 0)) &&
503 (0 == regexec (&ld->function_regex, function, 0, NULL, 0)))
506 return caller_level <= ld->level;
509 /* No matches - use global level, if defined */
511 return caller_level <= min_level;
512 /* All programs/services previously defaulted to WARNING.
513 * Now WE default to WARNING, and THEY default to NULL.
515 return caller_level <= GNUNET_ERROR_TYPE_WARNING;
520 * Utility function - parses a definition
523 * component;file;function;from_line-to_line;level[/component...]
524 * All entries are mandatory, but may be empty.
525 * Empty entries for component, file and function are treated as
526 * "matches anything".
527 * Empty line entry is treated as "from 0 to INT_MAX"
528 * Line entry with only one line is treated as "this line only"
529 * Entry for level MUST NOT be empty.
530 * Entries for component, file and function that consist of a
531 * single character "*" are treated (at the moment) the same way
532 * empty entries are treated (wildcard matching is not implemented (yet?)).
533 * file entry is matched to the end of __FILE__. That is, it might be
534 * a base name, or a base name with leading directory names (some compilers
535 * define __FILE__ to absolute file path).
537 * @param constname name of the environment variable from which to get the
538 * string to be parsed
539 * @param force 1 if definitions found in constname are to be forced
540 * @return number of added definitions
543 parse_definitions (const char *constname, int force)
549 char *function = NULL;
555 int from_line, to_line;
557 int keep_looking = 1;
559 tmp = getenv (constname);
562 def = GNUNET_strdup (tmp);
565 for (p = def, state = 0, start = def; keep_looking; p++)
569 case ';': /* found a field separator */
573 case 0: /* within a component name */
576 case 1: /* within a file name */
579 case 2: /* within a function name */
580 /* after a file name there must be a function name */
583 case 3: /* within a from-to line range */
584 if (strlen (start) > 0)
587 from_line = strtol (start, &t, 10);
588 if ( (0 != errno) || (from_line < 0) )
593 if ( (t < p) && ('-' == t[0]) )
597 to_line = strtol (start, &t, 10);
598 if ( (0 != errno) || (to_line < 0) || (t != p) )
604 else /* one number means "match this line only" */
607 else /* default to 0-max */
617 case '\0': /* found EOL */
619 /* fall through to '/' */
620 case '/': /* found a definition separator */
623 case 4: /* within a log level */
626 level = get_type ((const char *) start);
627 if ( (GNUNET_ERROR_TYPE_INVALID == level) ||
628 (GNUNET_ERROR_TYPE_UNSPECIFIED == level) ||
629 (0 != add_definition (comp, file, function, from_line, to_line,
651 * Utility function - parses GNUNET_LOG and GNUNET_FORCE_LOG.
654 parse_all_definitions ()
656 if (GNUNET_NO == gnunet_log_parsed)
657 parse_definitions ("GNUNET_LOG", 0);
658 gnunet_log_parsed = GNUNET_YES;
659 if (GNUNET_NO == gnunet_force_log_parsed)
660 gnunet_force_log_present =
661 parse_definitions ("GNUNET_FORCE_LOG", 1) > 0 ? GNUNET_YES : GNUNET_NO;
662 gnunet_force_log_parsed = GNUNET_YES;
670 * @param comp default component to use
671 * @param loglevel what types of messages should be logged
672 * @param logfile which file to write log messages to (can be NULL)
673 * @return #GNUNET_OK on success
676 GNUNET_log_setup (const char *comp,
677 const char *loglevel,
680 const char *env_logfile;
684 min_level = get_type (loglevel);
685 #if !defined(GNUNET_CULL_LOGGING)
686 parse_all_definitions ();
689 QueryPerformanceFrequency (&performance_frequency);
691 GNUNET_free_non_null (component);
692 GNUNET_asprintf (&component, "%s-%d", comp, getpid ());
693 GNUNET_free_non_null (component_nopid);
694 component_nopid = GNUNET_strdup (comp);
696 env_logfile = getenv ("GNUNET_FORCE_LOGFILE");
697 if ((NULL != env_logfile) && (strlen (env_logfile) > 0))
698 logfile = env_logfile;
701 GNUNET_free_non_null (log_file_name);
702 log_file_name = GNUNET_STRINGS_filename_expand (logfile);
703 if (NULL == log_file_name)
704 return GNUNET_SYSERR;
707 return setup_log_file (tm);
712 * Add a custom logger. Note that installing any custom logger
713 * will disable the standard logger. When multiple custom loggers
714 * are installed, all will be called. The standard logger will
715 * only be used if no custom loggers are present.
717 * @param logger log function
718 * @param logger_cls closure for @a logger
721 GNUNET_logger_add (GNUNET_Logger logger,
724 struct CustomLogger *entry;
726 entry = GNUNET_new (struct CustomLogger);
727 entry->logger = logger;
728 entry->logger_cls = logger_cls;
729 entry->next = loggers;
735 * Remove a custom logger.
737 * @param logger log function
738 * @param logger_cls closure for @a logger
741 GNUNET_logger_remove (GNUNET_Logger logger,
744 struct CustomLogger *pos;
745 struct CustomLogger *prev;
749 while ((NULL != pos) &&
750 ((pos->logger != logger) || (pos->logger_cls != logger_cls)))
755 GNUNET_assert (NULL != pos);
759 prev->next = pos->next;
764 CRITICAL_SECTION output_message_cs;
769 * Actually output the log message.
771 * @param kind how severe was the issue
772 * @param comp component responsible
773 * @param datestr current date/time
774 * @param msg the actual message
777 output_message (enum GNUNET_ErrorType kind,
782 struct CustomLogger *pos;
785 EnterCriticalSection (&output_message_cs);
787 /* only use the standard logger if no custom loggers are present */
788 if ( (NULL != GNUNET_stderr) &&
791 FPRINTF (GNUNET_stderr,
795 GNUNET_error_type_to_string (kind),
797 fflush (GNUNET_stderr);
802 pos->logger (pos->logger_cls, kind, comp, datestr, msg);
806 LeaveCriticalSection (&output_message_cs);
812 * Flush an existing bulk report to the output.
814 * @param datestr our current timestamp
817 flush_bulk (const char *datestr)
819 char msg[DATE_STR_SIZE + BULK_TRACK_SIZE + 256];
824 if ((0 == last_bulk_time.abs_value_us) || (0 == last_bulk_repeat))
827 last = memchr (last_bulk, '\0', BULK_TRACK_SIZE);
829 last = &last_bulk[BULK_TRACK_SIZE - 1];
830 else if (last != last_bulk)
837 ft = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration
838 (last_bulk_time), GNUNET_YES);
839 snprintf (msg, sizeof (msg),
840 _("Message `%.*s' repeated %u times in the last %s\n"),
841 BULK_TRACK_SIZE, last_bulk, last_bulk_repeat, ft);
844 output_message (last_bulk_kind, last_bulk_comp, datestr, msg);
845 last_bulk_time = GNUNET_TIME_absolute_get ();
846 last_bulk_repeat = 0;
851 * Ignore the next n calls to the log function.
853 * @param n number of log calls to ignore (could be negative)
854 * @param check_reset #GNUNET_YES to assert that the log skip counter is currently zero
857 GNUNET_log_skip (int n,
864 ok = (0 == skip_log);
877 * Get the number of log calls that are going to be skipped
879 * @return number of log calls to be ignored
882 GNUNET_get_log_skip ()
889 * Output a log message using the default mechanism.
891 * @param kind how severe was the issue
892 * @param comp component responsible
893 * @param message the actual message
894 * @param va arguments to the format string "message"
897 mylog (enum GNUNET_ErrorType kind,
902 char date[DATE_STR_SIZE];
903 char date2[DATE_STR_SIZE];
909 size = VSNPRINTF (NULL, 0, message, vacp) + 1;
910 GNUNET_assert (0 != size);
912 memset (date, 0, DATE_STR_SIZE);
920 offset = GNUNET_TIME_get_offset ();
922 timetmp += offset / 1000;
923 tmptr = localtime (&timetmp);
925 QueryPerformanceCounter (&pc);
928 strcpy (date, "localtime error");
932 strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%020llu", tmptr);
933 snprintf (date, sizeof (date), date2,
934 (long long) (pc.QuadPart /
935 (performance_frequency.QuadPart / 1000)));
938 struct timeval timeofday;
940 gettimeofday (&timeofday, NULL);
941 offset = GNUNET_TIME_get_offset ();
944 timeofday.tv_sec += offset / 1000LL;
945 timeofday.tv_usec += (offset % 1000LL) * 1000LL;
946 if (timeofday.tv_usec > 1000000LL)
948 timeofday.tv_usec -= 1000000LL;
954 timeofday.tv_sec += offset / 1000LL;
955 if (timeofday.tv_usec > - (offset % 1000LL) * 1000LL)
957 timeofday.tv_usec += (offset % 1000LL) * 1000LL;
961 timeofday.tv_usec += 1000000LL + (offset % 1000LL) * 1000LL;
965 tmptr = localtime (&timeofday.tv_sec);
968 strcpy (date, "localtime error");
972 strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%06u", tmptr);
973 snprintf (date, sizeof (date), date2, timeofday.tv_usec);
976 VSNPRINTF (buf, size, message, va);
978 (void) setup_log_file (tmptr);
979 if ((0 != (kind & GNUNET_ERROR_TYPE_BULK)) &&
980 (0 != last_bulk_time.abs_value_us) &&
981 (0 == strncmp (buf, last_bulk, sizeof (last_bulk))))
984 if ( (GNUNET_TIME_absolute_get_duration (last_bulk_time).rel_value_us >
985 BULK_DELAY_THRESHOLD) ||
986 (last_bulk_repeat > BULK_REPEAT_THRESHOLD) )
991 strncpy (last_bulk, buf, sizeof (last_bulk));
992 last_bulk_repeat = 0;
993 last_bulk_kind = kind;
994 last_bulk_time = GNUNET_TIME_absolute_get ();
995 strncpy (last_bulk_comp, comp, COMP_TRACK_SIZE);
996 output_message (kind, comp, date, buf);
1002 * Main log function.
1004 * @param kind how serious is the error?
1005 * @param message what is the message (format string)
1006 * @param ... arguments for format string
1009 GNUNET_log_nocheck (enum GNUNET_ErrorType kind,
1010 const char *message, ...)
1014 va_start (va, message);
1015 mylog (kind, component, message, va);
1021 * Log function that specifies an alternative component.
1022 * This function should be used by plugins.
1024 * @param kind how serious is the error?
1025 * @param comp component responsible for generating the message
1026 * @param message what is the message (format string)
1027 * @param ... arguments for format string
1030 GNUNET_log_from_nocheck (enum GNUNET_ErrorType kind, const char *comp,
1031 const char *message, ...)
1034 char comp_w_pid[128];
1037 comp = component_nopid;
1039 va_start (va, message);
1040 GNUNET_snprintf (comp_w_pid, sizeof (comp_w_pid), "%s-%d", comp, getpid ());
1041 mylog (kind, comp_w_pid, message, va);
1047 * Convert error type to string.
1049 * @param kind type to convert
1050 * @return string corresponding to the type
1053 GNUNET_error_type_to_string (enum GNUNET_ErrorType kind)
1055 if ((kind & GNUNET_ERROR_TYPE_ERROR) > 0)
1057 if ((kind & GNUNET_ERROR_TYPE_WARNING) > 0)
1058 return _("WARNING");
1059 if ((kind & GNUNET_ERROR_TYPE_INFO) > 0)
1061 if ((kind & GNUNET_ERROR_TYPE_DEBUG) > 0)
1063 if ((kind & ~GNUNET_ERROR_TYPE_BULK) == 0)
1065 return _("INVALID");
1070 * Convert a hash to a string (for printing debug messages).
1071 * This is one of the very few calls in the entire API that is
1074 * @param hc the hash code
1075 * @return string form; will be overwritten by next call to GNUNET_h2s.
1078 GNUNET_h2s (const struct GNUNET_HashCode * hc)
1080 static struct GNUNET_CRYPTO_HashAsciiEncoded ret;
1082 GNUNET_CRYPTO_hash_to_enc (hc, &ret);
1083 ret.encoding[8] = '\0';
1084 return (const char *) ret.encoding;
1089 * Convert a hash to a string (for printing debug messages).
1090 * This is one of the very few calls in the entire API that is
1093 * @param hc the hash code
1094 * @return string form; will be overwritten by next call to GNUNET_h2s_full.
1097 GNUNET_h2s_full (const struct GNUNET_HashCode * hc)
1099 static struct GNUNET_CRYPTO_HashAsciiEncoded ret;
1101 GNUNET_CRYPTO_hash_to_enc (hc, &ret);
1102 ret.encoding[sizeof (ret) - 1] = '\0';
1103 return (const char *) ret.encoding;
1108 * Convert a peer identity to a string (for printing debug messages).
1109 * This is one of the very few calls in the entire API that is
1112 * @param pid the peer identity
1113 * @return string form of the pid; will be overwritten by next
1114 * call to #GNUNET_i2s.
1117 GNUNET_i2s (const struct GNUNET_PeerIdentity *pid)
1119 static char buf[256];
1122 ret = GNUNET_CRYPTO_eddsa_public_key_to_string (&pid->public_key);
1131 * Convert a peer identity to a string (for printing debug messages).
1132 * This is one of the very few calls in the entire API that is
1135 * @param pid the peer identity
1136 * @return string form of the pid; will be overwritten by next
1137 * call to #GNUNET_i2s_full.
1140 GNUNET_i2s_full (const struct GNUNET_PeerIdentity *pid)
1142 static char buf[256];
1145 ret = GNUNET_CRYPTO_eddsa_public_key_to_string (&pid->public_key);
1153 * Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string
1154 * (for printing debug messages). This is one of the very few calls
1155 * in the entire API that is NOT reentrant!
1157 * @param addr the address
1158 * @param addrlen the length of the address in @a addr
1159 * @return nicely formatted string for the address
1160 * will be overwritten by next call to #GNUNET_a2s.
1163 GNUNET_a2s (const struct sockaddr *addr,
1167 #define LEN GNUNET_MAX ((INET6_ADDRSTRLEN + 8), \
1168 (1 + sizeof (struct sockaddr_un) - sizeof (sa_family_t)))
1170 #define LEN (INET6_ADDRSTRLEN + 8)
1172 static char buf[LEN];
1175 const struct sockaddr_in *v4;
1176 const struct sockaddr_un *un;
1177 const struct sockaddr_in6 *v6;
1181 return _("unknown address");
1182 switch (addr->sa_family)
1185 if (addrlen != sizeof (struct sockaddr_in))
1186 return "<invalid v4 address>";
1187 v4 = (const struct sockaddr_in *) addr;
1188 inet_ntop (AF_INET, &v4->sin_addr, buf, INET_ADDRSTRLEN);
1189 if (0 == ntohs (v4->sin_port))
1192 GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v4->sin_port));
1196 if (addrlen != sizeof (struct sockaddr_in6))
1197 return "<invalid v4 address>";
1198 v6 = (const struct sockaddr_in6 *) addr;
1200 inet_ntop (AF_INET6, &v6->sin6_addr, &buf[1], INET6_ADDRSTRLEN);
1201 if (0 == ntohs (v6->sin6_port))
1204 GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v6->sin6_port));
1208 if (addrlen <= sizeof (sa_family_t))
1209 return "<unbound UNIX client>";
1210 un = (const struct sockaddr_un *) addr;
1212 if ('\0' == un->sun_path[0])
1214 memset (buf, 0, sizeof (buf));
1215 GNUNET_snprintf (buf,
1218 (1 == off) ? "@" : "",
1219 (int) (addrlen - sizeof (sa_family_t) - off),
1220 &un->sun_path[off]);
1223 return _("invalid address");
1229 * Log error message about missing configuration option.
1231 * @param kind log level
1232 * @param section section with missing option
1233 * @param option name of missing option
1236 GNUNET_log_config_missing (enum GNUNET_ErrorType kind,
1237 const char *section,
1241 _("Configuration fails to specify option `%s' in section `%s'!\n"),
1248 * Log error message about invalid configuration option value.
1250 * @param kind log level
1251 * @param section section with invalid option
1252 * @param option name of invalid option
1253 * @param required what is required that is invalid about the option
1256 GNUNET_log_config_invalid (enum GNUNET_ErrorType kind,
1257 const char *section,
1259 const char *required)
1262 _("Configuration specifies invalid value for option `%s' in section `%s': %s\n"),
1263 option, section, required);
1270 void __attribute__ ((constructor))
1271 GNUNET_util_cl_init ()
1273 GNUNET_stderr = stderr;
1275 GNInitWinEnv (NULL);
1278 if (!InitializeCriticalSectionAndSpinCount (&output_message_cs, 0x00000400))
1287 void __attribute__ ((destructor))
1288 GNUNET_util_cl_fini ()
1291 DeleteCriticalSection (&output_message_cs);
1294 GNShutdownWinEnv ();
1298 /* end of common_logging.c */