2 This file is part of GNUnet.
3 (C) 2006, 2008, 2009 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 2, 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_common.h"
29 #include "gnunet_crypto_lib.h"
30 #include "gnunet_strings_lib.h"
31 #include "gnunet_time_lib.h"
34 * After how many seconds do we always print
35 * that "message X was repeated N times"? Use 12h.
37 #define BULK_DELAY_THRESHOLD (12 * 60 * 60 * 1000)
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 can a date/time string
56 #define DATE_STR_SIZE 64
59 * Linked list of active loggers.
64 * This is a linked list.
66 struct CustomLogger *next;
80 * The last "bulk" error message that we have been logging.
81 * Note that this message maybe truncated to the first BULK_TRACK_SIZE
82 * characters, in which case it is NOT 0-terminated!
84 static char last_bulk[BULK_TRACK_SIZE];
87 * Type of the last bulk message.
89 static enum GNUNET_ErrorType last_bulk_kind;
92 * Time of the last bulk error message (0 for none)
94 static struct GNUNET_TIME_Absolute last_bulk_time;
97 * Number of times that bulk message has been repeated since.
99 static unsigned int last_bulk_repeat;
102 * Component when the last bulk was logged.
104 static const char *last_bulk_comp;
109 static const char *component;
114 static enum GNUNET_ErrorType min_level;
117 * Linked list of our custom loggres.
119 static struct CustomLogger *loggers;
122 * Number of log calls to ignore.
124 static unsigned int skip_log;
127 * File descriptor to use for "stderr", or NULL for none.
129 static FILE *GNUNET_stderr;
132 * Convert a textual description of a loglevel
133 * to the respective GNUNET_GE_KIND.
135 * @param log loglevel to parse
136 * @return GNUNET_GE_INVALID if log does not parse
138 static enum GNUNET_ErrorType
139 get_type (const char *log)
141 if (0 == strcasecmp (log, _("DEBUG")))
142 return GNUNET_ERROR_TYPE_DEBUG;
143 if (0 == strcasecmp (log, _("INFO")))
144 return GNUNET_ERROR_TYPE_INFO;
145 if (0 == strcasecmp (log, _("WARNING")))
146 return GNUNET_ERROR_TYPE_WARNING;
147 if (0 == strcasecmp (log, _("ERROR")))
148 return GNUNET_ERROR_TYPE_ERROR;
149 return GNUNET_ERROR_TYPE_INVALID;
156 * @param comp default component to use
157 * @param loglevel what types of messages should be logged
158 * @param logfile which file to write log messages to (can be NULL)
159 * @return GNUNET_OK on success
162 GNUNET_log_setup (const char *comp, const char *loglevel, const char *logfile)
167 min_level = get_type (loglevel);
170 altlog = fopen (logfile, "a");
173 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "fopen", logfile);
174 return GNUNET_SYSERR;
176 if (GNUNET_stderr != NULL)
177 fclose (GNUNET_stderr);
178 GNUNET_stderr = altlog;
183 * Add a custom logger.
185 * @param logger log function
186 * @param logger_cls closure for logger
189 GNUNET_logger_add (GNUNET_Logger logger, void *logger_cls)
191 struct CustomLogger *entry;
193 entry = GNUNET_malloc (sizeof (struct CustomLogger));
194 entry->logger = logger;
195 entry->logger_cls = logger_cls;
196 entry->next = loggers;
201 * Remove a custom logger.
203 * @param logger log function
204 * @param logger_cls closure for logger
207 GNUNET_logger_remove (GNUNET_Logger logger, void *logger_cls)
209 struct CustomLogger *pos;
210 struct CustomLogger *prev;
214 while ((pos != NULL) &&
215 ((pos->logger != logger) || (pos->logger_cls != logger_cls)))
220 GNUNET_assert (pos != NULL);
224 prev->next = pos->next;
230 * Actually output the log message.
232 * @param kind how severe was the issue
233 * @param comp component responsible
234 * @param datestr current date/time
235 * @param msg the actual message
238 output_message (enum GNUNET_ErrorType kind,
239 const char *comp, const char *datestr, const char *msg)
241 struct CustomLogger *pos;
242 if (GNUNET_stderr != NULL)
243 fprintf (GNUNET_stderr, "%s %s %s %s", datestr, comp,
244 GNUNET_error_type_to_string (kind), msg);
248 pos->logger (pos->logger_cls, kind, comp, datestr, msg);
255 * Flush an existing bulk report to the output.
257 * @param datastr our current timestamp
260 flush_bulk (const char *datestr)
262 char msg[DATE_STR_SIZE + BULK_TRACK_SIZE + 256];
267 if ((last_bulk_time.value == 0) || (last_bulk_repeat == 0))
270 last = memchr (last_bulk, '\0', BULK_TRACK_SIZE);
272 last = &last_bulk[BULK_TRACK_SIZE - 1];
273 else if (last != last_bulk)
281 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration
283 snprintf (msg, sizeof (msg),
284 _("Message `%.*s' repeated %u times in the last %s\n"),
285 BULK_TRACK_SIZE, last_bulk, last_bulk_repeat, ft);
289 output_message (last_bulk_kind, last_bulk_comp, datestr, msg);
290 last_bulk_time = GNUNET_TIME_absolute_get ();
291 last_bulk_repeat = 0;
296 * Ignore the next n calls to the log function.
298 * @param n number of log calls to ignore
299 * @param check_reset GNUNET_YES to assert that the log skip counter is currently zero
302 GNUNET_log_skip (unsigned int n, int check_reset)
308 ok = (0 == skip_log);
319 * Output a log message using the default mechanism.
321 * @param kind how severe was the issue
322 * @param comp component responsible
323 * @param message the actual message
324 * @param va arguments to the format string "message"
327 mylog (enum GNUNET_ErrorType kind,
328 const char *comp, const char *message, va_list va)
330 char date[DATE_STR_SIZE];
342 if ((kind & (~GNUNET_ERROR_TYPE_BULK)) > min_level)
345 size = VSNPRINTF (NULL, 0, message, vacp) + 1;
350 VSNPRINTF (buf, size, message, va);
352 memset (date, 0, DATE_STR_SIZE);
353 tmptr = localtime (&timetmp);
354 strftime (date, DATE_STR_SIZE, "%b %d %H:%M:%S", tmptr);
355 if ((0 != (kind & GNUNET_ERROR_TYPE_BULK)) &&
356 (last_bulk_time.value != 0) &&
357 (0 == strncmp (buf, last_bulk, sizeof (last_bulk))))
360 if ((GNUNET_TIME_absolute_get_duration (last_bulk_time).value >
361 BULK_DELAY_THRESHOLD)
362 || (last_bulk_repeat > BULK_REPEAT_THRESHOLD))
368 strncpy (last_bulk, buf, sizeof (last_bulk));
369 last_bulk_repeat = 0;
370 last_bulk_kind = kind;
371 last_bulk_time = GNUNET_TIME_absolute_get ();
372 last_bulk_comp = comp;
373 output_message (kind, comp, date, buf);
381 * @param kind how serious is the error?
382 * @param message what is the message (format string)
383 * @param ... arguments for format string
386 GNUNET_log (enum GNUNET_ErrorType kind, const char *message, ...)
389 va_start (va, message);
390 mylog (kind, component, message, va);
396 * Log function that specifies an alternative component.
397 * This function should be used by plugins.
399 * @param kind how serious is the error?
400 * @param comp component responsible for generating the message
401 * @param message what is the message (format string)
402 * @param ... arguments for format string
405 GNUNET_log_from (enum GNUNET_ErrorType kind,
406 const char *comp, const char *message, ...)
409 va_start (va, message);
410 mylog (kind, comp, message, va);
416 * Convert error type to string.
418 * @param kind type to convert
419 * @return string corresponding to the type
422 GNUNET_error_type_to_string (enum GNUNET_ErrorType kind)
424 if ((kind & GNUNET_ERROR_TYPE_ERROR) > 0)
426 if ((kind & GNUNET_ERROR_TYPE_WARNING) > 0)
428 if ((kind & GNUNET_ERROR_TYPE_INFO) > 0)
430 if ((kind & GNUNET_ERROR_TYPE_DEBUG) > 0)
437 * Convert a hash to a string (for printing debug messages).
438 * This is one of the very few calls in the entire API that is
441 * @param pid the peer identity
442 * @return string form; will be overwritten by next call to GNUNET_h2s.
445 GNUNET_h2s (const GNUNET_HashCode *pid)
447 static struct GNUNET_CRYPTO_HashAsciiEncoded ret;
448 GNUNET_CRYPTO_hash_to_enc (pid, &ret);
449 ret.encoding[8] = '\0';
450 return (const char *) ret.encoding;
455 * Convert a peer identity to a string (for printing debug messages).
456 * This is one of the very few calls in the entire API that is
459 * @param pid the peer identity
460 * @return string form of the pid; will be overwritten by next
461 * call to GNUNET_i2s.
464 GNUNET_i2s (const struct GNUNET_PeerIdentity *pid)
466 static struct GNUNET_CRYPTO_HashAsciiEncoded ret;
467 GNUNET_CRYPTO_hash_to_enc (&pid->hashPubKey, &ret);
468 ret.encoding[4] = '\0';
469 return (const char *) ret.encoding;
475 * Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string
476 * (for printing debug messages). This is one of the very few calls
477 * in the entire API that is NOT reentrant!
479 * @param addr the address
480 * @param addrlen the length of the address
481 * @return nicely formatted string for the address
482 * will be overwritten by next call to GNUNET_a2s.
484 const char *GNUNET_a2s (const struct sockaddr *addr,
487 static char buf[INET6_ADDRSTRLEN+8];
489 const struct sockaddr_in * v4;
490 const struct sockaddr_in6 *v6;
493 return _("unknown address");
494 switch (addr->sa_family)
497 v4 = (const struct sockaddr_in*)addr;
498 inet_ntop(AF_INET, &v4->sin_addr, buf, INET_ADDRSTRLEN);
499 if (0 == ntohs(v4->sin_port))
502 sprintf (b2, "%u", ntohs(v4->sin_port));
506 v6 = (const struct sockaddr_in6*)addr;
508 inet_ntop(AF_INET6, &v6->sin6_addr, &buf[1], INET6_ADDRSTRLEN);
509 if (0 == ntohs(v6->sin6_port))
512 sprintf (b2, "%u", ntohs(v6->sin6_port));
516 return _("invalid address");
524 void __attribute__ ((constructor))
525 GNUNET_util_cl_init()
527 GNUNET_stderr = stderr;
530 /* end of common_logging.c */