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 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 * Linked list of active loggers.
70 * This is a linked list.
72 struct CustomLogger *next;
86 * The last "bulk" error message that we have been logging.
87 * Note that this message maybe truncated to the first BULK_TRACK_SIZE
88 * characters, in which case it is NOT 0-terminated!
90 static char last_bulk[BULK_TRACK_SIZE];
93 * Type of the last bulk message.
95 static enum GNUNET_ErrorType last_bulk_kind;
98 * Time of the last bulk error message (0 for none)
100 static struct GNUNET_TIME_Absolute last_bulk_time;
103 * Number of times that bulk message has been repeated since.
105 static unsigned int last_bulk_repeat;
108 * Component when the last bulk was logged. Will be 0-terminated.
110 static char last_bulk_comp[COMP_TRACK_SIZE+1];
115 static char *component;
120 static enum GNUNET_ErrorType min_level;
123 * Linked list of our custom loggres.
125 static struct CustomLogger *loggers;
128 * Number of log calls to ignore.
130 static unsigned int skip_log;
133 * File descriptor to use for "stderr", or NULL for none.
135 static FILE *GNUNET_stderr;
138 * Convert a textual description of a loglevel
139 * to the respective GNUNET_GE_KIND.
141 * @param log loglevel to parse
142 * @return GNUNET_GE_INVALID if log does not parse
144 static enum GNUNET_ErrorType
145 get_type (const char *log)
147 if (0 == strcasecmp (log, _("DEBUG")))
148 return GNUNET_ERROR_TYPE_DEBUG;
149 if (0 == strcasecmp (log, _("INFO")))
150 return GNUNET_ERROR_TYPE_INFO;
151 if (0 == strcasecmp (log, _("WARNING")))
152 return GNUNET_ERROR_TYPE_WARNING;
153 if (0 == strcasecmp (log, _("ERROR")))
154 return GNUNET_ERROR_TYPE_ERROR;
155 return GNUNET_ERROR_TYPE_INVALID;
162 * @param comp default component to use
163 * @param loglevel what types of messages should be logged
164 * @param logfile which file to write log messages to (can be NULL)
165 * @return GNUNET_OK on success
168 GNUNET_log_setup (const char *comp, const char *loglevel, const char *logfile)
173 GNUNET_free_non_null (component);
174 GNUNET_asprintf (&component,
178 min_level = get_type (loglevel);
181 dirwarn = (GNUNET_OK != GNUNET_DISK_directory_create_for_file (logfile));
182 altlog = FOPEN (logfile, "a");
185 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "fopen", logfile);
187 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
188 _("Failed to create or access directory for log file `%s'\n"),
190 return GNUNET_SYSERR;
192 if (GNUNET_stderr != NULL)
193 fclose (GNUNET_stderr);
194 GNUNET_stderr = altlog;
199 * Add a custom logger.
201 * @param logger log function
202 * @param logger_cls closure for logger
205 GNUNET_logger_add (GNUNET_Logger logger, void *logger_cls)
207 struct CustomLogger *entry;
209 entry = GNUNET_malloc (sizeof (struct CustomLogger));
210 entry->logger = logger;
211 entry->logger_cls = logger_cls;
212 entry->next = loggers;
217 * Remove a custom logger.
219 * @param logger log function
220 * @param logger_cls closure for logger
223 GNUNET_logger_remove (GNUNET_Logger logger, void *logger_cls)
225 struct CustomLogger *pos;
226 struct CustomLogger *prev;
230 while ((pos != NULL) &&
231 ((pos->logger != logger) || (pos->logger_cls != logger_cls)))
236 GNUNET_assert (pos != NULL);
240 prev->next = pos->next;
246 * Actually output the log message.
248 * @param kind how severe was the issue
249 * @param comp component responsible
250 * @param datestr current date/time
251 * @param msg the actual message
254 output_message (enum GNUNET_ErrorType kind,
255 const char *comp, const char *datestr, const char *msg)
257 struct CustomLogger *pos;
258 if (GNUNET_stderr != NULL)
259 fprintf (GNUNET_stderr, "%s %s %s %s", datestr, comp,
260 GNUNET_error_type_to_string (kind), msg);
264 pos->logger (pos->logger_cls, kind, comp, datestr, msg);
271 * Flush an existing bulk report to the output.
273 * @param datestr our current timestamp
276 flush_bulk (const char *datestr)
278 char msg[DATE_STR_SIZE + BULK_TRACK_SIZE + 256];
283 if ((last_bulk_time.value == 0) || (last_bulk_repeat == 0))
286 last = memchr (last_bulk, '\0', BULK_TRACK_SIZE);
288 last = &last_bulk[BULK_TRACK_SIZE - 1];
289 else if (last != last_bulk)
297 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration
299 snprintf (msg, sizeof (msg),
300 _("Message `%.*s' repeated %u times in the last %s\n"),
301 BULK_TRACK_SIZE, last_bulk, last_bulk_repeat, ft);
305 output_message (last_bulk_kind, last_bulk_comp, datestr, msg);
306 last_bulk_time = GNUNET_TIME_absolute_get ();
307 last_bulk_repeat = 0;
312 * Ignore the next n calls to the log function.
314 * @param n number of log calls to ignore
315 * @param check_reset GNUNET_YES to assert that the log skip counter is currently zero
318 GNUNET_log_skip (unsigned int n, int check_reset)
324 ok = (0 == skip_log);
335 * Output a log message using the default mechanism.
337 * @param kind how severe was the issue
338 * @param comp component responsible
339 * @param message the actual message
340 * @param va arguments to the format string "message"
343 mylog (enum GNUNET_ErrorType kind,
344 const char *comp, const char *message, va_list va)
346 char date[DATE_STR_SIZE];
358 if ((kind & (~GNUNET_ERROR_TYPE_BULK)) > min_level)
361 size = VSNPRINTF (NULL, 0, message, vacp) + 1;
366 VSNPRINTF (buf, size, message, va);
368 memset (date, 0, DATE_STR_SIZE);
369 tmptr = localtime (&timetmp);
370 strftime (date, DATE_STR_SIZE, "%b %d %H:%M:%S", tmptr);
371 if ((0 != (kind & GNUNET_ERROR_TYPE_BULK)) &&
372 (last_bulk_time.value != 0) &&
373 (0 == strncmp (buf, last_bulk, sizeof (last_bulk))))
376 if ((GNUNET_TIME_absolute_get_duration (last_bulk_time).value >
377 BULK_DELAY_THRESHOLD)
378 || (last_bulk_repeat > BULK_REPEAT_THRESHOLD))
384 strncpy (last_bulk, buf, sizeof (last_bulk));
385 last_bulk_repeat = 0;
386 last_bulk_kind = kind;
387 last_bulk_time = GNUNET_TIME_absolute_get ();
388 strncpy (last_bulk_comp, comp, sizeof (last_bulk_comp));
389 output_message (kind, comp, date, buf);
397 * @param kind how serious is the error?
398 * @param message what is the message (format string)
399 * @param ... arguments for format string
402 GNUNET_log (enum GNUNET_ErrorType kind, const char *message, ...)
405 va_start (va, message);
406 mylog (kind, component, message, va);
412 * Log function that specifies an alternative component.
413 * This function should be used by plugins.
415 * @param kind how serious is the error?
416 * @param comp component responsible for generating the message
417 * @param message what is the message (format string)
418 * @param ... arguments for format string
421 GNUNET_log_from (enum GNUNET_ErrorType kind,
422 const char *comp, const char *message, ...)
425 va_start (va, message);
426 mylog (kind, comp, message, va);
432 * Convert error type to string.
434 * @param kind type to convert
435 * @return string corresponding to the type
438 GNUNET_error_type_to_string (enum GNUNET_ErrorType kind)
440 if ((kind & GNUNET_ERROR_TYPE_ERROR) > 0)
442 if ((kind & GNUNET_ERROR_TYPE_WARNING) > 0)
444 if ((kind & GNUNET_ERROR_TYPE_INFO) > 0)
446 if ((kind & GNUNET_ERROR_TYPE_DEBUG) > 0)
453 * Convert a hash to a string (for printing debug messages).
454 * This is one of the very few calls in the entire API that is
457 * @param hc the hash code
458 * @return string form; will be overwritten by next call to GNUNET_h2s.
461 GNUNET_h2s (const GNUNET_HashCode * hc)
463 static struct GNUNET_CRYPTO_HashAsciiEncoded ret;
464 GNUNET_CRYPTO_hash_to_enc (hc, &ret);
465 ret.encoding[8] = '\0';
466 return (const char *) ret.encoding;
471 * Convert a peer identity to a string (for printing debug messages).
472 * This is one of the very few calls in the entire API that is
475 * @param pid the peer identity
476 * @return string form of the pid; will be overwritten by next
477 * call to GNUNET_i2s.
480 GNUNET_i2s (const struct GNUNET_PeerIdentity *pid)
482 static struct GNUNET_CRYPTO_HashAsciiEncoded ret;
483 GNUNET_CRYPTO_hash_to_enc (&pid->hashPubKey, &ret);
484 ret.encoding[4] = '\0';
485 return (const char *) ret.encoding;
491 * Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string
492 * (for printing debug messages). This is one of the very few calls
493 * in the entire API that is NOT reentrant!
495 * @param addr the address
496 * @param addrlen the length of the address
497 * @return nicely formatted string for the address
498 * will be overwritten by next call to GNUNET_a2s.
501 GNUNET_a2s (const struct sockaddr *addr, socklen_t addrlen)
503 static char buf[INET6_ADDRSTRLEN + 8];
505 const struct sockaddr_in *v4;
506 const struct sockaddr_in6 *v6;
509 return _("unknown address");
510 switch (addr->sa_family)
513 v4 = (const struct sockaddr_in *) addr;
514 inet_ntop (AF_INET, &v4->sin_addr, buf, INET_ADDRSTRLEN);
515 if (0 == ntohs (v4->sin_port))
518 sprintf (b2, "%u", ntohs (v4->sin_port));
522 v6 = (const struct sockaddr_in6 *) addr;
524 inet_ntop (AF_INET6, &v6->sin6_addr, &buf[1], INET6_ADDRSTRLEN);
525 if (0 == ntohs (v6->sin6_port))
528 sprintf (b2, "%u", ntohs (v6->sin6_port));
532 return _("invalid address");
540 void __attribute__ ((constructor)) GNUNET_util_cl_init ()
542 GNUNET_stderr = stderr;
545 /* end of common_logging.c */