/*
This file is part of GNUnet.
(C) 2006, 2008, 2009 Christian Grothoff (and other contributing authors)
GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 2, or (at your
option) any later version.
GNUnet is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNUnet; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
/**
* @file util/common_logging.c
* @brief error handling API
*
* @author Christian Grothoff
*/
#include "platform.h"
#include "gnunet_common.h"
#include "gnunet_crypto_lib.h"
#include "gnunet_strings_lib.h"
#include "gnunet_time_lib.h"
#include <regex.h>
/**
* After how many milliseconds do we always print
* that "message X was repeated N times"? Use 12h.
*/
#define BULK_DELAY_THRESHOLD (12 * 60 * 60 * 1000)
/**
* After how many repetitions do we always print
* that "message X was repeated N times"? (even if
* we have not yet reached the delay threshold)
*/
#define BULK_REPEAT_THRESHOLD 1000
/**
* How many characters do we use for matching of
* bulk messages?
*/
#define BULK_TRACK_SIZE 256
/**
* How many characters do we use for matching of
* bulk components?
*/
#define COMP_TRACK_SIZE 32
/**
* How many characters can a date/time string
* be at most?
*/
#define DATE_STR_SIZE 64
/**
* How many log files to keep?
*/
#define ROTATION_KEEP 3
#ifndef PATH_MAX
/**
* Assumed maximum path length (for the log file name).
*/
#define PATH_MAX 4096
#endif
/**
* Linked list of active loggers.
*/
struct CustomLogger
{
/**
* This is a linked list.
*/
struct CustomLogger *next;
/**
* Log function.
*/
GNUNET_Logger logger;
/**
* Closure for logger.
*/
void *logger_cls;
};
/**
* The last "bulk" error message that we have been logging.
* Note that this message maybe truncated to the first BULK_TRACK_SIZE
* characters, in which case it is NOT 0-terminated!
*/
static char last_bulk[BULK_TRACK_SIZE];
/**
* Type of the last bulk message.
*/
static enum GNUNET_ErrorType last_bulk_kind;
/**
* Time of the last bulk error message (0 for none)
*/
static struct GNUNET_TIME_Absolute last_bulk_time;
/**
* Number of times that bulk message has been repeated since.
*/
static unsigned int last_bulk_repeat;
/**
* Component when the last bulk was logged. Will be 0-terminated.
*/
static char last_bulk_comp[COMP_TRACK_SIZE + 1];
/**
* Running component.
*/
static char *component;
/**
* Running component (without pid).
*/
static char *component_nopid;
/**
* Format string describing the name of the log file.
*/
static char *log_file_name;
/**
* Minimum log level.
*/
static enum GNUNET_ErrorType min_level;
/**
* Linked list of our custom loggres.
*/
static struct CustomLogger *loggers;
/**
* Number of log calls to ignore.
*/
int skip_log = 0;
/**
* File descriptor to use for "stderr", or NULL for none.
*/
static FILE *GNUNET_stderr;
/**
* Represents a single logging definition
*/
struct LogDef
{
/**
* Component name regex
*/
regex_t component_regex;
/**
* File name regex
*/
regex_t file_regex;
/**
* Function name regex
*/
regex_t function_regex;
/**
* Lowest line at which this definition matches.
* Defaults to 0. Must be <= to_line.
*/
int from_line;
/**
* Highest line at which this definition matches.
* Defaults to INT_MAX. Must be >= from_line.
*/
int to_line;
/**
* Maximal log level allowed for calls that match this definition.
* Calls with higher log level will be disabled.
* Must be >= 0
*/
int level;
/**
* 1 if this definition comes from GNUNET_FORCE_LOG, which means that it
* overrides any configuration options. 0 otherwise.
*/
int force;
};
/**
* Dynamic array of logging definitions
*/
static struct LogDef *logdefs;
/**
* Allocated size of logdefs array (in units)
*/
static int logdefs_size;
/**
* The number of units used in logdefs array.
*/
static int logdefs_len;
/**
* GNUNET_YES if GNUNET_LOG environment variable is already parsed.
*/
static int gnunet_log_parsed;
/**
* GNUNET_YES if GNUNET_FORCE_LOG environment variable is already parsed.
*/
static int gnunet_force_log_parsed;
/**
* GNUNET_YES if at least one definition with forced == 1 is available.
*/
static int gnunet_force_log_present;
#ifdef WINDOWS
/**
* Contains the number of performance counts per second.
*/
static LARGE_INTEGER performance_frequency;