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;
126 static FILE *GNUNET_stderr;
129 * Convert a textual description of a loglevel
130 * to the respective GNUNET_GE_KIND.
131 * @returns GNUNET_GE_INVALID if log does not parse
133 static enum GNUNET_ErrorType
134 get_type (const char *log)
136 if (0 == strcasecmp (log, _("DEBUG")))
137 return GNUNET_ERROR_TYPE_DEBUG;
138 if (0 == strcasecmp (log, _("INFO")))
139 return GNUNET_ERROR_TYPE_INFO;
140 if (0 == strcasecmp (log, _("WARNING")))
141 return GNUNET_ERROR_TYPE_WARNING;
142 if (0 == strcasecmp (log, _("ERROR")))
143 return GNUNET_ERROR_TYPE_ERROR;
144 return GNUNET_ERROR_TYPE_INVALID;
150 * @param comp default component to use
151 * @param loglevel what types of messages should be logged
154 GNUNET_log_setup (const char *comp, const char *loglevel, const char *logfile)
159 min_level = get_type (loglevel);
162 altlog = fopen (logfile, "a");
165 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "fopen", logfile);
166 return GNUNET_SYSERR;
168 if (GNUNET_stderr != NULL)
169 fclose (GNUNET_stderr);
170 GNUNET_stderr = altlog;
175 * Add a custom logger.
177 * @param logger log function
178 * @param logger_cls closure for logger
181 GNUNET_logger_add (GNUNET_Logger logger, void *logger_cls)
183 struct CustomLogger *entry;
185 entry = GNUNET_malloc (sizeof (struct CustomLogger));
186 entry->logger = logger;
187 entry->logger_cls = logger_cls;
188 entry->next = loggers;
193 * Remove a custom logger.
195 * @param logger log function
196 * @param logger_cls closure for logger
199 GNUNET_logger_remove (GNUNET_Logger logger, void *logger_cls)
201 struct CustomLogger *pos;
202 struct CustomLogger *prev;
206 while ((pos != NULL) &&
207 ((pos->logger != logger) || (pos->logger_cls != logger_cls)))
212 GNUNET_assert (pos != NULL);
216 prev->next = pos->next;
221 output_message (enum GNUNET_ErrorType kind,
222 const char *comp, const char *datestr, const char *msg)
224 struct CustomLogger *pos;
225 if (GNUNET_stderr != NULL)
226 fprintf (GNUNET_stderr, "%s %s %s %s", datestr, comp,
227 GNUNET_error_type_to_string (kind), msg);
231 pos->logger (pos->logger_cls, kind, comp, datestr, msg);
237 flush_bulk (const char *datestr)
239 char msg[DATE_STR_SIZE + BULK_TRACK_SIZE + 256];
244 if ((last_bulk_time.value == 0) || (last_bulk_repeat == 0))
247 last = memchr (last_bulk, '\0', BULK_TRACK_SIZE);
249 last = &last_bulk[BULK_TRACK_SIZE - 1];
250 else if (last != last_bulk)
258 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration
260 snprintf (msg, sizeof (msg),
261 _("Message `%.*s' repeated %u times in the last %s\n"),
262 BULK_TRACK_SIZE, last_bulk, last_bulk_repeat, ft);
266 output_message (last_bulk_kind, last_bulk_comp, datestr, msg);
267 last_bulk_time = GNUNET_TIME_absolute_get ();
268 last_bulk_repeat = 0;
273 * Ignore the next n calls to the log function.
275 * @param n number of log calls to ignore
278 GNUNET_log_skip (unsigned int n)
284 ok = (0 == skip_log);
293 mylog (enum GNUNET_ErrorType kind,
294 const char *comp, const char *message, va_list va)
296 char date[DATE_STR_SIZE];
308 if ((kind & (~GNUNET_ERROR_TYPE_BULK)) > min_level)
311 size = VSNPRINTF (NULL, 0, message, vacp) + 1;
316 VSNPRINTF (buf, size, message, va);
318 memset (date, 0, DATE_STR_SIZE);
319 tmptr = localtime (&timetmp);
320 strftime (date, DATE_STR_SIZE, "%b %d %H:%M:%S", tmptr);
321 if ((0 != (kind & GNUNET_ERROR_TYPE_BULK)) &&
322 (last_bulk_time.value != 0) &&
323 (0 == strncmp (buf, last_bulk, sizeof (last_bulk))))
326 if ((GNUNET_TIME_absolute_get_duration (last_bulk_time).value >
327 BULK_DELAY_THRESHOLD)
328 || (last_bulk_repeat > BULK_REPEAT_THRESHOLD))
334 strncpy (last_bulk, buf, sizeof (last_bulk));
335 last_bulk_repeat = 0;
336 last_bulk_kind = kind;
337 last_bulk_time = GNUNET_TIME_absolute_get ();
338 last_bulk_comp = comp;
339 output_message (kind, comp, date, buf);
345 GNUNET_log (enum GNUNET_ErrorType kind, const char *message, ...)
348 va_start (va, message);
349 mylog (kind, component, message, va);
355 GNUNET_log_from (enum GNUNET_ErrorType kind,
356 const char *comp, const char *message, ...)
359 va_start (va, message);
360 mylog (kind, comp, message, va);
366 * Convert KIND to String
369 GNUNET_error_type_to_string (enum GNUNET_ErrorType kind)
371 if ((kind & GNUNET_ERROR_TYPE_ERROR) > 0)
373 if ((kind & GNUNET_ERROR_TYPE_WARNING) > 0)
375 if ((kind & GNUNET_ERROR_TYPE_INFO) > 0)
377 if ((kind & GNUNET_ERROR_TYPE_DEBUG) > 0)
384 * Convert a peer identity to a string (for printing debug messages).
385 * This is one of the very few calls in the entire API that is
388 * @param pid the peer identity
389 * @return string form of the pid; will be overwritten by next
390 * call to GNUNET_i2s.
393 GNUNET_i2s (const struct GNUNET_PeerIdentity *pid)
395 static struct GNUNET_CRYPTO_HashAsciiEncoded ret;
396 GNUNET_CRYPTO_hash_to_enc (&pid->hashPubKey, &ret);
397 ret.encoding[4] = '\0';
398 return (const char *) ret.encoding;
404 * Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string
405 * (for printing debug messages). This is one of the very few calls
406 * in the entire API that is NOT reentrant!
408 * @param addr the address
409 * @param addrlen the length of the address
410 * @return nicely formatted string for the address
411 * will be overwritten by next call to GNUNET_a2s.
413 const char *GNUNET_a2s (const struct sockaddr *addr,
416 static char buf[INET6_ADDRSTRLEN+8];
418 const struct sockaddr_in * v4;
419 const struct sockaddr_in6 *v6;
420 switch (addr->sa_family)
423 v4 = (const struct sockaddr_in*)addr;
424 inet_ntop(AF_INET, &v4->sin_addr, buf, INET_ADDRSTRLEN);
425 if (0 == ntohs(v4->sin_port))
428 sprintf (b2, "%u", ntohs(v4->sin_port));
432 v6 = (const struct sockaddr_in6*)addr;
434 inet_ntop(AF_INET6, &v6->sin6_addr, &buf[1], INET6_ADDRSTRLEN);
435 if (0 == ntohs(v6->sin6_port))
438 sprintf (b2, "%u", ntohs(v6->sin6_port));
442 return _("invalid address");
450 void __attribute__ ((constructor))
451 GNUNET_util_cl_init()
453 GNUNET_stderr = stderr;
456 /* end of common_logging.c */