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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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] __attribute__ ((nonstring));
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 #if !defined(GNUNET_CULL_LOGGING)
209 * Dynamic array of logging definitions
211 static struct LogDef *logdefs;
214 * Allocated size of logdefs array (in units)
216 static int logdefs_size;
219 * The number of units used in logdefs array.
221 static int logdefs_len;
224 * #GNUNET_YES if GNUNET_LOG environment variable is already parsed.
226 static int gnunet_log_parsed;
229 * #GNUNET_YES if GNUNET_FORCE_LOG environment variable is already parsed.
231 static int gnunet_force_log_parsed;
234 * #GNUNET_YES if at least one definition with forced == 1 is available.
236 static int gnunet_force_log_present;
241 * Contains the number of performance counts per second.
243 static LARGE_INTEGER performance_frequency;
248 * Convert a textual description of a loglevel
249 * to the respective GNUNET_GE_KIND.
251 * @param log loglevel to parse
252 * @return GNUNET_GE_INVALID if log does not parse
254 static enum GNUNET_ErrorType
255 get_type (const char *log)
258 return GNUNET_ERROR_TYPE_UNSPECIFIED;
259 if (0 == strcasecmp (log, _("DEBUG")))
260 return GNUNET_ERROR_TYPE_DEBUG;
261 if (0 == strcasecmp (log, _("INFO")))
262 return GNUNET_ERROR_TYPE_INFO;
263 if (0 == strcasecmp (log, _("MESSAGE")))
264 return GNUNET_ERROR_TYPE_MESSAGE;
265 if (0 == strcasecmp (log, _("WARNING")))
266 return GNUNET_ERROR_TYPE_WARNING;
267 if (0 == strcasecmp (log, _("ERROR")))
268 return GNUNET_ERROR_TYPE_ERROR;
269 if (0 == strcasecmp (log, _("NONE")))
270 return GNUNET_ERROR_TYPE_NONE;
271 return GNUNET_ERROR_TYPE_INVALID;
276 * Abort the process, generate a core dump if possible.
288 #if !defined(GNUNET_CULL_LOGGING)
290 * Utility function - reallocates logdefs array to be twice as large.
295 logdefs_size = (logdefs_size + 1) * 2;
296 logdefs = GNUNET_realloc (logdefs, logdefs_size * sizeof (struct LogDef));
300 #if ! TALER_WALLET_ONLY
302 * Rotate logs, deleting the oldest log.
304 * @param new_name new name to add to the rotation
307 log_rotate (const char *new_name)
309 static char *rotation[ROTATION_KEEP];
310 static unsigned int rotation_off;
313 if ('\0' == *new_name)
314 return; /* not a real log file name */
315 discard = rotation[rotation_off % ROTATION_KEEP];
318 /* Note: can't log errors during logging (recursion!), so this
319 operation MUST silently fail... */
320 (void) UNLINK (discard);
321 GNUNET_free (discard);
323 rotation[rotation_off % ROTATION_KEEP] = GNUNET_strdup (new_name);
329 * Setup the log file.
331 * @param tm timestamp for which we should setup logging
332 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
335 setup_log_file (const struct tm *tm)
337 static char last_fn[PATH_MAX + 1];
338 char fn[PATH_MAX + 1];
344 if (NULL == log_file_name)
345 return GNUNET_SYSERR;
346 if (0 == strftime (fn, sizeof (fn), log_file_name, tm))
347 return GNUNET_SYSERR;
348 leftsquare = strrchr (fn, '[');
349 if ( (NULL != leftsquare) && (']' == leftsquare[1]) )
351 char *logfile_copy = GNUNET_strdup (fn);
353 logfile_copy[leftsquare - fn] = '\0';
354 logfile_copy[leftsquare - fn + 1] = '\0';
360 &logfile_copy[leftsquare - fn + 2]);
361 GNUNET_free (logfile_copy);
363 if (0 == strcmp (fn, last_fn))
364 return GNUNET_OK; /* no change */
365 log_rotate (last_fn);
366 strcpy (last_fn, fn);
368 GNUNET_DISK_directory_create_for_file (fn))
371 "Failed to create directory for `%s': %s\n",
374 return GNUNET_SYSERR;
377 altlog_fd = OPEN (fn, O_APPEND |
380 _S_IREAD | _S_IWRITE);
382 altlog_fd = OPEN (fn, O_APPEND |
384 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
388 if (NULL != GNUNET_stderr)
389 fclose (GNUNET_stderr);
390 dup_return = dup2 (altlog_fd, 2);
391 (void) close (altlog_fd);
392 if (-1 != dup_return)
394 altlog = fdopen (2, "ab");
408 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", fn);
409 return GNUNET_SYSERR;
411 GNUNET_stderr = altlog;
418 * Utility function - adds a parsed definition to logdefs array.
420 * @param component see struct LogDef, can't be NULL
421 * @param file see struct LogDef, can't be NULL
422 * @param function see struct LogDef, can't be NULL
423 * @param from_line see struct LogDef
424 * @param to_line see struct LogDef
425 * @param level see struct LogDef, must be >= 0
426 * @param force see struct LogDef
427 * @return 0 on success, regex-specific error otherwise
430 add_definition (const char *component,
432 const char *function,
441 if (logdefs_size == logdefs_len)
443 memset (&n, 0, sizeof (n));
444 if (0 == strlen (component))
445 component = (char *) ".*";
446 r = regcomp (&n.component_regex, (const char *) component, REG_NOSUB);
451 if (0 == strlen (file))
452 file = (char *) ".*";
453 r = regcomp (&n.file_regex, (const char *) file, REG_NOSUB);
456 regfree (&n.component_regex);
459 if ((NULL == function) || (0 == strlen (function)))
460 function = (char *) ".*";
461 r = regcomp (&n.function_regex, (const char *) function, REG_NOSUB);
464 regfree (&n.component_regex);
465 regfree (&n.file_regex);
468 n.from_line = from_line;
472 logdefs[logdefs_len++] = n;
478 * Decides whether a particular logging call should or should not be allowed
479 * to be made. Used internally by GNUNET_log*()
481 * @param caller_level loglevel the caller wants to use
482 * @param comp component name the caller uses (NULL means that global
483 * component name is used)
484 * @param file file name containing the logging call, usually __FILE__
485 * @param function function which tries to make a logging call,
486 * usually __FUNCTION__
487 * @param line line at which the call is made, usually __LINE__
488 * @return 0 to disallow the call, 1 to allow it
491 GNUNET_get_log_call_status (int caller_level,
494 const char *function,
502 /* Use default component */
503 comp = component_nopid;
505 /* We have no definitions to override globally configured log level,
506 * so just use it right away.
508 if ( (min_level >= 0) && (GNUNET_NO == gnunet_force_log_present) )
509 return caller_level <= min_level;
511 /* Only look for forced definitions? */
512 force_only = min_level >= 0;
513 for (i = 0; i < logdefs_len; i++)
516 if (( (!force_only) || ld->force) &&
517 (line >= ld->from_line && line <= ld->to_line) &&
518 (0 == regexec (&ld->component_regex, comp, 0, NULL, 0)) &&
519 (0 == regexec (&ld->file_regex, file, 0, NULL, 0)) &&
520 (0 == regexec (&ld->function_regex, function, 0, NULL, 0)))
523 return caller_level <= ld->level;
526 /* No matches - use global level, if defined */
528 return caller_level <= min_level;
529 /* All programs/services previously defaulted to WARNING.
530 * Now *we* default to WARNING, and THEY default to NULL.
531 * Or rather we default to MESSAGE, since things aren't always bad.
533 return caller_level <= GNUNET_ERROR_TYPE_MESSAGE;
538 * Utility function - parses a definition
541 * component;file;function;from_line-to_line;level[/component...]
542 * All entries are mandatory, but may be empty.
543 * Empty entries for component, file and function are treated as
544 * "matches anything".
545 * Empty line entry is treated as "from 0 to INT_MAX"
546 * Line entry with only one line is treated as "this line only"
547 * Entry for level MUST NOT be empty.
548 * Entries for component, file and function that consist of a
549 * single character "*" are treated (at the moment) the same way
550 * empty entries are treated (wildcard matching is not implemented (yet?)).
551 * file entry is matched to the end of __FILE__. That is, it might be
552 * a base name, or a base name with leading directory names (some compilers
553 * define __FILE__ to absolute file path).
555 * @param constname name of the environment variable from which to get the
556 * string to be parsed
557 * @param force 1 if definitions found in constname are to be forced
558 * @return number of added definitions
561 parse_definitions (const char *constname, int force)
567 char *function = NULL;
573 int from_line, to_line;
575 int keep_looking = 1;
577 tmp = getenv (constname);
580 def = GNUNET_strdup (tmp);
583 for (p = def, state = 0, start = def; keep_looking; p++)
587 case ';': /* found a field separator */
591 case 0: /* within a component name */
594 case 1: /* within a file name */
597 case 2: /* within a function name */
598 /* after a file name there must be a function name */
601 case 3: /* within a from-to line range */
602 if (strlen (start) > 0)
605 from_line = strtol (start, &t, 10);
606 if ( (0 != errno) || (from_line < 0) )
611 if ( (t < p) && ('-' == t[0]) )
615 to_line = strtol (start, &t, 10);
616 if ( (0 != errno) || (to_line < 0) || (t != p) )
622 else /* one number means "match this line only" */
625 else /* default to 0-max */
633 _("ERROR: Unable to parse log definition: Syntax error at `%s'.\n"),
640 case '\0': /* found EOL */
642 /* fall through to '/' */
643 case '/': /* found a definition separator */
646 case 4: /* within a log level */
649 level = get_type ((const char *) start);
650 if ( (GNUNET_ERROR_TYPE_INVALID == level) ||
651 (GNUNET_ERROR_TYPE_UNSPECIFIED == level) ||
652 (0 != add_definition (comp, file, function, from_line, to_line,
663 _("ERROR: Unable to parse log definition: Syntax error at `%s'.\n"),
677 * Utility function - parses GNUNET_LOG and GNUNET_FORCE_LOG.
680 parse_all_definitions ()
682 if (GNUNET_NO == gnunet_force_log_parsed)
683 gnunet_force_log_present =
684 parse_definitions ("GNUNET_FORCE_LOG", 1) > 0 ? GNUNET_YES : GNUNET_NO;
685 gnunet_force_log_parsed = GNUNET_YES;
687 if (GNUNET_NO == gnunet_log_parsed)
688 parse_definitions ("GNUNET_LOG", 0);
689 gnunet_log_parsed = GNUNET_YES;
697 * @param comp default component to use
698 * @param loglevel what types of messages should be logged
699 * @param logfile which file to write log messages to (can be NULL)
700 * @return #GNUNET_OK on success
703 GNUNET_log_setup (const char *comp,
704 const char *loglevel,
707 const char *env_logfile;
709 min_level = get_type (loglevel);
710 #if !defined(GNUNET_CULL_LOGGING)
711 parse_all_definitions ();
714 QueryPerformanceFrequency (&performance_frequency);
716 GNUNET_free_non_null (component);
717 GNUNET_asprintf (&component, "%s-%d", comp, getpid ());
718 GNUNET_free_non_null (component_nopid);
719 component_nopid = GNUNET_strdup (comp);
721 env_logfile = getenv ("GNUNET_FORCE_LOGFILE");
722 if ((NULL != env_logfile) && (strlen (env_logfile) > 0))
723 logfile = env_logfile;
726 GNUNET_free_non_null (log_file_name);
727 log_file_name = GNUNET_STRINGS_filename_expand (logfile);
728 if (NULL == log_file_name)
729 return GNUNET_SYSERR;
730 #if TALER_WALLET_ONLY || defined(GNUNET_CULL_LOGGING)
731 /* log file option not allowed for wallet logic */
732 GNUNET_assert (NULL == logfile);
741 return setup_log_file (tm);
748 * Add a custom logger. Note that installing any custom logger
749 * will disable the standard logger. When multiple custom loggers
750 * are installed, all will be called. The standard logger will
751 * only be used if no custom loggers are present.
753 * @param logger log function
754 * @param logger_cls closure for @a logger
757 GNUNET_logger_add (GNUNET_Logger logger,
760 struct CustomLogger *entry;
762 entry = GNUNET_new (struct CustomLogger);
763 entry->logger = logger;
764 entry->logger_cls = logger_cls;
765 entry->next = loggers;
771 * Remove a custom logger.
773 * @param logger log function
774 * @param logger_cls closure for @a logger
777 GNUNET_logger_remove (GNUNET_Logger logger,
780 struct CustomLogger *pos;
781 struct CustomLogger *prev;
785 while ((NULL != pos) &&
786 ((pos->logger != logger) || (pos->logger_cls != logger_cls)))
791 GNUNET_assert (NULL != pos);
795 prev->next = pos->next;
800 CRITICAL_SECTION output_message_cs;
805 * Actually output the log message.
807 * @param kind how severe was the issue
808 * @param comp component responsible
809 * @param datestr current date/time
810 * @param msg the actual message
813 output_message (enum GNUNET_ErrorType kind,
818 struct CustomLogger *pos;
821 EnterCriticalSection (&output_message_cs);
823 /* only use the standard logger if no custom loggers are present */
824 if ( (NULL != GNUNET_stderr) &&
827 if (kind == GNUNET_ERROR_TYPE_MESSAGE)
829 /* The idea here is to produce "normal" output messages
830 * for end users while still having the power of the
831 * logging engine for developer needs. So ideally this
832 * is what it should look like when CLI tools are used
833 * interactively, yet the same message shouldn't look
834 * this way if the output is going to logfiles or robots
837 FPRINTF (GNUNET_stderr,
843 FPRINTF (GNUNET_stderr,
847 GNUNET_error_type_to_string (kind),
850 fflush (GNUNET_stderr);
855 pos->logger (pos->logger_cls,
863 LeaveCriticalSection (&output_message_cs);
869 * Flush an existing bulk report to the output.
871 * @param datestr our current timestamp
874 flush_bulk (const char *datestr)
876 char msg[DATE_STR_SIZE + BULK_TRACK_SIZE + 256];
881 if ( (0 == last_bulk_time.abs_value_us) ||
882 (0 == last_bulk_repeat) )
885 last = memchr (last_bulk, '\0', BULK_TRACK_SIZE);
887 last = &last_bulk[BULK_TRACK_SIZE - 1];
888 else if (last != last_bulk)
895 ft = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration
896 (last_bulk_time), GNUNET_YES);
897 snprintf (msg, sizeof (msg),
898 _("Message `%.*s' repeated %u times in the last %s\n"),
899 BULK_TRACK_SIZE, last_bulk, last_bulk_repeat, ft);
902 output_message (last_bulk_kind, last_bulk_comp, datestr, msg);
903 last_bulk_time = GNUNET_TIME_absolute_get ();
904 last_bulk_repeat = 0;
909 * Ignore the next n calls to the log function.
911 * @param n number of log calls to ignore (could be negative)
912 * @param check_reset #GNUNET_YES to assert that the log skip counter is currently zero
915 GNUNET_log_skip (int n,
922 ok = (0 == skip_log);
935 * Get the number of log calls that are going to be skipped
937 * @return number of log calls to be ignored
940 GNUNET_get_log_skip ()
947 * Output a log message using the default mechanism.
949 * @param kind how severe was the issue
950 * @param comp component responsible
951 * @param message the actual message
952 * @param va arguments to the format string "message"
955 mylog (enum GNUNET_ErrorType kind,
960 char date[DATE_STR_SIZE];
961 char date2[DATE_STR_SIZE];
967 size = VSNPRINTF (NULL,
971 GNUNET_assert (0 != size);
983 offset = GNUNET_TIME_get_offset ();
985 timetmp += offset / 1000;
986 tmptr = localtime (&timetmp);
988 QueryPerformanceCounter (&pc);
991 strcpy (date, "localtime error");
998 "%b %d %H:%M:%S-%%020llu",
1005 (long long) (pc.QuadPart /
1006 (performance_frequency.QuadPart / 1000))))
1010 struct timeval timeofday;
1012 gettimeofday (&timeofday,
1014 offset = GNUNET_TIME_get_offset ();
1017 timeofday.tv_sec += offset / 1000LL;
1018 timeofday.tv_usec += (offset % 1000LL) * 1000LL;
1019 if (timeofday.tv_usec > 1000000LL)
1021 timeofday.tv_usec -= 1000000LL;
1027 timeofday.tv_sec += offset / 1000LL;
1028 if (timeofday.tv_usec > - (offset % 1000LL) * 1000LL)
1030 timeofday.tv_usec += (offset % 1000LL) * 1000LL;
1034 timeofday.tv_usec += 1000000LL + (offset % 1000LL) * 1000LL;
1038 tmptr = localtime (&timeofday.tv_sec);
1049 "%b %d %H:%M:%S-%%06u",
1064 #if ! (defined(GNUNET_CULL_LOGGING) || TALER_WALLET_ONLY)
1066 (void) setup_log_file (tmptr);
1068 if ((0 != (kind & GNUNET_ERROR_TYPE_BULK)) &&
1069 (0 != last_bulk_time.abs_value_us) &&
1072 sizeof (last_bulk))))
1075 if ( (GNUNET_TIME_absolute_get_duration (last_bulk_time).rel_value_us >
1076 BULK_DELAY_THRESHOLD) ||
1077 (last_bulk_repeat > BULK_REPEAT_THRESHOLD) )
1084 sizeof (last_bulk));
1085 last_bulk_repeat = 0;
1086 last_bulk_kind = kind;
1087 last_bulk_time = GNUNET_TIME_absolute_get ();
1088 strncpy (last_bulk_comp,
1091 output_message (kind,
1100 * Main log function.
1102 * @param kind how serious is the error?
1103 * @param message what is the message (format string)
1104 * @param ... arguments for format string
1107 GNUNET_log_nocheck (enum GNUNET_ErrorType kind,
1108 const char *message, ...)
1112 va_start (va, message);
1113 mylog (kind, component, message, va);
1119 * Log function that specifies an alternative component.
1120 * This function should be used by plugins.
1122 * @param kind how serious is the error?
1123 * @param comp component responsible for generating the message
1124 * @param message what is the message (format string)
1125 * @param ... arguments for format string
1128 GNUNET_log_from_nocheck (enum GNUNET_ErrorType kind, const char *comp,
1129 const char *message, ...)
1132 char comp_w_pid[128];
1135 comp = component_nopid;
1137 va_start (va, message);
1138 GNUNET_snprintf (comp_w_pid, sizeof (comp_w_pid), "%s-%d", comp, getpid ());
1139 mylog (kind, comp_w_pid, message, va);
1145 * Convert error type to string.
1147 * @param kind type to convert
1148 * @return string corresponding to the type
1151 GNUNET_error_type_to_string (enum GNUNET_ErrorType kind)
1153 if ((kind & GNUNET_ERROR_TYPE_ERROR) > 0)
1155 if ((kind & GNUNET_ERROR_TYPE_WARNING) > 0)
1156 return _("WARNING");
1157 if ((kind & GNUNET_ERROR_TYPE_MESSAGE) > 0)
1158 return _("MESSAGE");
1159 if ((kind & GNUNET_ERROR_TYPE_INFO) > 0)
1161 if ((kind & GNUNET_ERROR_TYPE_DEBUG) > 0)
1163 if ((kind & ~GNUNET_ERROR_TYPE_BULK) == 0)
1165 return _("INVALID");
1170 * Convert a hash to a string (for printing debug messages).
1171 * This is one of the very few calls in the entire API that is
1174 * @param hc the hash code
1175 * @return string form; will be overwritten by next call to GNUNET_h2s.
1178 GNUNET_h2s (const struct GNUNET_HashCode * hc)
1180 static struct GNUNET_CRYPTO_HashAsciiEncoded ret;
1182 GNUNET_CRYPTO_hash_to_enc (hc, &ret);
1183 ret.encoding[8] = '\0';
1184 return (const char *) ret.encoding;
1189 * Convert a hash to a string (for printing debug messages).
1190 * This is one of the very few calls in the entire API that is
1191 * NOT reentrant! Identical to #GNUNET_h2s(), except that another
1192 * buffer is used so both #GNUNET_h2s() and #GNUNET_h2s2() can be
1193 * used within the same log statement.
1195 * @param hc the hash code
1196 * @return string form; will be overwritten by next call to GNUNET_h2s.
1199 GNUNET_h2s2 (const struct GNUNET_HashCode * hc)
1201 static struct GNUNET_CRYPTO_HashAsciiEncoded ret;
1203 GNUNET_CRYPTO_hash_to_enc (hc, &ret);
1204 ret.encoding[8] = '\0';
1205 return (const char *) ret.encoding;
1211 * Convert a public key value to a string (for printing debug messages).
1212 * This is one of the very few calls in the entire API that is
1215 * @param hc the hash code
1219 GNUNET_p2s (const struct GNUNET_CRYPTO_EddsaPublicKey *p)
1221 static struct GNUNET_CRYPTO_HashAsciiEncoded ret;
1222 struct GNUNET_HashCode hc;
1224 GNUNET_CRYPTO_hash (p,
1227 GNUNET_CRYPTO_hash_to_enc (&hc,
1229 ret.encoding[6] = '\0';
1230 return (const char *) ret.encoding;
1236 * Convert a public key value to a string (for printing debug messages).
1237 * This is one of the very few calls in the entire API that is
1240 * @param hc the hash code
1244 GNUNET_p2s2 (const struct GNUNET_CRYPTO_EddsaPublicKey *p)
1246 static struct GNUNET_CRYPTO_HashAsciiEncoded ret;
1247 struct GNUNET_HashCode hc;
1249 GNUNET_CRYPTO_hash (p,
1252 GNUNET_CRYPTO_hash_to_enc (&hc,
1254 ret.encoding[6] = '\0';
1255 return (const char *) ret.encoding;
1261 * Convert a public key value to a string (for printing debug messages).
1262 * This is one of the very few calls in the entire API that is
1265 * @param hc the hash code
1269 GNUNET_e2s (const struct GNUNET_CRYPTO_EcdhePublicKey *p)
1271 static struct GNUNET_CRYPTO_HashAsciiEncoded ret;
1272 struct GNUNET_HashCode hc;
1274 GNUNET_CRYPTO_hash (p,
1277 GNUNET_CRYPTO_hash_to_enc (&hc,
1279 ret.encoding[6] = '\0';
1280 return (const char *) ret.encoding;
1286 * Convert a public key value to a string (for printing debug messages).
1287 * This is one of the very few calls in the entire API that is
1290 * @param hc the hash code
1294 GNUNET_e2s2 (const struct GNUNET_CRYPTO_EcdhePublicKey *p)
1296 static struct GNUNET_CRYPTO_HashAsciiEncoded ret;
1297 struct GNUNET_HashCode hc;
1299 GNUNET_CRYPTO_hash (p,
1302 GNUNET_CRYPTO_hash_to_enc (&hc,
1304 ret.encoding[6] = '\0';
1305 return (const char *) ret.encoding;
1311 * Convert a short hash value to a string (for printing debug messages).
1312 * This is one of the very few calls in the entire API that is
1315 * @param shc the hash code
1319 GNUNET_sh2s (const struct GNUNET_ShortHashCode *shc)
1321 static char buf[64];
1323 GNUNET_STRINGS_data_to_string (shc,
1328 return (const char *) buf;
1333 * Convert a hash to a string (for printing debug messages).
1334 * This is one of the very few calls in the entire API that is
1337 * @param hc the hash code
1338 * @return string form; will be overwritten by next call to GNUNET_h2s_full.
1341 GNUNET_h2s_full (const struct GNUNET_HashCode * hc)
1343 static struct GNUNET_CRYPTO_HashAsciiEncoded ret;
1345 GNUNET_CRYPTO_hash_to_enc (hc, &ret);
1346 ret.encoding[sizeof (ret) - 1] = '\0';
1347 return (const char *) ret.encoding;
1352 * Convert a peer identity to a string (for printing debug messages).
1353 * This is one of the very few calls in the entire API that is
1356 * @param pid the peer identity
1357 * @return string form of the pid; will be overwritten by next
1358 * call to #GNUNET_i2s.
1361 GNUNET_i2s (const struct GNUNET_PeerIdentity *pid)
1368 ret = GNUNET_CRYPTO_eddsa_public_key_to_string (&pid->public_key);
1379 * Convert a peer identity to a string (for printing debug messages).
1380 * This is one of the very few calls in the entire API that is
1381 * NOT reentrant! Identical to #GNUNET_i2s(), except that another
1382 * buffer is used so both #GNUNET_i2s() and #GNUNET_i2s2() can be
1383 * used within the same log statement.
1385 * @param pid the peer identity
1386 * @return string form of the pid; will be overwritten by next
1387 * call to #GNUNET_i2s.
1390 GNUNET_i2s2 (const struct GNUNET_PeerIdentity *pid)
1397 ret = GNUNET_CRYPTO_eddsa_public_key_to_string (&pid->public_key);
1408 * Convert a peer identity to a string (for printing debug messages).
1409 * This is one of the very few calls in the entire API that is
1412 * @param pid the peer identity
1413 * @return string form of the pid; will be overwritten by next
1414 * call to #GNUNET_i2s_full.
1417 GNUNET_i2s_full (const struct GNUNET_PeerIdentity *pid)
1419 static char buf[256];
1422 ret = GNUNET_CRYPTO_eddsa_public_key_to_string (&pid->public_key);
1430 * Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string
1431 * (for printing debug messages). This is one of the very few calls
1432 * in the entire API that is NOT reentrant!
1434 * @param addr the address
1435 * @param addrlen the length of the address in @a addr
1436 * @return nicely formatted string for the address
1437 * will be overwritten by next call to #GNUNET_a2s.
1440 GNUNET_a2s (const struct sockaddr *addr,
1444 #define LEN GNUNET_MAX ((INET6_ADDRSTRLEN + 8), \
1445 (1 + sizeof (struct sockaddr_un) - sizeof (sa_family_t)))
1447 #define LEN (INET6_ADDRSTRLEN + 8)
1449 static char buf[LEN];
1452 const struct sockaddr_in *v4;
1453 const struct sockaddr_un *un;
1454 const struct sockaddr_in6 *v6;
1458 return _("unknown address");
1459 switch (addr->sa_family)
1462 if (addrlen != sizeof (struct sockaddr_in))
1463 return "<invalid v4 address>";
1464 v4 = (const struct sockaddr_in *) addr;
1465 inet_ntop (AF_INET, &v4->sin_addr, buf, INET_ADDRSTRLEN);
1466 if (0 == ntohs (v4->sin_port))
1469 GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v4->sin_port));
1473 if (addrlen != sizeof (struct sockaddr_in6))
1474 return "<invalid v4 address>";
1475 v6 = (const struct sockaddr_in6 *) addr;
1477 inet_ntop (AF_INET6, &v6->sin6_addr, &buf[1], INET6_ADDRSTRLEN);
1478 if (0 == ntohs (v6->sin6_port))
1481 GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v6->sin6_port));
1485 if (addrlen <= sizeof (sa_family_t))
1486 return "<unbound UNIX client>";
1487 un = (const struct sockaddr_un *) addr;
1489 if ('\0' == un->sun_path[0])
1491 memset (buf, 0, sizeof (buf));
1492 GNUNET_snprintf (buf,
1495 (1 == off) ? "@" : "",
1496 (int) (addrlen - sizeof (sa_family_t) - off),
1497 &un->sun_path[off]);
1500 return _("invalid address");
1506 * Log error message about missing configuration option.
1508 * @param kind log level
1509 * @param section section with missing option
1510 * @param option name of missing option
1513 GNUNET_log_config_missing (enum GNUNET_ErrorType kind,
1514 const char *section,
1518 _("Configuration fails to specify option `%s' in section `%s'!\n"),
1525 * Log error message about invalid configuration option value.
1527 * @param kind log level
1528 * @param section section with invalid option
1529 * @param option name of invalid option
1530 * @param required what is required that is invalid about the option
1533 GNUNET_log_config_invalid (enum GNUNET_ErrorType kind,
1534 const char *section,
1536 const char *required)
1539 _("Configuration specifies invalid value for option `%s' in section `%s': %s\n"),
1540 option, section, required);
1547 void __attribute__ ((constructor))
1548 GNUNET_util_cl_init ()
1550 GNUNET_stderr = stderr;
1552 GNInitWinEnv (NULL);
1555 if (!InitializeCriticalSectionAndSpinCount (&output_message_cs, 0x00000400))
1564 void __attribute__ ((destructor))
1565 GNUNET_util_cl_fini ()
1568 DeleteCriticalSection (&output_message_cs);
1571 GNShutdownWinEnv ();
1575 /* end of common_logging.c */