2 This file is part of GNUnet.
3 (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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file util/common_logging.c
23 * @brief error handling API
25 * @author Christian Grothoff
28 #include "gnunet_util_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.
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];
338 if (NULL == log_file_name)
339 return GNUNET_SYSERR;
340 if (0 == strftime (fn, sizeof (fn), log_file_name, tm))
341 return GNUNET_SYSERR;
342 leftsquare = strrchr (fn, '[');
343 if ( (NULL != leftsquare) && (']' == leftsquare[1]) )
345 char *logfile_copy = GNUNET_strdup (fn);
346 logfile_copy[leftsquare - fn] = '\0';
347 logfile_copy[leftsquare - fn + 1] = '\0';
348 snprintf (fn, PATH_MAX, "%s%d%s",
349 logfile_copy, getpid (), &logfile_copy[leftsquare - fn + 2]);
350 GNUNET_free (logfile_copy);
352 if (0 == strcmp (fn, last_fn))
353 return GNUNET_OK; /* no change */
354 log_rotate (last_fn);
355 strcpy (last_fn, fn);
356 dirwarn = (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn));
358 altlog_fd = OPEN (fn, O_APPEND |
361 _S_IREAD | _S_IWRITE);
363 altlog_fd = OPEN (fn, O_APPEND |
365 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
369 if (NULL != GNUNET_stderr)
370 fclose (GNUNET_stderr);
371 dup_return = dup2 (altlog_fd, 2);
372 (void) close (altlog_fd);
373 if (-1 != dup_return)
375 altlog = fdopen (2, "ab");
389 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", fn);
391 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
392 _("Failed to create or access directory for log file `%s'\n"),
394 return GNUNET_SYSERR;
396 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 (char *component, char *file, char *function, int from_line,
414 int to_line, int level, int force)
419 if (logdefs_size == logdefs_len)
421 memset (&n, 0, sizeof (n));
422 if (0 == strlen (component))
423 component = (char *) ".*";
424 r = regcomp (&n.component_regex, (const char *) component, REG_NOSUB);
429 if (0 == strlen (file))
430 file = (char *) ".*";
431 r = regcomp (&n.file_regex, (const char *) file, REG_NOSUB);
434 regfree (&n.component_regex);
437 if ((NULL == function) || (0 == strlen (function)))
438 function = (char *) ".*";
439 r = regcomp (&n.function_regex, (const char *) function, REG_NOSUB);
442 regfree (&n.component_regex);
443 regfree (&n.file_regex);
446 n.from_line = from_line;
450 logdefs[logdefs_len++] = n;
456 * Decides whether a particular logging call should or should not be allowed
457 * to be made. Used internally by GNUNET_log*()
459 * @param caller_level loglevel the caller wants to use
460 * @param comp component name the caller uses (NULL means that global
461 * component name is used)
462 * @param file file name containing the logging call, usually __FILE__
463 * @param function function which tries to make a logging call,
464 * usually __FUNCTION__
465 * @param line line at which the call is made, usually __LINE__
466 * @return 0 to disallow the call, 1 to allow it
469 GNUNET_get_log_call_status (int caller_level, const char *comp,
470 const char *file, const char *function, int line)
477 /* Use default component */
478 comp = component_nopid;
480 /* We have no definitions to override globally configured log level,
481 * so just use it right away.
483 if ( (min_level >= 0) && (GNUNET_NO == gnunet_force_log_present) )
484 return caller_level <= min_level;
486 /* Only look for forced definitions? */
487 force_only = min_level >= 0;
488 for (i = 0; i < logdefs_len; i++)
491 if (( (!force_only) || ld->force) &&
492 (line >= ld->from_line && line <= ld->to_line) &&
493 (0 == regexec (&ld->component_regex, comp, 0, NULL, 0)) &&
494 (0 == regexec (&ld->file_regex, file, 0, NULL, 0)) &&
495 (0 == regexec (&ld->function_regex, function, 0, NULL, 0)))
498 return caller_level <= ld->level;
501 /* No matches - use global level, if defined */
503 return caller_level <= min_level;
504 /* All programs/services previously defaulted to WARNING.
505 * Now WE default to WARNING, and THEY default to NULL.
507 return caller_level <= GNUNET_ERROR_TYPE_WARNING;
512 * Utility function - parses a definition
515 * component;file;function;from_line-to_line;level[/component...]
516 * All entries are mandatory, but may be empty.
517 * Empty entries for component, file and function are treated as
518 * "matches anything".
519 * Empty line entry is treated as "from 0 to INT_MAX"
520 * Line entry with only one line is treated as "this line only"
521 * Entry for level MUST NOT be empty.
522 * Entries for component, file and function that consist of a
523 * single character "*" are treated (at the moment) the same way
524 * empty entries are treated (wildcard matching is not implemented (yet?)).
525 * file entry is matched to the end of __FILE__. That is, it might be
526 * a base name, or a base name with leading directory names (some compilers
527 * define __FILE__ to absolute file path).
529 * @param constname name of the environment variable from which to get the
530 * string to be parsed
531 * @param force 1 if definitions found in constname are to be forced
532 * @return number of added definitions
535 parse_definitions (const char *constname, int force)
541 char *function = NULL;
547 int from_line, to_line;
549 int keep_looking = 1;
551 tmp = getenv (constname);
554 def = GNUNET_strdup (tmp);
557 for (p = def, state = 0, start = def; keep_looking; p++)
561 case ';': /* found a field separator */
565 case 0: /* within a component name */
568 case 1: /* within a file name */
571 case 2: /* within a function name */
572 /* after a file name there must be a function name */
575 case 3: /* within a from-to line range */
576 if (strlen (start) > 0)
579 from_line = strtol (start, &t, 10);
580 if ( (0 != errno) || (from_line < 0) )
585 if ( (t < p) && ('-' == t[0]) )
589 to_line = strtol (start, &t, 10);
590 if ( (0 != errno) || (to_line < 0) || (t != p) )
596 else /* one number means "match this line only" */
599 else /* default to 0-max */
609 case '\0': /* found EOL */
611 /* fall through to '/' */
612 case '/': /* found a definition separator */
615 case 4: /* within a log level */
618 level = get_type ((const char *) start);
619 if ( (GNUNET_ERROR_TYPE_INVALID == level) ||
620 (GNUNET_ERROR_TYPE_UNSPECIFIED == level) ||
621 (0 != add_definition (comp, file, function, from_line, to_line,
643 * Utility function - parses GNUNET_LOG and GNUNET_FORCE_LOG.
646 parse_all_definitions ()
648 if (GNUNET_NO == gnunet_log_parsed)
649 parse_definitions ("GNUNET_LOG", 0);
650 gnunet_log_parsed = GNUNET_YES;
651 if (GNUNET_NO == gnunet_force_log_parsed)
652 gnunet_force_log_present =
653 parse_definitions ("GNUNET_FORCE_LOG", 1) > 0 ? GNUNET_YES : GNUNET_NO;
654 gnunet_force_log_parsed = GNUNET_YES;
662 * @param comp default component to use
663 * @param loglevel what types of messages should be logged
664 * @param logfile which file to write log messages to (can be NULL)
665 * @return #GNUNET_OK on success
668 GNUNET_log_setup (const char *comp,
669 const char *loglevel,
672 const char *env_logfile;
676 min_level = get_type (loglevel);
677 #if !defined(GNUNET_CULL_LOGGING)
678 parse_all_definitions ();
681 QueryPerformanceFrequency (&performance_frequency);
683 GNUNET_free_non_null (component);
684 GNUNET_asprintf (&component, "%s-%d", comp, getpid ());
685 GNUNET_free_non_null (component_nopid);
686 component_nopid = GNUNET_strdup (comp);
688 env_logfile = getenv ("GNUNET_FORCE_LOGFILE");
689 if ((NULL != env_logfile) && (strlen (env_logfile) > 0))
690 logfile = env_logfile;
693 GNUNET_free_non_null (log_file_name);
694 log_file_name = GNUNET_STRINGS_filename_expand (logfile);
695 if (NULL == log_file_name)
696 return GNUNET_SYSERR;
699 return setup_log_file (tm);
704 * Add a custom logger.
706 * @param logger log function
707 * @param logger_cls closure for logger
710 GNUNET_logger_add (GNUNET_Logger logger, void *logger_cls)
712 struct CustomLogger *entry;
714 entry = GNUNET_malloc (sizeof (struct CustomLogger));
715 entry->logger = logger;
716 entry->logger_cls = logger_cls;
717 entry->next = loggers;
723 * Remove a custom logger.
725 * @param logger log function
726 * @param logger_cls closure for logger
729 GNUNET_logger_remove (GNUNET_Logger logger, void *logger_cls)
731 struct CustomLogger *pos;
732 struct CustomLogger *prev;
736 while ((pos != NULL) &&
737 ((pos->logger != logger) || (pos->logger_cls != logger_cls)))
742 GNUNET_assert (pos != NULL);
746 prev->next = pos->next;
751 CRITICAL_SECTION output_message_cs;
756 * Actually output the log message.
758 * @param kind how severe was the issue
759 * @param comp component responsible
760 * @param datestr current date/time
761 * @param msg the actual message
764 output_message (enum GNUNET_ErrorType kind, const char *comp,
765 const char *datestr, const char *msg)
767 struct CustomLogger *pos;
769 EnterCriticalSection (&output_message_cs);
771 if (NULL != GNUNET_stderr)
773 FPRINTF (GNUNET_stderr, "%s %s %s %s", datestr, comp,
774 GNUNET_error_type_to_string (kind), msg);
775 fflush (GNUNET_stderr);
780 pos->logger (pos->logger_cls, kind, comp, datestr, msg);
784 LeaveCriticalSection (&output_message_cs);
790 * Flush an existing bulk report to the output.
792 * @param datestr our current timestamp
795 flush_bulk (const char *datestr)
797 char msg[DATE_STR_SIZE + BULK_TRACK_SIZE + 256];
802 if ((0 == last_bulk_time.abs_value_us) || (0 == last_bulk_repeat))
805 last = memchr (last_bulk, '\0', BULK_TRACK_SIZE);
807 last = &last_bulk[BULK_TRACK_SIZE - 1];
808 else if (last != last_bulk)
815 ft = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration
816 (last_bulk_time), GNUNET_YES);
817 snprintf (msg, sizeof (msg),
818 _("Message `%.*s' repeated %u times in the last %s\n"),
819 BULK_TRACK_SIZE, last_bulk, last_bulk_repeat, ft);
822 output_message (last_bulk_kind, last_bulk_comp, datestr, msg);
823 last_bulk_time = GNUNET_TIME_absolute_get ();
824 last_bulk_repeat = 0;
829 * Ignore the next n calls to the log function.
831 * @param n number of log calls to ignore (could be negative)
832 * @param check_reset #GNUNET_YES to assert that the log skip counter is currently zero
835 GNUNET_log_skip (int n,
842 ok = (0 == skip_log);
855 * Get the number of log calls that are going to be skipped
857 * @return number of log calls to be ignored
860 GNUNET_get_log_skip ()
867 * Output a log message using the default mechanism.
869 * @param kind how severe was the issue
870 * @param comp component responsible
871 * @param message the actual message
872 * @param va arguments to the format string "message"
875 mylog (enum GNUNET_ErrorType kind,
880 char date[DATE_STR_SIZE];
881 char date2[DATE_STR_SIZE];
887 size = VSNPRINTF (NULL, 0, message, vacp) + 1;
888 GNUNET_assert (0 != size);
890 memset (date, 0, DATE_STR_SIZE);
898 offset = GNUNET_TIME_get_offset ();
900 timetmp += offset / 1000;
901 tmptr = localtime (&timetmp);
903 QueryPerformanceCounter (&pc);
906 strcpy (date, "localtime error");
910 strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%020llu", tmptr);
911 snprintf (date, sizeof (date), date2,
912 (long long) (pc.QuadPart /
913 (performance_frequency.QuadPart / 1000)));
916 struct timeval timeofday;
918 gettimeofday (&timeofday, NULL);
919 offset = GNUNET_TIME_get_offset ();
922 timeofday.tv_sec += offset / 1000LL;
923 timeofday.tv_usec += (offset % 1000LL) * 1000LL;
924 if (timeofday.tv_usec > 1000000LL)
926 timeofday.tv_usec -= 1000000LL;
932 timeofday.tv_sec += offset / 1000LL;
933 if (timeofday.tv_usec > - (offset % 1000LL) * 1000LL)
935 timeofday.tv_usec += (offset % 1000LL) * 1000LL;
939 timeofday.tv_usec += 1000000LL + (offset % 1000LL) * 1000LL;
943 tmptr = localtime (&timeofday.tv_sec);
946 strcpy (date, "localtime error");
950 strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%06u", tmptr);
951 snprintf (date, sizeof (date), date2, timeofday.tv_usec);
954 VSNPRINTF (buf, size, message, va);
956 (void) setup_log_file (tmptr);
957 if ((0 != (kind & GNUNET_ERROR_TYPE_BULK)) &&
958 (0 != last_bulk_time.abs_value_us) &&
959 (0 == strncmp (buf, last_bulk, sizeof (last_bulk))))
962 if ( (GNUNET_TIME_absolute_get_duration (last_bulk_time).rel_value_us >
963 BULK_DELAY_THRESHOLD) ||
964 (last_bulk_repeat > BULK_REPEAT_THRESHOLD) )
969 strncpy (last_bulk, buf, sizeof (last_bulk));
970 last_bulk_repeat = 0;
971 last_bulk_kind = kind;
972 last_bulk_time = GNUNET_TIME_absolute_get ();
973 strncpy (last_bulk_comp, comp, COMP_TRACK_SIZE);
974 output_message (kind, comp, date, buf);
982 * @param kind how serious is the error?
983 * @param message what is the message (format string)
984 * @param ... arguments for format string
987 GNUNET_log_nocheck (enum GNUNET_ErrorType kind,
988 const char *message, ...)
992 va_start (va, message);
993 mylog (kind, component, message, va);
999 * Log function that specifies an alternative component.
1000 * This function should be used by plugins.
1002 * @param kind how serious is the error?
1003 * @param comp component responsible for generating the message
1004 * @param message what is the message (format string)
1005 * @param ... arguments for format string
1008 GNUNET_log_from_nocheck (enum GNUNET_ErrorType kind, const char *comp,
1009 const char *message, ...)
1012 char comp_w_pid[128];
1015 comp = component_nopid;
1017 va_start (va, message);
1018 GNUNET_snprintf (comp_w_pid, sizeof (comp_w_pid), "%s-%d", comp, getpid ());
1019 mylog (kind, comp_w_pid, message, va);
1025 * Convert error type to string.
1027 * @param kind type to convert
1028 * @return string corresponding to the type
1031 GNUNET_error_type_to_string (enum GNUNET_ErrorType kind)
1033 if ((kind & GNUNET_ERROR_TYPE_ERROR) > 0)
1035 if ((kind & GNUNET_ERROR_TYPE_WARNING) > 0)
1036 return _("WARNING");
1037 if ((kind & GNUNET_ERROR_TYPE_INFO) > 0)
1039 if ((kind & GNUNET_ERROR_TYPE_DEBUG) > 0)
1041 if ((kind & ~GNUNET_ERROR_TYPE_BULK) == 0)
1043 return _("INVALID");
1048 * Convert a hash to a string (for printing debug messages).
1049 * This is one of the very few calls in the entire API that is
1052 * @param hc the hash code
1053 * @return string form; will be overwritten by next call to GNUNET_h2s.
1056 GNUNET_h2s (const struct GNUNET_HashCode * hc)
1058 static struct GNUNET_CRYPTO_HashAsciiEncoded ret;
1060 GNUNET_CRYPTO_hash_to_enc (hc, &ret);
1061 ret.encoding[8] = '\0';
1062 return (const char *) ret.encoding;
1067 * Convert a hash to a string (for printing debug messages).
1068 * This is one of the very few calls in the entire API that is
1071 * @param hc the hash code
1072 * @return string form; will be overwritten by next call to GNUNET_h2s_full.
1075 GNUNET_h2s_full (const struct GNUNET_HashCode * hc)
1077 static struct GNUNET_CRYPTO_HashAsciiEncoded ret;
1079 GNUNET_CRYPTO_hash_to_enc (hc, &ret);
1080 ret.encoding[sizeof (ret) - 1] = '\0';
1081 return (const char *) ret.encoding;
1086 * Convert a peer identity to a string (for printing debug messages).
1087 * This is one of the very few calls in the entire API that is
1090 * @param pid the peer identity
1091 * @return string form of the pid; will be overwritten by next
1092 * call to #GNUNET_i2s.
1095 GNUNET_i2s (const struct GNUNET_PeerIdentity *pid)
1097 static char buf[256];
1100 ret = GNUNET_CRYPTO_ecc_public_sign_key_to_string (&pid->public_key);
1109 * Convert a peer identity to a string (for printing debug messages).
1110 * This is one of the very few calls in the entire API that is
1113 * @param pid the peer identity
1114 * @return string form of the pid; will be overwritten by next
1115 * call to #GNUNET_i2s_full.
1118 GNUNET_i2s_full (const struct GNUNET_PeerIdentity *pid)
1120 static char buf[256];
1123 ret = GNUNET_CRYPTO_ecc_public_sign_key_to_string (&pid->public_key);
1131 * Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string
1132 * (for printing debug messages). This is one of the very few calls
1133 * in the entire API that is NOT reentrant!
1135 * @param addr the address
1136 * @param addrlen the length of the address
1137 * @return nicely formatted string for the address
1138 * will be overwritten by next call to GNUNET_a2s.
1141 GNUNET_a2s (const struct sockaddr *addr, socklen_t addrlen)
1143 static char buf[INET6_ADDRSTRLEN + 8];
1145 const struct sockaddr_in *v4;
1146 const struct sockaddr_un *un;
1147 const struct sockaddr_in6 *v6;
1151 return _("unknown address");
1152 switch (addr->sa_family)
1155 if (addrlen != sizeof (struct sockaddr_in))
1156 return "<invalid v4 address>";
1157 v4 = (const struct sockaddr_in *) addr;
1158 inet_ntop (AF_INET, &v4->sin_addr, buf, INET_ADDRSTRLEN);
1159 if (0 == ntohs (v4->sin_port))
1162 GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v4->sin_port));
1166 if (addrlen != sizeof (struct sockaddr_in6))
1167 return "<invalid v4 address>";
1168 v6 = (const struct sockaddr_in6 *) addr;
1170 inet_ntop (AF_INET6, &v6->sin6_addr, &buf[1], INET6_ADDRSTRLEN);
1171 if (0 == ntohs (v6->sin6_port))
1174 GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v6->sin6_port));
1178 if (addrlen <= sizeof (sa_family_t))
1179 return "<unbound UNIX client>";
1180 un = (const struct sockaddr_un *) addr;
1182 if (un->sun_path[0] == '\0')
1184 memset (buf, 0, sizeof (buf));
1185 snprintf (buf, sizeof (buf) - 1, "%s%.*s", (off == 1) ? "@" : "",
1186 (int) (addrlen - sizeof (sa_family_t) - 1 - off),
1187 &un->sun_path[off]);
1190 return _("invalid address");
1196 * Log error message about missing configuration option.
1198 * @param kind log level
1199 * @param section section with missing option
1200 * @param option name of missing option
1203 GNUNET_log_config_missing (enum GNUNET_ErrorType kind,
1204 const char *section,
1208 _("Configuration fails to specify option `%s' in section `%s'!\n"),
1215 * Log error message about invalid configuration option value.
1217 * @param kind log level
1218 * @param section section with invalid option
1219 * @param option name of invalid option
1220 * @param required what is required that is invalid about the option
1223 GNUNET_log_config_invalid (enum GNUNET_ErrorType kind,
1224 const char *section,
1226 const char *required)
1229 _("Configuration specifies invalid value for option `%s' in section `%s': %s\n"),
1230 option, section, required);
1237 void __attribute__ ((constructor)) GNUNET_util_cl_init ()
1239 GNUNET_stderr = stderr;
1241 GNInitWinEnv (NULL);
1244 if (!InitializeCriticalSectionAndSpinCount (&output_message_cs, 0x00000400))
\r
1253 void __attribute__ ((destructor)) GNUNET_util_cl_fini ()
1256 DeleteCriticalSection (&output_message_cs);
1259 GNShutdownWinEnv ();
1263 /* end of common_logging.c */