sandbox: refactor string-based option-unchanged tests to use a macro
[tor.git] / src / common / torlog.h
blob34f70f3c0073b353265fa8b1254a325e74fda7c1
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 */
7 /**
8 * \file torlog.h
10 * \brief Headers for log.c
11 **/
13 #ifndef TOR_TORLOG_H
15 #include "compat.h"
16 #include "testsupport.h"
18 #ifdef HAVE_SYSLOG_H
19 #include <syslog.h>
20 #define LOG_WARN LOG_WARNING
21 #if LOG_DEBUG < LOG_ERR
22 #error "Your syslog.h thinks high numbers are more important. " \
23 "We aren't prepared to deal with that."
24 #endif
25 #else
26 /* Note: Syslog's logging code refers to priorities, with 0 being the most
27 * important. Thus, all our comparisons needed to be reversed when we added
28 * syslog support.
30 * The upshot of this is that comments about log levels may be messed up: for
31 * "maximum severity" read "most severe" and "numerically *lowest* severity".
34 /** Debug-level severity: for hyper-verbose messages of no interest to
35 * anybody but developers. */
36 #define LOG_DEBUG 7
37 /** Info-level severity: for messages that appear frequently during normal
38 * operation. */
39 #define LOG_INFO 6
40 /** Notice-level severity: for messages that appear infrequently
41 * during normal operation; that the user will probably care about;
42 * and that are not errors.
44 #define LOG_NOTICE 5
45 /** Warn-level severity: for messages that only appear when something has gone
46 * wrong. */
47 #define LOG_WARN 4
48 /** Error-level severity: for messages that only appear when something has gone
49 * very wrong, and the Tor process can no longer proceed. */
50 #define LOG_ERR 3
51 #endif
53 /* Logging domains */
55 /** Catch-all for miscellaneous events and fatal errors. */
56 #define LD_GENERAL (1u<<0)
57 /** The cryptography subsystem. */
58 #define LD_CRYPTO (1u<<1)
59 /** Networking. */
60 #define LD_NET (1u<<2)
61 /** Parsing and acting on our configuration. */
62 #define LD_CONFIG (1u<<3)
63 /** Reading and writing from the filesystem. */
64 #define LD_FS (1u<<4)
65 /** Other servers' (non)compliance with the Tor protocol. */
66 #define LD_PROTOCOL (1u<<5)
67 /** Memory management. */
68 #define LD_MM (1u<<6)
69 /** HTTP implementation. */
70 #define LD_HTTP (1u<<7)
71 /** Application (socks) requests. */
72 #define LD_APP (1u<<8)
73 /** Communication via the controller protocol. */
74 #define LD_CONTROL (1u<<9)
75 /** Building, using, and managing circuits. */
76 #define LD_CIRC (1u<<10)
77 /** Hidden services. */
78 #define LD_REND (1u<<11)
79 /** Internal errors in this Tor process. */
80 #define LD_BUG (1u<<12)
81 /** Learning and using information about Tor servers. */
82 #define LD_DIR (1u<<13)
83 /** Learning and using information about Tor servers. */
84 #define LD_DIRSERV (1u<<14)
85 /** Onion routing protocol. */
86 #define LD_OR (1u<<15)
87 /** Generic edge-connection functionality. */
88 #define LD_EDGE (1u<<16)
89 #define LD_EXIT LD_EDGE
90 /** Bandwidth accounting. */
91 #define LD_ACCT (1u<<17)
92 /** Router history */
93 #define LD_HIST (1u<<18)
94 /** OR handshaking */
95 #define LD_HANDSHAKE (1u<<19)
96 /** Heartbeat messages */
97 #define LD_HEARTBEAT (1u<<20)
98 /** Abstract channel_t code */
99 #define LD_CHANNEL (1u<<21)
100 /** Number of logging domains in the code. */
101 #define N_LOGGING_DOMAINS 22
103 /** This log message is not safe to send to a callback-based logger
104 * immediately. Used as a flag, not a log domain. */
105 #define LD_NOCB (1u<<31)
106 /** This log message should not include a function name, even if it otherwise
107 * would. Used as a flag, not a log domain. */
108 #define LD_NOFUNCNAME (1u<<30)
110 /** Mask of zero or more log domains, OR'd together. */
111 typedef uint32_t log_domain_mask_t;
113 /** Configures which severities are logged for each logging domain for a given
114 * log target. */
115 typedef struct log_severity_list_t {
116 /** For each log severity, a bitmask of which domains a given logger is
117 * logging. */
118 log_domain_mask_t masks[LOG_DEBUG-LOG_ERR+1];
119 } log_severity_list_t;
121 /** Callback type used for add_callback_log. */
122 typedef void (*log_callback)(int severity, uint32_t domain, const char *msg);
124 void init_logging(void);
125 int parse_log_level(const char *level);
126 const char *log_level_to_string(int level);
127 int parse_log_severity_config(const char **cfg,
128 log_severity_list_t *severity_out);
129 void set_log_severity_config(int minSeverity, int maxSeverity,
130 log_severity_list_t *severity_out);
131 void add_stream_log(const log_severity_list_t *severity, const char *name,
132 int fd);
133 int add_file_log(const log_severity_list_t *severity, const char *filename);
134 #ifdef HAVE_SYSLOG_H
135 int add_syslog_log(const log_severity_list_t *severity);
136 #endif
137 int add_callback_log(const log_severity_list_t *severity, log_callback cb);
138 void logs_set_domain_logging(int enabled);
139 int get_min_log_level(void);
140 void switch_logs_debug(void);
141 void logs_free_all(void);
142 void add_temp_log(int min_severity);
143 void close_temp_logs(void);
144 void rollback_log_changes(void);
145 void mark_logs_temp(void);
146 void change_callback_log_severity(int loglevelMin, int loglevelMax,
147 log_callback cb);
148 void flush_pending_log_callbacks(void);
149 void log_set_application_name(const char *name);
150 void set_log_time_granularity(int granularity_msec);
152 void tor_log(int severity, log_domain_mask_t domain, const char *format, ...)
153 CHECK_PRINTF(3,4);
155 void tor_log_err_sigsafe(const char *m, ...);
156 int tor_log_get_sigsafe_err_fds(const int **out);
157 void tor_log_update_sigsafe_err_fds(void);
159 struct smartlist_t;
160 void tor_log_get_logfile_names(struct smartlist_t *out);
162 extern int log_global_min_severity_;
164 #if defined(__GNUC__) || defined(RUNNING_DOXYGEN)
165 void log_fn_(int severity, log_domain_mask_t domain,
166 const char *funcname, const char *format, ...)
167 CHECK_PRINTF(4,5);
168 struct ratelim_t;
169 void log_fn_ratelim_(struct ratelim_t *ratelim, int severity,
170 log_domain_mask_t domain, const char *funcname,
171 const char *format, ...)
172 CHECK_PRINTF(5,6);
173 /** Log a message at level <b>severity</b>, using a pretty-printed version
174 * of the current function name. */
175 #define log_fn(severity, domain, args...) \
176 log_fn_(severity, domain, __PRETTY_FUNCTION__, args)
177 /** As log_fn, but use <b>ratelim</b> (an instance of ratelim_t) to control
178 * the frequency at which messages can appear.
180 #define log_fn_ratelim(ratelim, severity, domain, args...) \
181 log_fn_ratelim_(ratelim, severity, domain, __PRETTY_FUNCTION__, args)
182 #define log_debug(domain, args...) \
183 STMT_BEGIN \
184 if (PREDICT_UNLIKELY(log_global_min_severity_ == LOG_DEBUG)) \
185 log_fn_(LOG_DEBUG, domain, __PRETTY_FUNCTION__, args); \
186 STMT_END
187 #define log_info(domain, args...) \
188 log_fn_(LOG_INFO, domain, __PRETTY_FUNCTION__, args)
189 #define log_notice(domain, args...) \
190 log_fn_(LOG_NOTICE, domain, __PRETTY_FUNCTION__, args)
191 #define log_warn(domain, args...) \
192 log_fn_(LOG_WARN, domain, __PRETTY_FUNCTION__, args)
193 #define log_err(domain, args...) \
194 log_fn_(LOG_ERR, domain, __PRETTY_FUNCTION__, args)
196 #else /* ! defined(__GNUC__) */
198 void log_fn_(int severity, log_domain_mask_t domain, const char *format, ...);
199 struct ratelim_t;
200 void log_fn_ratelim_(struct ratelim_t *ratelim, int severity,
201 log_domain_mask_t domain, const char *format, ...);
202 void log_debug_(log_domain_mask_t domain, const char *format, ...);
203 void log_info_(log_domain_mask_t domain, const char *format, ...);
204 void log_notice_(log_domain_mask_t domain, const char *format, ...);
205 void log_warn_(log_domain_mask_t domain, const char *format, ...);
206 void log_err_(log_domain_mask_t domain, const char *format, ...);
208 #if defined(_MSC_VER) && _MSC_VER < 1300
209 /* MSVC 6 and earlier don't have __func__, or even __LINE__. */
210 #define log_fn log_fn_
211 #define log_fn_ratelim log_fn_ratelim_
212 #define log_debug log_debug_
213 #define log_info log_info_
214 #define log_notice log_notice_
215 #define log_warn log_warn_
216 #define log_err log_err_
217 #else
218 /* We don't have GCC's varargs macros, so use a global variable to pass the
219 * function name to log_fn */
220 extern const char *log_fn_function_name_;
221 /* We abuse the comma operator here, since we can't use the standard
222 * do {...} while (0) trick to wrap this macro, since the macro can't take
223 * arguments. */
224 #define log_fn (log_fn_function_name_=__func__),log_fn_
225 #define log_fn_ratelim (log_fn_function_name_=__func__),log_fn_ratelim_
226 #define log_debug (log_fn_function_name_=__func__),log_debug_
227 #define log_info (log_fn_function_name_=__func__),log_info_
228 #define log_notice (log_fn_function_name_=__func__),log_notice_
229 #define log_warn (log_fn_function_name_=__func__),log_warn_
230 #define log_err (log_fn_function_name_=__func__),log_err_
231 #endif
233 #endif /* !GNUC */
235 #ifdef LOG_PRIVATE
236 MOCK_DECL(STATIC void, logv, (int severity, log_domain_mask_t domain,
237 const char *funcname, const char *suffix, const char *format,
238 va_list ap) CHECK_PRINTF(5,0));
239 #endif
241 # define TOR_TORLOG_H
242 #endif