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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file util/common_logging.c
21 * @brief error handling API
22 * @author Christian Grothoff
25 #include "gnunet_crypto_lib.h"
26 #include "gnunet_disk_lib.h"
27 #include "gnunet_strings_lib.h"
32 * After how many milliseconds do we always print
33 * that "message X was repeated N times"? Use 12h.
35 #define BULK_DELAY_THRESHOLD (12 * 60 * 60 * 1000LL * 1000LL)
38 * After how many repetitions do we always print
39 * that "message X was repeated N times"? (even if
40 * we have not yet reached the delay threshold)
42 #define BULK_REPEAT_THRESHOLD 1000
45 * How many characters do we use for matching of
48 #define BULK_TRACK_SIZE 256
51 * How many characters do we use for matching of
54 #define COMP_TRACK_SIZE 32
57 * How many characters can a date/time string
60 #define DATE_STR_SIZE 64
63 * How many log files to keep?
65 #define ROTATION_KEEP 3
69 * Assumed maximum path length (for the log file name).
76 * Linked list of active loggers.
81 * This is a linked list.
83 struct CustomLogger *next;
97 * The last "bulk" error message that we have been logging.
98 * Note that this message maybe truncated to the first BULK_TRACK_SIZE
99 * characters, in which case it is NOT 0-terminated!
101 static char last_bulk[BULK_TRACK_SIZE];
104 * Type of the last bulk message.
106 static enum GNUNET_ErrorType last_bulk_kind;
109 * Time of the last bulk error message (0 for none)
111 static struct GNUNET_TIME_Absolute last_bulk_time;
114 * Number of times that bulk message has been repeated since.
116 static unsigned int last_bulk_repeat;
119 * Component when the last bulk was logged. Will be 0-terminated.
121 static char last_bulk_comp[COMP_TRACK_SIZE + 1];
126 static char *component;
129 * Running component (without pid).
131 static char *component_nopid;
134 * Format string describing the name of the log file.
136 static char *log_file_name;
141 static enum GNUNET_ErrorType min_level;
144 * Linked list of our custom loggres.
146 static struct CustomLogger *loggers;
149 * Number of log calls to ignore.
151 static int skip_log = 0;
154 * File descriptor to use for "stderr", or NULL for none.
156 static FILE *GNUNET_stderr;
159 * Represents a single logging definition
164 * Component name regex
166 regex_t component_regex;
174 * Function name regex
176 regex_t function_regex;
179 * Lowest line at which this definition matches.
180 * Defaults to 0. Must be <= to_line.
185 * Highest line at which this definition matches.
186 * Defaults to INT_MAX. Must be >= from_line.
191 * Maximal log level allowed for calls that match this definition.
192 * Calls with higher log level will be disabled.
198 * 1 if this definition comes from GNUNET_FORCE_LOG, which means that it
199 * overrides any configuration options. 0 otherwise.
205 #if !defined(GNUNET_CULL_LOGGING)
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;
239 * Contains the number of performance counts per second.
241 static LARGE_INTEGER performance_frequency;
246 * Convert a textual description of a loglevel
247 * to the respective GNUNET_GE_KIND.
249 * @param log loglevel to parse
250 * @return GNUNET_GE_INVALID if log does not parse
252 static enum GNUNET_ErrorType
253 get_type (const char *log)
256 return GNUNET_ERROR_TYPE_UNSPECIFIED;
257 if (0 == strcasecmp (log, _("DEBUG")))
258 return GNUNET_ERROR_TYPE_DEBUG;
259 if (0 == strcasecmp (log, _("INFO")))
260 return GNUNET_ERROR_TYPE_INFO;
261 if (0 == strcasecmp (log, _("MESSAGE")))
262 return GNUNET_ERROR_TYPE_MESSAGE;
263 if (0 == strcasecmp (log, _("WARNING")))
264 return GNUNET_ERROR_TYPE_WARNING;
265 if (0 == strcasecmp (log, _("ERROR")))
266 return GNUNET_ERROR_TYPE_ERROR;
267 if (0 == strcasecmp (log, _("NONE")))
268 return GNUNET_ERROR_TYPE_NONE;
269 return GNUNET_ERROR_TYPE_INVALID;
274 * Abort the process, generate a core dump if possible.
286 #if !defined(GNUNET_CULL_LOGGING)
288 * Utility function - reallocates logdefs array to be twice as large.
293 logdefs_size = (logdefs_size + 1) * 2;
294 logdefs = GNUNET_realloc (logdefs, logdefs_size * sizeof (struct LogDef));
298 #if ! TALER_WALLET_ONLY
300 * Rotate logs, deleting the oldest log.
302 * @param new_name new name to add to the rotation
305 log_rotate (const char *new_name)
307 static char *rotation[ROTATION_KEEP];
308 static unsigned int rotation_off;
311 if ('\0' == *new_name)
312 return; /* not a real log file name */
313 discard = rotation[rotation_off % ROTATION_KEEP];
316 /* Note: can't log errors during logging (recursion!), so this
317 operation MUST silently fail... */
318 (void) UNLINK (discard);
319 GNUNET_free (discard);
321 rotation[rotation_off % ROTATION_KEEP] = GNUNET_strdup (new_name);
327 * Setup the log file.
329 * @param tm timestamp for which we should setup logging
330 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
333 setup_log_file (const struct tm *tm)
335 static char last_fn[PATH_MAX + 1];
336 char fn[PATH_MAX + 1];
342 if (NULL == log_file_name)
343 return GNUNET_SYSERR;
344 if (0 == strftime (fn, sizeof (fn), log_file_name, tm))
345 return GNUNET_SYSERR;
346 leftsquare = strrchr (fn, '[');
347 if ( (NULL != leftsquare) && (']' == leftsquare[1]) )
349 char *logfile_copy = GNUNET_strdup (fn);
351 logfile_copy[leftsquare - fn] = '\0';
352 logfile_copy[leftsquare - fn + 1] = '\0';
358 &logfile_copy[leftsquare - fn + 2]);
359 GNUNET_free (logfile_copy);
361 if (0 == strcmp (fn, last_fn))
362 return GNUNET_OK; /* no change */
363 log_rotate (last_fn);
364 strcpy (last_fn, fn);
366 GNUNET_DISK_directory_create_for_file (fn))
369 "Failed to create directory for `%s': %s\n",
372 return GNUNET_SYSERR;
375 altlog_fd = OPEN (fn, O_APPEND |
378 _S_IREAD | _S_IWRITE);
380 altlog_fd = OPEN (fn, O_APPEND |
382 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
386 if (NULL != GNUNET_stderr)
387 fclose (GNUNET_stderr);
388 dup_return = dup2 (altlog_fd, 2);
389 (void) close (altlog_fd);
390 if (-1 != dup_return)
392 altlog = fdopen (2, "ab");
406 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", fn);
407 return GNUNET_SYSERR;
409 GNUNET_stderr = altlog;
416 * Utility function - adds a parsed definition to logdefs array.
418 * @param component see struct LogDef, can't be NULL
419 * @param file see struct LogDef, can't be NULL
420 * @param function see struct LogDef, can't be NULL
421 * @param from_line see struct LogDef
422 * @param to_line see struct LogDef
423 * @param level see struct LogDef, must be >= 0
424 * @param force see struct LogDef
425 * @return 0 on success, regex-specific error otherwise
428 add_definition (const char *component,
430 const char *function,
439 if (logdefs_size == logdefs_len)
441 memset (&n, 0, sizeof (n));
442 if (0 == strlen (component))
443 component = (char *) ".*";
444 r = regcomp (&n.component_regex, (const char *) component, REG_NOSUB);
449 if (0 == strlen (file))
450 file = (char *) ".*";
451 r = regcomp (&n.file_regex, (const char *) file, REG_NOSUB);
454 regfree (&n.component_regex);
457 if ((NULL == function) || (0 == strlen (function)))
458 function = (char *) ".*";
459 r = regcomp (&n.function_regex, (const char *) function, REG_NOSUB);
462 regfree (&n.component_regex);
463 regfree (&n.file_regex);
466 n.from_line = from_line;
470 logdefs[logdefs_len++] = n;
476 * Decides whether a particular logging call should or should not be allowed
477 * to be made. Used internally by GNUNET_log*()
479 * @param caller_level loglevel the caller wants to use
480 * @param comp component name the caller uses (NULL means that global
481 * component name is used)
482 * @param file file name containing the logging call, usually __FILE__
483 * @param function function which tries to make a logging call,
484 * usually __FUNCTION__
485 * @param line line at which the call is made, usually __LINE__
486 * @return 0 to disallow the call, 1 to allow it
489 GNUNET_get_log_call_status (int caller_level,
492 const char *function,
500 /* Use default component */
501 comp = component_nopid;
503 /* We have no definitions to override globally configured log level,
504 * so just use it right away.
506 if ( (min_level >= 0) && (GNUNET_NO == gnunet_force_log_present) )
507 return caller_level <= min_level;
509 /* Only look for forced definitions? */
510 force_only = min_level >= 0;
511 for (i = 0; i < logdefs_len; i++)
514 if (( (!force_only) || ld->force) &&
515 (line >= ld->from_line && line <= ld->to_line) &&
516 (0 == regexec (&ld->component_regex, comp, 0, NULL, 0)) &&
517 (0 == regexec (&ld->file_regex, file, 0, NULL, 0)) &&
518 (0 == regexec (&ld->function_regex, function, 0, NULL, 0)))
521 return caller_level <= ld->level;
524 /* No matches - use global level, if defined */
526 return caller_level <= min_level;
527 /* All programs/services previously defaulted to WARNING.
528 * Now *we* default to WARNING, and THEY default to NULL.
529 * Or rather we default to MESSAGE, since things aren't always bad.
531 return caller_level <= GNUNET_ERROR_TYPE_MESSAGE;
536 * Utility function - parses a definition
539 * component;file;function;from_line-to_line;level[/component...]
540 * All entries are mandatory, but may be empty.
541 * Empty entries for component, file and function are treated as
542 * "matches anything".
543 * Empty line entry is treated as "from 0 to INT_MAX"
544 * Line entry with only one line is treated as "this line only"
545 * Entry for level MUST NOT be empty.
546 * Entries for component, file and function that consist of a
547 * single character "*" are treated (at the moment) the same way
548 * empty entries are treated (wildcard matching is not implemented (yet?)).
549 * file entry is matched to the end of __FILE__. That is, it might be
550 * a base name, or a base name with leading directory names (some compilers
551 * define __FILE__ to absolute file path).
553 * @param constname name of the environment variable from which to get the
554 * string to be parsed
555 * @param force 1 if definitions found in constname are to be forced
556 * @return number of added definitions
559 parse_definitions (const char *constname, int force)
565 char *function = NULL;
571 int from_line, to_line;
573 int keep_looking = 1;
575 tmp = getenv (constname);
578 def = GNUNET_strdup (tmp);
581 for (p = def, state = 0, start = def; keep_looking; p++)
585 case ';': /* found a field separator */
589 case 0: /* within a component name */
592 case 1: /* within a file name */
595 case 2: /* within a function name */
596 /* after a file name there must be a function name */
599 case 3: /* within a from-to line range */
600 if (strlen (start) > 0)
603 from_line = strtol (start, &t, 10);
604 if ( (0 != errno) || (from_line < 0) )
609 if ( (t < p) && ('-' == t[0]) )
613 to_line = strtol (start, &t, 10);
614 if ( (0 != errno) || (to_line < 0) || (t != p) )
620 else /* one number means "match this line only" */
623 else /* default to 0-max */
631 _("ERROR: Unable to parse log definition: Syntax error at `%s'.\n"),
638 case '\0': /* found EOL */
640 /* fall through to '/' */
641 case '/': /* found a definition separator */
644 case 4: /* within a log level */
647 level = get_type ((const char *) start);
648 if ( (GNUNET_ERROR_TYPE_INVALID == level) ||
649 (GNUNET_ERROR_TYPE_UNSPECIFIED == level) ||
650 (0 != add_definition (comp, file, function, from_line, to_line,
661 _("ERROR: Unable to parse log definition: Syntax error at `%s'.\n"),
675 * Utility function - parses GNUNET_LOG and GNUNET_FORCE_LOG.
678 parse_all_definitions ()
680 if (GNUNET_NO == gnunet_log_parsed)
681 parse_definitions ("GNUNET_LOG", 0);
682 gnunet_log_parsed = GNUNET_YES;
683 if (GNUNET_NO == gnunet_force_log_parsed)
684 gnunet_force_log_present =
685 parse_definitions ("GNUNET_FORCE_LOG", 1) > 0 ? GNUNET_YES : GNUNET_NO;
686 gnunet_force_log_parsed = GNUNET_YES;
694 * @param comp default component to use
695 * @param loglevel what types of messages should be logged
696 * @param logfile which file to write log messages to (can be NULL)
697 * @return #GNUNET_OK on success
700 GNUNET_log_setup (const char *comp,
701 const char *loglevel,
704 const char *env_logfile;
706 min_level = get_type (loglevel);
707 #if !defined(GNUNET_CULL_LOGGING)
708 parse_all_definitions ();
711 QueryPerformanceFrequency (&performance_frequency);
713 GNUNET_free_non_null (component);
714 GNUNET_asprintf (&component, "%s-%d", comp, getpid ());
715 GNUNET_free_non_null (component_nopid);
716 component_nopid = GNUNET_strdup (comp);
718 env_logfile = getenv ("GNUNET_FORCE_LOGFILE");
719 if ((NULL != env_logfile) && (strlen (env_logfile) > 0))
720 logfile = env_logfile;
723 GNUNET_free_non_null (log_file_name);
724 log_file_name = GNUNET_STRINGS_filename_expand (logfile);
725 if (NULL == log_file_name)
726 return GNUNET_SYSERR;
727 #if TALER_WALLET_ONLY || defined(GNUNET_CULL_LOGGING)
728 /* log file option not allowed for wallet logic */
729 GNUNET_assert (NULL == logfile);
738 return setup_log_file (tm);
745 * Add a custom logger. Note that installing any custom logger
746 * will disable the standard logger. When multiple custom loggers
747 * are installed, all will be called. The standard logger will
748 * only be used if no custom loggers are present.
750 * @param logger log function
751 * @param logger_cls closure for @a logger
754 GNUNET_logger_add (GNUNET_Logger logger,
757 struct CustomLogger *entry;
759 entry = GNUNET_new (struct CustomLogger);
760 entry->logger = logger;
761 entry->logger_cls = logger_cls;
762 entry->next = loggers;
768 * Remove a custom logger.
770 * @param logger log function
771 * @param logger_cls closure for @a logger
774 GNUNET_logger_remove (GNUNET_Logger logger,
777 struct CustomLogger *pos;
778 struct CustomLogger *prev;
782 while ((NULL != pos) &&
783 ((pos->logger != logger) || (pos->logger_cls != logger_cls)))
788 GNUNET_assert (NULL != pos);
792 prev->next = pos->next;
797 CRITICAL_SECTION output_message_cs;
802 * Actually output the log message.
804 * @param kind how severe was the issue
805 * @param comp component responsible
806 * @param datestr current date/time
807 * @param msg the actual message
810 output_message (enum GNUNET_ErrorType kind,
815 struct CustomLogger *pos;
818 EnterCriticalSection (&output_message_cs);
820 /* only use the standard logger if no custom loggers are present */
821 if ( (NULL != GNUNET_stderr) &&
824 if (kind == GNUNET_ERROR_TYPE_MESSAGE)
826 /* The idea here is to produce "normal" output messages
827 * for end users while still having the power of the
828 * logging engine for developer needs. So ideally this
829 * is what it should look like when CLI tools are used
830 * interactively, yet the same message shouldn't look
831 * this way if the output is going to logfiles or robots
834 FPRINTF (GNUNET_stderr,
840 FPRINTF (GNUNET_stderr,
844 GNUNET_error_type_to_string (kind),
847 fflush (GNUNET_stderr);
852 pos->logger (pos->logger_cls,
860 LeaveCriticalSection (&output_message_cs);
866 * Flush an existing bulk report to the output.
868 * @param datestr our current timestamp
871 flush_bulk (const char *datestr)
873 char msg[DATE_STR_SIZE + BULK_TRACK_SIZE + 256];
878 if ( (0 == last_bulk_time.abs_value_us) ||
879 (0 == last_bulk_repeat) )
882 last = memchr (last_bulk, '\0', BULK_TRACK_SIZE);
884 last = &last_bulk[BULK_TRACK_SIZE - 1];
885 else if (last != last_bulk)
892 ft = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration
893 (last_bulk_time), GNUNET_YES);
894 snprintf (msg, sizeof (msg),
895 _("Message `%.*s' repeated %u times in the last %s\n"),
896 BULK_TRACK_SIZE, last_bulk, last_bulk_repeat, ft);
899 output_message (last_bulk_kind, last_bulk_comp, datestr, msg);
900 last_bulk_time = GNUNET_TIME_absolute_get ();
901 last_bulk_repeat = 0;
906 * Ignore the next n calls to the log function.
908 * @param n number of log calls to ignore (could be negative)
909 * @param check_reset #GNUNET_YES to assert that the log skip counter is currently zero
912 GNUNET_log_skip (int n,
919 ok = (0 == skip_log);
932 * Get the number of log calls that are going to be skipped
934 * @return number of log calls to be ignored
937 GNUNET_get_log_skip ()
944 * Output a log message using the default mechanism.
946 * @param kind how severe was the issue
947 * @param comp component responsible
948 * @param message the actual message
949 * @param va arguments to the format string "message"
952 mylog (enum GNUNET_ErrorType kind,
957 char date[DATE_STR_SIZE];
958 char date2[DATE_STR_SIZE];
964 size = VSNPRINTF (NULL,
968 GNUNET_assert (0 != size);
980 offset = GNUNET_TIME_get_offset ();
982 timetmp += offset / 1000;
983 tmptr = localtime (&timetmp);
985 QueryPerformanceCounter (&pc);
988 strcpy (date, "localtime error");
995 "%b %d %H:%M:%S-%%020llu",
1002 (long long) (pc.QuadPart /
1003 (performance_frequency.QuadPart / 1000))))
1007 struct timeval timeofday;
1009 gettimeofday (&timeofday,
1011 offset = GNUNET_TIME_get_offset ();
1014 timeofday.tv_sec += offset / 1000LL;
1015 timeofday.tv_usec += (offset % 1000LL) * 1000LL;
1016 if (timeofday.tv_usec > 1000000LL)
1018 timeofday.tv_usec -= 1000000LL;
1024 timeofday.tv_sec += offset / 1000LL;
1025 if (timeofday.tv_usec > - (offset % 1000LL) * 1000LL)
1027 timeofday.tv_usec += (offset % 1000LL) * 1000LL;
1031 timeofday.tv_usec += 1000000LL + (offset % 1000LL) * 1000LL;
1035 tmptr = localtime (&timeofday.tv_sec);
1046 "%b %d %H:%M:%S-%%06u",
1061 #if ! (defined(GNUNET_CULL_LOGGING) || TALER_WALLET_ONLY)
1063 (void) setup_log_file (tmptr);
1065 if ((0 != (kind & GNUNET_ERROR_TYPE_BULK)) &&
1066 (0 != last_bulk_time.abs_value_us) &&
1069 sizeof (last_bulk))))
1072 if ( (GNUNET_TIME_absolute_get_duration (last_bulk_time).rel_value_us >
1073 BULK_DELAY_THRESHOLD) ||
1074 (last_bulk_repeat > BULK_REPEAT_THRESHOLD) )
1081 sizeof (last_bulk));
1082 last_bulk_repeat = 0;
1083 last_bulk_kind = kind;
1084 last_bulk_time = GNUNET_TIME_absolute_get ();
1085 strncpy (last_bulk_comp,
1088 output_message (kind,
1097 * Main log function.
1099 * @param kind how serious is the error?
1100 * @param message what is the message (format string)
1101 * @param ... arguments for format string
1104 GNUNET_log_nocheck (enum GNUNET_ErrorType kind,
1105 const char *message, ...)
1109 va_start (va, message);
1110 mylog (kind, component, message, va);
1116 * Log function that specifies an alternative component.
1117 * This function should be used by plugins.
1119 * @param kind how serious is the error?
1120 * @param comp component responsible for generating the message
1121 * @param message what is the message (format string)
1122 * @param ... arguments for format string
1125 GNUNET_log_from_nocheck (enum GNUNET_ErrorType kind, const char *comp,
1126 const char *message, ...)
1129 char comp_w_pid[128];
1132 comp = component_nopid;
1134 va_start (va, message);
1135 GNUNET_snprintf (comp_w_pid, sizeof (comp_w_pid), "%s-%d", comp, getpid ());
1136 mylog (kind, comp_w_pid, message, va);
1142 * Convert error type to string.
1144 * @param kind type to convert
1145 * @return string corresponding to the type
1148 GNUNET_error_type_to_string (enum GNUNET_ErrorType kind)
1150 if ((kind & GNUNET_ERROR_TYPE_ERROR) > 0)
1152 if ((kind & GNUNET_ERROR_TYPE_WARNING) > 0)
1153 return _("WARNING");
1154 if ((kind & GNUNET_ERROR_TYPE_MESSAGE) > 0)
1155 return _("MESSAGE");
1156 if ((kind & GNUNET_ERROR_TYPE_INFO) > 0)
1158 if ((kind & GNUNET_ERROR_TYPE_DEBUG) > 0)
1160 if ((kind & ~GNUNET_ERROR_TYPE_BULK) == 0)
1162 return _("INVALID");
1167 * Convert a hash to a string (for printing debug messages).
1168 * This is one of the very few calls in the entire API that is
1171 * @param hc the hash code
1172 * @return string form; will be overwritten by next call to GNUNET_h2s.
1175 GNUNET_h2s (const struct GNUNET_HashCode * hc)
1177 static struct GNUNET_CRYPTO_HashAsciiEncoded ret;
1179 GNUNET_CRYPTO_hash_to_enc (hc, &ret);
1180 ret.encoding[8] = '\0';
1181 return (const char *) ret.encoding;
1186 * Convert a hash to a string (for printing debug messages).
1187 * This is one of the very few calls in the entire API that is
1188 * NOT reentrant! Identical to #GNUNET_h2s(), except that another
1189 * buffer is used so both #GNUNET_h2s() and #GNUNET_h2s2() can be
1190 * used within the same log statement.
1192 * @param hc the hash code
1193 * @return string form; will be overwritten by next call to GNUNET_h2s.
1196 GNUNET_h2s2 (const struct GNUNET_HashCode * hc)
1198 static struct GNUNET_CRYPTO_HashAsciiEncoded ret;
1200 GNUNET_CRYPTO_hash_to_enc (hc, &ret);
1201 ret.encoding[8] = '\0';
1202 return (const char *) ret.encoding;
1208 * Convert a public key value to a string (for printing debug messages).
1209 * This is one of the very few calls in the entire API that is
1212 * @param hc the hash code
1216 GNUNET_p2s (const struct GNUNET_CRYPTO_EddsaPublicKey *p)
1218 static struct GNUNET_CRYPTO_HashAsciiEncoded ret;
1219 struct GNUNET_HashCode hc;
1221 GNUNET_CRYPTO_hash (p,
1224 GNUNET_CRYPTO_hash_to_enc (&hc,
1226 ret.encoding[6] = '\0';
1227 return (const char *) ret.encoding;
1233 * Convert a public key value to a string (for printing debug messages).
1234 * This is one of the very few calls in the entire API that is
1237 * @param hc the hash code
1241 GNUNET_p2s2 (const struct GNUNET_CRYPTO_EddsaPublicKey *p)
1243 static struct GNUNET_CRYPTO_HashAsciiEncoded ret;
1244 struct GNUNET_HashCode hc;
1246 GNUNET_CRYPTO_hash (p,
1249 GNUNET_CRYPTO_hash_to_enc (&hc,
1251 ret.encoding[6] = '\0';
1252 return (const char *) ret.encoding;
1258 * Convert a public key value to a string (for printing debug messages).
1259 * This is one of the very few calls in the entire API that is
1262 * @param hc the hash code
1266 GNUNET_e2s (const struct GNUNET_CRYPTO_EcdhePublicKey *p)
1268 static struct GNUNET_CRYPTO_HashAsciiEncoded ret;
1269 struct GNUNET_HashCode hc;
1271 GNUNET_CRYPTO_hash (p,
1274 GNUNET_CRYPTO_hash_to_enc (&hc,
1276 ret.encoding[6] = '\0';
1277 return (const char *) ret.encoding;
1283 * Convert a public key value to a string (for printing debug messages).
1284 * This is one of the very few calls in the entire API that is
1287 * @param hc the hash code
1291 GNUNET_e2s2 (const struct GNUNET_CRYPTO_EcdhePublicKey *p)
1293 static struct GNUNET_CRYPTO_HashAsciiEncoded ret;
1294 struct GNUNET_HashCode hc;
1296 GNUNET_CRYPTO_hash (p,
1299 GNUNET_CRYPTO_hash_to_enc (&hc,
1301 ret.encoding[6] = '\0';
1302 return (const char *) ret.encoding;
1308 * Convert a short hash value to a string (for printing debug messages).
1309 * This is one of the very few calls in the entire API that is
1312 * @param shc the hash code
1316 GNUNET_sh2s (const struct GNUNET_ShortHashCode *shc)
1318 static char buf[64];
1320 GNUNET_STRINGS_data_to_string (shc,
1325 return (const char *) buf;
1330 * Convert a hash to a string (for printing debug messages).
1331 * This is one of the very few calls in the entire API that is
1334 * @param hc the hash code
1335 * @return string form; will be overwritten by next call to GNUNET_h2s_full.
1338 GNUNET_h2s_full (const struct GNUNET_HashCode * hc)
1340 static struct GNUNET_CRYPTO_HashAsciiEncoded ret;
1342 GNUNET_CRYPTO_hash_to_enc (hc, &ret);
1343 ret.encoding[sizeof (ret) - 1] = '\0';
1344 return (const char *) ret.encoding;
1349 * Convert a peer identity to a string (for printing debug messages).
1350 * This is one of the very few calls in the entire API that is
1353 * @param pid the peer identity
1354 * @return string form of the pid; will be overwritten by next
1355 * call to #GNUNET_i2s.
1358 GNUNET_i2s (const struct GNUNET_PeerIdentity *pid)
1365 ret = GNUNET_CRYPTO_eddsa_public_key_to_string (&pid->public_key);
1376 * Convert a peer identity to a string (for printing debug messages).
1377 * This is one of the very few calls in the entire API that is
1378 * NOT reentrant! Identical to #GNUNET_i2s(), except that another
1379 * buffer is used so both #GNUNET_i2s() and #GNUNET_i2s2() can be
1380 * used within the same log statement.
1382 * @param pid the peer identity
1383 * @return string form of the pid; will be overwritten by next
1384 * call to #GNUNET_i2s.
1387 GNUNET_i2s2 (const struct GNUNET_PeerIdentity *pid)
1394 ret = GNUNET_CRYPTO_eddsa_public_key_to_string (&pid->public_key);
1405 * Convert a peer identity to a string (for printing debug messages).
1406 * This is one of the very few calls in the entire API that is
1409 * @param pid the peer identity
1410 * @return string form of the pid; will be overwritten by next
1411 * call to #GNUNET_i2s_full.
1414 GNUNET_i2s_full (const struct GNUNET_PeerIdentity *pid)
1416 static char buf[256];
1419 ret = GNUNET_CRYPTO_eddsa_public_key_to_string (&pid->public_key);
1427 * Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string
1428 * (for printing debug messages). This is one of the very few calls
1429 * in the entire API that is NOT reentrant!
1431 * @param addr the address
1432 * @param addrlen the length of the address in @a addr
1433 * @return nicely formatted string for the address
1434 * will be overwritten by next call to #GNUNET_a2s.
1437 GNUNET_a2s (const struct sockaddr *addr,
1441 #define LEN GNUNET_MAX ((INET6_ADDRSTRLEN + 8), \
1442 (1 + sizeof (struct sockaddr_un) - sizeof (sa_family_t)))
1444 #define LEN (INET6_ADDRSTRLEN + 8)
1446 static char buf[LEN];
1449 const struct sockaddr_in *v4;
1450 const struct sockaddr_un *un;
1451 const struct sockaddr_in6 *v6;
1455 return _("unknown address");
1456 switch (addr->sa_family)
1459 if (addrlen != sizeof (struct sockaddr_in))
1460 return "<invalid v4 address>";
1461 v4 = (const struct sockaddr_in *) addr;
1462 inet_ntop (AF_INET, &v4->sin_addr, buf, INET_ADDRSTRLEN);
1463 if (0 == ntohs (v4->sin_port))
1466 GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v4->sin_port));
1470 if (addrlen != sizeof (struct sockaddr_in6))
1471 return "<invalid v4 address>";
1472 v6 = (const struct sockaddr_in6 *) addr;
1474 inet_ntop (AF_INET6, &v6->sin6_addr, &buf[1], INET6_ADDRSTRLEN);
1475 if (0 == ntohs (v6->sin6_port))
1478 GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v6->sin6_port));
1482 if (addrlen <= sizeof (sa_family_t))
1483 return "<unbound UNIX client>";
1484 un = (const struct sockaddr_un *) addr;
1486 if ('\0' == un->sun_path[0])
1488 memset (buf, 0, sizeof (buf));
1489 GNUNET_snprintf (buf,
1492 (1 == off) ? "@" : "",
1493 (int) (addrlen - sizeof (sa_family_t) - off),
1494 &un->sun_path[off]);
1497 return _("invalid address");
1503 * Log error message about missing configuration option.
1505 * @param kind log level
1506 * @param section section with missing option
1507 * @param option name of missing option
1510 GNUNET_log_config_missing (enum GNUNET_ErrorType kind,
1511 const char *section,
1515 _("Configuration fails to specify option `%s' in section `%s'!\n"),
1522 * Log error message about invalid configuration option value.
1524 * @param kind log level
1525 * @param section section with invalid option
1526 * @param option name of invalid option
1527 * @param required what is required that is invalid about the option
1530 GNUNET_log_config_invalid (enum GNUNET_ErrorType kind,
1531 const char *section,
1533 const char *required)
1536 _("Configuration specifies invalid value for option `%s' in section `%s': %s\n"),
1537 option, section, required);
1544 void __attribute__ ((constructor))
1545 GNUNET_util_cl_init ()
1547 GNUNET_stderr = stderr;
1549 GNInitWinEnv (NULL);
1552 if (!InitializeCriticalSectionAndSpinCount (&output_message_cs, 0x00000400))
1561 void __attribute__ ((destructor))
1562 GNUNET_util_cl_fini ()
1565 DeleteCriticalSection (&output_message_cs);
1568 GNShutdownWinEnv ();
1572 /* end of common_logging.c */