Bump copyright date to 2019
[tor.git] / src / lib / log / log.h
blobdbc1c47021f39e6be8b2039715ba0e6d6f509114
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-2019, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
7 /**
8 * \file log.h
10 * \brief Headers for log.c
11 **/
13 #ifndef TOR_TORLOG_H
15 #include <stdarg.h>
16 #include "lib/cc/torint.h"
17 #include "lib/cc/compat_compiler.h"
18 #include "lib/testsupport/testsupport.h"
20 #ifdef HAVE_SYSLOG_H
21 #include <syslog.h>
22 #define LOG_WARN LOG_WARNING
23 #if LOG_DEBUG < LOG_ERR
24 #error "Your syslog.h thinks high numbers are more important. " \
25 "We aren't prepared to deal with that."
26 #endif
27 #else /* !(defined(HAVE_SYSLOG_H)) */
28 /* Note: Syslog's logging code refers to priorities, with 0 being the most
29 * important. Thus, all our comparisons needed to be reversed when we added
30 * syslog support.
32 * The upshot of this is that comments about log levels may be messed up: for
33 * "maximum severity" read "most severe" and "numerically *lowest* severity".
36 /** Debug-level severity: for hyper-verbose messages of no interest to
37 * anybody but developers. */
38 #define LOG_DEBUG 7
39 /** Info-level severity: for messages that appear frequently during normal
40 * operation. */
41 #define LOG_INFO 6
42 /** Notice-level severity: for messages that appear infrequently
43 * during normal operation; that the user will probably care about;
44 * and that are not errors.
46 #define LOG_NOTICE 5
47 /** Warn-level severity: for messages that only appear when something has gone
48 * wrong. */
49 #define LOG_WARN 4
50 /** Error-level severity: for messages that only appear when something has gone
51 * very wrong, and the Tor process can no longer proceed. */
52 #define LOG_ERR 3
53 #endif /* defined(HAVE_SYSLOG_H) */
55 /* Logging domains */
57 /** Catch-all for miscellaneous events and fatal errors. */
58 #define LD_GENERAL (1u<<0)
59 /** The cryptography subsystem. */
60 #define LD_CRYPTO (1u<<1)
61 /** Networking. */
62 #define LD_NET (1u<<2)
63 /** Parsing and acting on our configuration. */
64 #define LD_CONFIG (1u<<3)
65 /** Reading and writing from the filesystem. */
66 #define LD_FS (1u<<4)
67 /** Other servers' (non)compliance with the Tor protocol. */
68 #define LD_PROTOCOL (1u<<5)
69 /** Memory management. */
70 #define LD_MM (1u<<6)
71 /** HTTP implementation. */
72 #define LD_HTTP (1u<<7)
73 /** Application (socks) requests. */
74 #define LD_APP (1u<<8)
75 /** Communication via the controller protocol. */
76 #define LD_CONTROL (1u<<9)
77 /** Building, using, and managing circuits. */
78 #define LD_CIRC (1u<<10)
79 /** Hidden services. */
80 #define LD_REND (1u<<11)
81 /** Internal errors in this Tor process. */
82 #define LD_BUG (1u<<12)
83 /** Learning and using information about Tor servers. */
84 #define LD_DIR (1u<<13)
85 /** Learning and using information about Tor servers. */
86 #define LD_DIRSERV (1u<<14)
87 /** Onion routing protocol. */
88 #define LD_OR (1u<<15)
89 /** Generic edge-connection functionality. */
90 #define LD_EDGE (1u<<16)
91 #define LD_EXIT LD_EDGE
92 /** Bandwidth accounting. */
93 #define LD_ACCT (1u<<17)
94 /** Router history */
95 #define LD_HIST (1u<<18)
96 /** OR handshaking */
97 #define LD_HANDSHAKE (1u<<19)
98 /** Heartbeat messages */
99 #define LD_HEARTBEAT (1u<<20)
100 /** Abstract channel_t code */
101 #define LD_CHANNEL (1u<<21)
102 /** Scheduler */
103 #define LD_SCHED (1u<<22)
104 /** Guard nodes */
105 #define LD_GUARD (1u<<23)
106 /** Generation and application of consensus diffs. */
107 #define LD_CONSDIFF (1u<<24)
108 /** Denial of Service mitigation. */
109 #define LD_DOS (1u<<25)
110 /** Processes */
111 #define LD_PROCESS (1u<<26)
112 /** Pluggable Transports. */
113 #define LD_PT (1u<<27)
114 /** Bootstrap tracker. */
115 #define LD_BTRACK (1u<<28)
116 /** Number of logging domains in the code. */
117 #define N_LOGGING_DOMAINS 29
119 /** This log message is not safe to send to a callback-based logger
120 * immediately. Used as a flag, not a log domain. */
121 #define LD_NOCB (1u<<31)
122 /** This log message should not include a function name, even if it otherwise
123 * would. Used as a flag, not a log domain. */
124 #define LD_NOFUNCNAME (1u<<30)
126 #ifdef TOR_UNIT_TESTS
127 /** This log message should not be intercepted by mock_saving_logv */
128 #define LD_NO_MOCK (1u<<29)
129 #endif
131 /** Mask of zero or more log domains, OR'd together. */
132 typedef uint32_t log_domain_mask_t;
134 /** Configures which severities are logged for each logging domain for a given
135 * log target. */
136 typedef struct log_severity_list_t {
137 /** For each log severity, a bitmask of which domains a given logger is
138 * logging. */
139 log_domain_mask_t masks[LOG_DEBUG-LOG_ERR+1];
140 } log_severity_list_t;
142 /** Callback type used for add_callback_log. */
143 typedef void (*log_callback)(int severity, uint32_t domain, const char *msg);
145 void init_logging(int disable_startup_queue);
146 int parse_log_level(const char *level);
147 const char *log_level_to_string(int level);
148 int parse_log_severity_config(const char **cfg,
149 log_severity_list_t *severity_out);
150 void set_log_severity_config(int minSeverity, int maxSeverity,
151 log_severity_list_t *severity_out);
152 void add_stream_log(const log_severity_list_t *severity, const char *name,
153 int fd);
154 int add_file_log(const log_severity_list_t *severity,
155 const char *filename,
156 int fd);
158 #ifdef HAVE_SYSLOG_H
159 int add_syslog_log(const log_severity_list_t *severity,
160 const char* syslog_identity_tag);
161 #endif // HAVE_SYSLOG_H.
162 #ifdef HAVE_ANDROID_LOG_H
163 int add_android_log(const log_severity_list_t *severity,
164 const char *android_identity_tag);
165 #endif // HAVE_ANDROID_LOG_H.
166 int add_callback_log(const log_severity_list_t *severity, log_callback cb);
167 typedef void (*pending_callback_callback)(void);
168 void logs_set_pending_callback_callback(pending_callback_callback cb);
169 void logs_set_domain_logging(int enabled);
170 int get_min_log_level(void);
171 void switch_logs_debug(void);
172 void logs_free_all(void);
173 void add_temp_log(int min_severity);
174 void close_temp_logs(void);
175 void rollback_log_changes(void);
176 void mark_logs_temp(void);
177 void change_callback_log_severity(int loglevelMin, int loglevelMax,
178 log_callback cb);
179 void flush_pending_log_callbacks(void);
180 void flush_log_messages_from_startup(void);
181 void log_set_application_name(const char *name);
182 void set_log_time_granularity(int granularity_msec);
183 void truncate_logs(void);
185 void tor_log(int severity, log_domain_mask_t domain, const char *format, ...)
186 CHECK_PRINTF(3,4);
188 void tor_log_update_sigsafe_err_fds(void);
190 struct smartlist_t;
191 void tor_log_get_logfile_names(struct smartlist_t *out);
193 extern int log_global_min_severity_;
195 void log_fn_(int severity, log_domain_mask_t domain,
196 const char *funcname, const char *format, ...)
197 CHECK_PRINTF(4,5);
198 struct ratelim_t;
199 void log_fn_ratelim_(struct ratelim_t *ratelim, int severity,
200 log_domain_mask_t domain, const char *funcname,
201 const char *format, ...)
202 CHECK_PRINTF(5,6);
204 int log_message_is_interesting(int severity, log_domain_mask_t domain);
205 void tor_log_string(int severity, log_domain_mask_t domain,
206 const char *function, const char *string);
208 #if defined(__GNUC__) && __GNUC__ <= 3
210 /* These are the GCC varidaic macros, so that older versions of GCC don't
211 * break. */
213 /** Log a message at level <b>severity</b>, using a pretty-printed version
214 * of the current function name. */
215 #define log_fn(severity, domain, args...) \
216 log_fn_(severity, domain, __FUNCTION__, args)
217 /** As log_fn, but use <b>ratelim</b> (an instance of ratelim_t) to control
218 * the frequency at which messages can appear.
220 #define log_fn_ratelim(ratelim, severity, domain, args...) \
221 log_fn_ratelim_(ratelim, severity, domain, __FUNCTION__, args)
222 #define log_debug(domain, args...) \
223 STMT_BEGIN \
224 if (PREDICT_UNLIKELY(log_global_min_severity_ == LOG_DEBUG)) \
225 log_fn_(LOG_DEBUG, domain, __FUNCTION__, args); \
226 STMT_END
227 #define log_info(domain, args...) \
228 log_fn_(LOG_INFO, domain, __FUNCTION__, args)
229 #define log_notice(domain, args...) \
230 log_fn_(LOG_NOTICE, domain, __FUNCTION__, args)
231 #define log_warn(domain, args...) \
232 log_fn_(LOG_WARN, domain, __FUNCTION__, args)
233 #define log_err(domain, args...) \
234 log_fn_(LOG_ERR, domain, __FUNCTION__, args)
236 #else /* !(defined(__GNUC__) && __GNUC__ <= 3) */
238 /* Here are the c99 variadic macros, to work with non-GCC compilers */
240 #define log_debug(domain, args, ...) \
241 STMT_BEGIN \
242 if (PREDICT_UNLIKELY(log_global_min_severity_ == LOG_DEBUG)) \
243 log_fn_(LOG_DEBUG, domain, __FUNCTION__, args, ##__VA_ARGS__); \
244 STMT_END
245 #define log_info(domain, args,...) \
246 log_fn_(LOG_INFO, domain, __FUNCTION__, args, ##__VA_ARGS__)
247 #define log_notice(domain, args,...) \
248 log_fn_(LOG_NOTICE, domain, __FUNCTION__, args, ##__VA_ARGS__)
249 #define log_warn(domain, args,...) \
250 log_fn_(LOG_WARN, domain, __FUNCTION__, args, ##__VA_ARGS__)
251 #define log_err(domain, args,...) \
252 log_fn_(LOG_ERR, domain, __FUNCTION__, args, ##__VA_ARGS__)
253 /** Log a message at level <b>severity</b>, using a pretty-printed version
254 * of the current function name. */
255 #define log_fn(severity, domain, args,...) \
256 log_fn_(severity, domain, __FUNCTION__, args, ##__VA_ARGS__)
257 /** As log_fn, but use <b>ratelim</b> (an instance of ratelim_t) to control
258 * the frequency at which messages can appear.
260 #define log_fn_ratelim(ratelim, severity, domain, args,...) \
261 log_fn_ratelim_(ratelim, severity, domain, __FUNCTION__, \
262 args, ##__VA_ARGS__)
263 #endif /* defined(__GNUC__) && __GNUC__ <= 3 */
265 /** This defines log levels that are linked in the Rust log module, rather
266 * than re-defining these in both Rust and C.
268 * C_RUST_COUPLED src/rust/tor_log LogSeverity, LogDomain
270 extern const int LOG_WARN_;
271 extern const int LOG_NOTICE_;
272 extern const log_domain_mask_t LD_NET_;
273 extern const log_domain_mask_t LD_GENERAL_;
275 #ifdef LOG_PRIVATE
276 MOCK_DECL(STATIC void, logv, (int severity, log_domain_mask_t domain,
277 const char *funcname, const char *suffix, const char *format,
278 va_list ap) CHECK_PRINTF(5,0));
279 #endif
281 # define TOR_TORLOG_H
282 #endif /* !defined(TOR_TORLOG_H) */