1 /* Copyright (c) 2001, Matej Pfajfar.
2 * Copyright (c) 2001-2004, Roger Dingledine.
3 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4 * Copyright (c) 2007-2013, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
10 * \brief Headers for log.c
19 #define LOG_WARN LOG_WARNING
20 #if LOG_DEBUG < LOG_ERR
21 #error "Your syslog.h thinks high numbers are more important. " \
22 "We aren't prepared to deal with that."
25 /* Note: Syslog's logging code refers to priorities, with 0 being the most
26 * important. Thus, all our comparisons needed to be reversed when we added
29 * The upshot of this is that comments about log levels may be messed up: for
30 * "maximum severity" read "most severe" and "numerically *lowest* severity".
33 /** Debug-level severity: for hyper-verbose messages of no interest to
34 * anybody but developers. */
36 /** Info-level severity: for messages that appear frequently during normal
39 /** Notice-level severity: for messages that appear infrequently
40 * during normal operation; that the user will probably care about;
41 * and that are not errors.
44 /** Warn-level severity: for messages that only appear when something has gone
47 /** Error-level severity: for messages that only appear when something has gone
48 * very wrong, and the Tor process can no longer proceed. */
54 /** Catch-all for miscellaneous events and fatal errors. */
55 #define LD_GENERAL (1u<<0)
56 /** The cryptography subsystem. */
57 #define LD_CRYPTO (1u<<1)
59 #define LD_NET (1u<<2)
60 /** Parsing and acting on our configuration. */
61 #define LD_CONFIG (1u<<3)
62 /** Reading and writing from the filesystem. */
64 /** Other servers' (non)compliance with the Tor protocol. */
65 #define LD_PROTOCOL (1u<<5)
66 /** Memory management. */
68 /** HTTP implementation. */
69 #define LD_HTTP (1u<<7)
70 /** Application (socks) requests. */
71 #define LD_APP (1u<<8)
72 /** Communication via the controller protocol. */
73 #define LD_CONTROL (1u<<9)
74 /** Building, using, and managing circuits. */
75 #define LD_CIRC (1u<<10)
76 /** Hidden services. */
77 #define LD_REND (1u<<11)
78 /** Internal errors in this Tor process. */
79 #define LD_BUG (1u<<12)
80 /** Learning and using information about Tor servers. */
81 #define LD_DIR (1u<<13)
82 /** Learning and using information about Tor servers. */
83 #define LD_DIRSERV (1u<<14)
84 /** Onion routing protocol. */
85 #define LD_OR (1u<<15)
86 /** Generic edge-connection functionality. */
87 #define LD_EDGE (1u<<16)
88 #define LD_EXIT LD_EDGE
89 /** Bandwidth accounting. */
90 #define LD_ACCT (1u<<17)
92 #define LD_HIST (1u<<18)
94 #define LD_HANDSHAKE (1u<<19)
95 /** Heartbeat messages */
96 #define LD_HEARTBEAT (1u<<20)
97 /** Abstract channel_t code */
98 #define LD_CHANNEL (1u<<21)
99 /** Number of logging domains in the code. */
100 #define N_LOGGING_DOMAINS 22
102 /** This log message is not safe to send to a callback-based logger
103 * immediately. Used as a flag, not a log domain. */
104 #define LD_NOCB (1u<<31)
106 /** Mask of zero or more log domains, OR'd together. */
107 typedef uint32_t log_domain_mask_t
;
109 /** Configures which severities are logged for each logging domain for a given
111 typedef struct log_severity_list_t
{
112 /** For each log severity, a bitmask of which domains a given logger is
114 log_domain_mask_t masks
[LOG_DEBUG
-LOG_ERR
+1];
115 } log_severity_list_t
;
118 /** Given a severity, yields an index into log_severity_list_t.masks to use
119 * for that severity. */
120 #define SEVERITY_MASK_IDX(sev) ((sev) - LOG_ERR)
123 /** Callback type used for add_callback_log. */
124 typedef void (*log_callback
)(int severity
, uint32_t domain
, const char *msg
);
126 void init_logging(void);
127 int parse_log_level(const char *level
);
128 const char *log_level_to_string(int level
);
129 int parse_log_severity_config(const char **cfg
,
130 log_severity_list_t
*severity_out
);
131 void set_log_severity_config(int minSeverity
, int maxSeverity
,
132 log_severity_list_t
*severity_out
);
133 void add_stream_log(const log_severity_list_t
*severity
, const char *name
,
135 int add_file_log(const log_severity_list_t
*severity
, const char *filename
);
137 int add_syslog_log(const log_severity_list_t
*severity
);
139 int add_callback_log(const log_severity_list_t
*severity
, log_callback cb
);
140 void logs_set_domain_logging(int enabled
);
141 int get_min_log_level(void);
142 void switch_logs_debug(void);
143 void logs_free_all(void);
144 void add_temp_log(int min_severity
);
145 void close_temp_logs(void);
146 void rollback_log_changes(void);
147 void mark_logs_temp(void);
148 void change_callback_log_severity(int loglevelMin
, int loglevelMax
,
150 void flush_pending_log_callbacks(void);
151 void log_set_application_name(const char *name
);
152 void set_log_time_granularity(int granularity_msec
);
154 void tor_log(int severity
, log_domain_mask_t domain
, const char *format
, ...)
157 #if defined(__GNUC__) || defined(RUNNING_DOXYGEN)
158 extern int log_global_min_severity_
;
160 void log_fn_(int severity
, log_domain_mask_t domain
,
161 const char *funcname
, const char *format
, ...)
164 void log_fn_ratelim_(struct ratelim_t
*ratelim
, int severity
,
165 log_domain_mask_t domain
, const char *funcname
,
166 const char *format
, ...)
168 /** Log a message at level <b>severity</b>, using a pretty-printed version
169 * of the current function name. */
170 #define log_fn(severity, domain, args...) \
171 log_fn_(severity, domain, __PRETTY_FUNCTION__, args)
172 /** As log_fn, but use <b>ratelim</b> (an instance of ratelim_t) to control
173 * the frequency at which messages can appear.
175 #define log_fn_ratelim(ratelim, severity, domain, args...) \
176 log_fn_ratelim_(ratelim, severity, domain, __PRETTY_FUNCTION__, args)
177 #define log_debug(domain, args...) \
179 if (PREDICT_UNLIKELY(log_global_min_severity_ == LOG_DEBUG)) \
180 log_fn_(LOG_DEBUG, domain, __PRETTY_FUNCTION__, args); \
182 #define log_info(domain, args...) \
183 log_fn_(LOG_INFO, domain, __PRETTY_FUNCTION__, args)
184 #define log_notice(domain, args...) \
185 log_fn_(LOG_NOTICE, domain, __PRETTY_FUNCTION__, args)
186 #define log_warn(domain, args...) \
187 log_fn_(LOG_WARN, domain, __PRETTY_FUNCTION__, args)
188 #define log_err(domain, args...) \
189 log_fn_(LOG_ERR, domain, __PRETTY_FUNCTION__, args)
191 #else /* ! defined(__GNUC__) */
193 void log_fn_(int severity
, log_domain_mask_t domain
, const char *format
, ...);
195 void log_fn_ratelim_(struct ratelim_t
*ratelim
, int severity
,
196 log_domain_mask_t domain
, const char *format
, ...);
197 void log_debug_(log_domain_mask_t domain
, const char *format
, ...);
198 void log_info_(log_domain_mask_t domain
, const char *format
, ...);
199 void log_notice_(log_domain_mask_t domain
, const char *format
, ...);
200 void log_warn_(log_domain_mask_t domain
, const char *format
, ...);
201 void log_err_(log_domain_mask_t domain
, const char *format
, ...);
203 #if defined(_MSC_VER) && _MSC_VER < 1300
204 /* MSVC 6 and earlier don't have __func__, or even __LINE__. */
205 #define log_fn log_fn_
206 #define log_fn_ratelim log_fn_ratelim_
207 #define log_debug log_debug_
208 #define log_info log_info_
209 #define log_notice log_notice_
210 #define log_warn log_warn_
211 #define log_err log_err_
213 /* We don't have GCC's varargs macros, so use a global variable to pass the
214 * function name to log_fn */
215 extern const char *log_fn_function_name_
;
216 /* We abuse the comma operator here, since we can't use the standard
217 * do {...} while (0) trick to wrap this macro, since the macro can't take
219 #define log_fn (log_fn_function_name_=__func__),log_fn_
220 #define log_fn_ratelim (log_fn_function_name_=__func__),log_fn_ratelim_
221 #define log_debug (log_fn_function_name_=__func__),log_debug_
222 #define log_info (log_fn_function_name_=__func__),log_info_
223 #define log_notice (log_fn_function_name_=__func__),log_notice_
224 #define log_warn (log_fn_function_name_=__func__),log_warn_
225 #define log_err (log_fn_function_name_=__func__),log_err_
230 # define TOR_TORLOG_H