Back to index

tor  0.2.3.19-rc
torlog.h
Go to the documentation of this file.
00001 /* Copyright (c) 2001, Matej Pfajfar.
00002  * Copyright (c) 2001-2004, Roger Dingledine.
00003  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
00004  * Copyright (c) 2007-2012, The Tor Project, Inc. */
00005 /* See LICENSE for licensing information */
00006 
00013 #ifndef _TOR_LOG_H
00014 
00015 #include "compat.h"
00016 
00017 #ifdef HAVE_SYSLOG_H
00018 #include <syslog.h>
00019 #define LOG_WARN LOG_WARNING
00020 #if LOG_DEBUG < LOG_ERR
00021 #error "Your syslog.h thinks high numbers are more important.  " \
00022        "We aren't prepared to deal with that."
00023 #endif
00024 #else
00025 /* Note: Syslog's logging code refers to priorities, with 0 being the most
00026  * important.  Thus, all our comparisons needed to be reversed when we added
00027  * syslog support.
00028  *
00029  * The upshot of this is that comments about log levels may be messed up: for
00030  * "maximum severity" read "most severe" and "numerically *lowest* severity".
00031  */
00032 
00035 #define LOG_DEBUG   7
00036 
00038 #define LOG_INFO    6
00039 
00043 #define LOG_NOTICE  5
00044 
00046 #define LOG_WARN    4
00047 
00049 #define LOG_ERR     3
00050 #endif
00051 
00052 /* Logging domains */
00053 
00055 #define LD_GENERAL  (1u<<0)
00056 
00057 #define LD_CRYPTO   (1u<<1)
00058 
00059 #define LD_NET      (1u<<2)
00060 
00061 #define LD_CONFIG   (1u<<3)
00062 
00063 #define LD_FS       (1u<<4)
00064 
00065 #define LD_PROTOCOL (1u<<5)
00066 
00067 #define LD_MM       (1u<<6)
00068 
00069 #define LD_HTTP     (1u<<7)
00070 
00071 #define LD_APP      (1u<<8)
00072 
00073 #define LD_CONTROL  (1u<<9)
00074 
00075 #define LD_CIRC     (1u<<10)
00076 
00077 #define LD_REND     (1u<<11)
00078 
00079 #define LD_BUG      (1u<<12)
00080 
00081 #define LD_DIR      (1u<<13)
00082 
00083 #define LD_DIRSERV  (1u<<14)
00084 
00085 #define LD_OR       (1u<<15)
00086 
00087 #define LD_EDGE     (1u<<16)
00088 #define LD_EXIT     LD_EDGE
00089 
00090 #define LD_ACCT     (1u<<17)
00091 
00092 #define LD_HIST     (1u<<18)
00093 
00094 #define LD_HANDSHAKE (1u<<19)
00095 
00096 #define LD_HEARTBEAT (1u<<20)
00097 
00098 #define N_LOGGING_DOMAINS 21
00099 
00102 #define LD_NOCB (1u<<31)
00103 
00105 typedef uint32_t log_domain_mask_t;
00106 
00109 typedef struct log_severity_list_t {
00112   log_domain_mask_t masks[LOG_DEBUG-LOG_ERR+1];
00113 } log_severity_list_t;
00114 
00115 #ifdef LOG_PRIVATE
00116 
00118 #define SEVERITY_MASK_IDX(sev) ((sev) - LOG_ERR)
00119 #endif
00120 
00122 typedef void (*log_callback)(int severity, uint32_t domain, const char *msg);
00123 
00124 void init_logging(void);
00125 int parse_log_level(const char *level);
00126 const char *log_level_to_string(int level);
00127 int parse_log_severity_config(const char **cfg,
00128                               log_severity_list_t *severity_out);
00129 void set_log_severity_config(int minSeverity, int maxSeverity,
00130                              log_severity_list_t *severity_out);
00131 void add_stream_log(const log_severity_list_t *severity, const char *name,
00132                     int fd);
00133 int add_file_log(const log_severity_list_t *severity, const char *filename);
00134 #ifdef HAVE_SYSLOG_H
00135 int add_syslog_log(const log_severity_list_t *severity);
00136 #endif
00137 int add_callback_log(const log_severity_list_t *severity, log_callback cb);
00138 void logs_set_domain_logging(int enabled);
00139 int get_min_log_level(void);
00140 void switch_logs_debug(void);
00141 void logs_free_all(void);
00142 void add_temp_log(int min_severity);
00143 void close_temp_logs(void);
00144 void rollback_log_changes(void);
00145 void mark_logs_temp(void);
00146 void change_callback_log_severity(int loglevelMin, int loglevelMax,
00147                                   log_callback cb);
00148 void flush_pending_log_callbacks(void);
00149 void log_set_application_name(const char *name);
00150 void set_log_time_granularity(int granularity_msec);
00151 
00152 void tor_log(int severity, log_domain_mask_t domain, const char *format, ...)
00153   CHECK_PRINTF(3,4);
00154 #define log tor_log /* hack it so we don't conflict with log() as much */
00155 
00156 #if defined(__GNUC__) || defined(RUNNING_DOXYGEN)
00157 extern int _log_global_min_severity;
00158 
00159 void _log_fn(int severity, log_domain_mask_t domain,
00160              const char *funcname, const char *format, ...)
00161   CHECK_PRINTF(4,5);
00164 #define log_fn(severity, domain, args...)               \
00165   _log_fn(severity, domain, __PRETTY_FUNCTION__, args)
00166 #define log_debug(domain, args...)                                      \
00167   STMT_BEGIN                                                            \
00168     if (PREDICT_UNLIKELY(_log_global_min_severity == LOG_DEBUG))        \
00169       _log_fn(LOG_DEBUG, domain, __PRETTY_FUNCTION__, args);            \
00170   STMT_END
00171 #define log_info(domain, args...)                           \
00172   _log_fn(LOG_INFO, domain, __PRETTY_FUNCTION__, args)
00173 #define log_notice(domain, args...)                         \
00174   _log_fn(LOG_NOTICE, domain, __PRETTY_FUNCTION__, args)
00175 #define log_warn(domain, args...)                           \
00176   _log_fn(LOG_WARN, domain, __PRETTY_FUNCTION__, args)
00177 #define log_err(domain, args...)                            \
00178   _log_fn(LOG_ERR, domain, __PRETTY_FUNCTION__, args)
00179 
00180 #else /* ! defined(__GNUC__) */
00181 
00182 void _log_fn(int severity, log_domain_mask_t domain, const char *format, ...);
00183 void _log_debug(log_domain_mask_t domain, const char *format, ...);
00184 void _log_info(log_domain_mask_t domain, const char *format, ...);
00185 void _log_notice(log_domain_mask_t domain, const char *format, ...);
00186 void _log_warn(log_domain_mask_t domain, const char *format, ...);
00187 void _log_err(log_domain_mask_t domain, const char *format, ...);
00188 
00189 #if defined(_MSC_VER) && _MSC_VER < 1300
00190 /* MSVC 6 and earlier don't have __func__, or even __LINE__. */
00191 #define log_fn _log_fn
00192 #define log_debug _log_debug
00193 #define log_info _log_info
00194 #define log_notice _log_notice
00195 #define log_warn _log_warn
00196 #define log_err _log_err
00197 #else
00198 /* We don't have GCC's varargs macros, so use a global variable to pass the
00199  * function name to log_fn */
00200 extern const char *_log_fn_function_name;
00201 /* We abuse the comma operator here, since we can't use the standard
00202  * do {...} while (0) trick to wrap this macro, since the macro can't take
00203  * arguments. */
00204 #define log_fn (_log_fn_function_name=__func__),_log_fn
00205 #define log_debug (_log_fn_function_name=__func__),_log_debug
00206 #define log_info (_log_fn_function_name=__func__),_log_info
00207 #define log_notice (_log_fn_function_name=__func__),_log_notice
00208 #define log_warn (_log_fn_function_name=__func__),_log_warn
00209 #define log_err (_log_fn_function_name=__func__),_log_err
00210 #endif
00211 
00212 #endif /* !GNUC */
00213 
00214 # define _TOR_LOG_H
00215 #endif
00216