tor  0.4.2.0-alpha-dev
log.h
Go to the documentation of this file.
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 */
6 
13 #ifndef TOR_TORLOG_H
14 #define TOR_TORLOG_H
15 
16 #include <stdarg.h>
17 #include "lib/cc/torint.h"
18 #include "lib/cc/compat_compiler.h"
19 #include "lib/defs/logging_types.h"
21 
22 #ifdef HAVE_SYSLOG_H
23 #include <syslog.h>
24 #define LOG_WARN LOG_WARNING
25 #if LOG_DEBUG < LOG_ERR
26 #error "Your syslog.h thinks high numbers are more important. " \
27  "We aren't prepared to deal with that."
28 #endif
29 #else /* !(defined(HAVE_SYSLOG_H)) */
30 /* Note: Syslog's logging code refers to priorities, with 0 being the most
31  * important. Thus, all our comparisons needed to be reversed when we added
32  * syslog support.
33  *
34  * The upshot of this is that comments about log levels may be messed up: for
35  * "maximum severity" read "most severe" and "numerically *lowest* severity".
36  */
37 
40 #define LOG_DEBUG 7
41 
43 #define LOG_INFO 6
44 
48 #define LOG_NOTICE 5
49 
51 #define LOG_WARN 4
52 
54 #define LOG_ERR 3
55 #endif /* defined(HAVE_SYSLOG_H) */
56 
57 /* Logging domains */
58 
60 #define LD_GENERAL (UINT64_C(1)<<0)
61 
62 #define LD_CRYPTO (UINT64_C(1)<<1)
63 
64 #define LD_NET (UINT64_C(1)<<2)
65 
66 #define LD_CONFIG (UINT64_C(1)<<3)
67 
68 #define LD_FS (UINT64_C(1)<<4)
69 
70 #define LD_PROTOCOL (UINT64_C(1)<<5)
71 
72 #define LD_MM (UINT64_C(1)<<6)
73 
74 #define LD_HTTP (UINT64_C(1)<<7)
75 
76 #define LD_APP (UINT64_C(1)<<8)
77 
78 #define LD_CONTROL (UINT64_C(1)<<9)
79 
80 #define LD_CIRC (UINT64_C(1)<<10)
81 
82 #define LD_REND (UINT64_C(1)<<11)
83 
84 #define LD_BUG (UINT64_C(1)<<12)
85 
86 #define LD_DIR (UINT64_C(1)<<13)
87 
88 #define LD_DIRSERV (UINT64_C(1)<<14)
89 
90 #define LD_OR (UINT64_C(1)<<15)
91 
92 #define LD_EDGE (UINT64_C(1)<<16)
93 #define LD_EXIT LD_EDGE
94 
95 #define LD_ACCT (UINT64_C(1)<<17)
96 
97 #define LD_HIST (UINT64_C(1)<<18)
98 
99 #define LD_HANDSHAKE (UINT64_C(1)<<19)
100 
101 #define LD_HEARTBEAT (UINT64_C(1)<<20)
102 
103 #define LD_CHANNEL (UINT64_C(1)<<21)
104 
105 #define LD_SCHED (UINT64_C(1)<<22)
106 
107 #define LD_GUARD (UINT64_C(1)<<23)
108 
109 #define LD_CONSDIFF (UINT64_C(1)<<24)
110 
111 #define LD_DOS (UINT64_C(1)<<25)
112 
113 #define LD_PROCESS (UINT64_C(1)<<26)
114 
115 #define LD_PT (UINT64_C(1)<<27)
116 
117 #define LD_BTRACK (UINT64_C(1)<<28)
118 
119 #define LD_MESG (UINT64_C(1)<<29)
120 #define N_LOGGING_DOMAINS 30
121 
123 #define LOWEST_RESERVED_LD_FLAG_ (UINT64_C(1)<<61)
124 #ifdef TOR_UNIT_TESTS
125 
126 #define LD_NO_MOCK (UINT64_C(1)<<61)
127 #endif
128 
131 #define LD_NOCB (UINT64_C(1)<<62)
132 
134 #define LD_NOFUNCNAME (UINT64_C(1)<<63)
135 
138 typedef struct log_severity_list_t {
143 
145 typedef void (*log_callback)(int severity, log_domain_mask_t domain,
146  const char *msg);
147 
148 void init_logging(int disable_startup_queue);
149 int parse_log_level(const char *level);
150 const char *log_level_to_string(int level);
151 int parse_log_severity_config(const char **cfg,
152  log_severity_list_t *severity_out);
153 void set_log_severity_config(int minSeverity, int maxSeverity,
154  log_severity_list_t *severity_out);
155 void add_stream_log(const log_severity_list_t *severity, const char *name,
156  int fd);
157 int add_file_log(const log_severity_list_t *severity,
158  const char *filename,
159  int fd);
160 
161 #ifdef HAVE_SYSLOG_H
162 int add_syslog_log(const log_severity_list_t *severity,
163  const char* syslog_identity_tag);
164 #endif // HAVE_SYSLOG_H.
165 #ifdef HAVE_ANDROID_LOG_H
166 int add_android_log(const log_severity_list_t *severity,
167  const char *android_identity_tag);
168 #endif // HAVE_ANDROID_LOG_H.
169 int add_callback_log(const log_severity_list_t *severity, log_callback cb);
170 typedef void (*pending_callback_callback)(void);
171 void logs_set_pending_callback_callback(pending_callback_callback cb);
172 void logs_set_domain_logging(int enabled);
173 int get_min_log_level(void);
174 void switch_logs_debug(void);
175 void logs_free_all(void);
176 void logs_close_sigsafe(void);
177 void add_temp_log(int min_severity);
178 void close_temp_logs(void);
179 void rollback_log_changes(void);
180 void mark_logs_temp(void);
181 void change_callback_log_severity(int loglevelMin, int loglevelMax,
182  log_callback cb);
183 void flush_pending_log_callbacks(void);
185 void log_set_application_name(const char *name);
186 void set_log_time_granularity(int granularity_msec);
187 void truncate_logs(void);
188 
189 void tor_log(int severity, log_domain_mask_t domain, const char *format, ...)
190  CHECK_PRINTF(3,4);
191 
193 
194 struct smartlist_t;
195 void tor_log_get_logfile_names(struct smartlist_t *out);
196 
197 extern int log_global_min_severity_;
198 
199 #ifdef TOR_COVERAGE
200 /* For coverage builds, we try to avoid our log_debug optimization, since it
201  * can have weird effects on internal macro coverage. */
202 #define debug_logging_enabled() (1)
203 #else
204 static inline bool debug_logging_enabled(void);
208 static inline bool debug_logging_enabled(void)
209 {
210  return PREDICT_UNLIKELY(log_global_min_severity_ == LOG_DEBUG);
211 }
212 #endif /* defined(TOR_COVERAGE) */
213 
214 void log_fn_(int severity, log_domain_mask_t domain,
215  const char *funcname, const char *format, ...)
216  CHECK_PRINTF(4,5);
217 struct ratelim_t;
218 void log_fn_ratelim_(struct ratelim_t *ratelim, int severity,
219  log_domain_mask_t domain, const char *funcname,
220  const char *format, ...)
221  CHECK_PRINTF(5,6);
222 
223 int log_message_is_interesting(int severity, log_domain_mask_t domain);
224 void tor_log_string(int severity, log_domain_mask_t domain,
225  const char *function, const char *string);
226 
227 #if defined(__GNUC__) && __GNUC__ <= 3
228 
229 /* These are the GCC varidaic macros, so that older versions of GCC don't
230  * break. */
231 
234 #define log_fn(severity, domain, args...) \
235  log_fn_(severity, domain, __FUNCTION__, args)
236 
239 #define log_fn_ratelim(ratelim, severity, domain, args...) \
240  log_fn_ratelim_(ratelim, severity, domain, __FUNCTION__, args)
241 #define log_debug(domain, args...) \
242  STMT_BEGIN \
243  if (debug_logging_enabled()) \
244  log_fn_(LOG_DEBUG, domain, __FUNCTION__, args); \
245  STMT_END
246 #define log_info(domain, args...) \
247  log_fn_(LOG_INFO, domain, __FUNCTION__, args)
248 #define log_notice(domain, args...) \
249  log_fn_(LOG_NOTICE, domain, __FUNCTION__, args)
250 #define log_warn(domain, args...) \
251  log_fn_(LOG_WARN, domain, __FUNCTION__, args)
252 #define log_err(domain, args...) \
253  log_fn_(LOG_ERR, domain, __FUNCTION__, args)
254 
255 #else /* !(defined(__GNUC__) && __GNUC__ <= 3) */
256 
257 /* Here are the c99 variadic macros, to work with non-GCC compilers */
258 
259 #define log_debug(domain, args, ...) \
260  STMT_BEGIN \
261  if (debug_logging_enabled()) \
262  log_fn_(LOG_DEBUG, domain, __FUNCTION__, args, ##__VA_ARGS__); \
263  STMT_END
264 #define log_info(domain, args,...) \
265  log_fn_(LOG_INFO, domain, __FUNCTION__, args, ##__VA_ARGS__)
266 #define log_notice(domain, args,...) \
267  log_fn_(LOG_NOTICE, domain, __FUNCTION__, args, ##__VA_ARGS__)
268 #define log_warn(domain, args,...) \
269  log_fn_(LOG_WARN, domain, __FUNCTION__, args, ##__VA_ARGS__)
270 #define log_err(domain, args,...) \
271  log_fn_(LOG_ERR, domain, __FUNCTION__, args, ##__VA_ARGS__)
272 
274 #define log_fn(severity, domain, args,...) \
275  log_fn_(severity, domain, __FUNCTION__, args, ##__VA_ARGS__)
276 
279 #define log_fn_ratelim(ratelim, severity, domain, args,...) \
280  log_fn_ratelim_(ratelim, severity, domain, __FUNCTION__, \
281  args, ##__VA_ARGS__)
282 #endif /* defined(__GNUC__) && __GNUC__ <= 3 */
283 
289 extern const int LOG_WARN_;
290 extern const int LOG_NOTICE_;
291 extern const log_domain_mask_t LD_NET_;
292 extern const log_domain_mask_t LD_GENERAL_;
293 
294 #ifdef LOG_PRIVATE
295 MOCK_DECL(STATIC void, logv, (int severity, log_domain_mask_t domain,
296  const char *funcname, const char *suffix, const char *format,
297  va_list ap) CHECK_PRINTF(5,0));
298 #endif
299 
300 #endif /* !defined(TOR_TORLOG_H) */
void init_logging(int disable_startup_queue)
Definition: log.c:965
void close_temp_logs(void)
Definition: log.c:1152
log_domain_mask_t masks[LOG_DEBUG-LOG_ERR+1]
Definition: log.h:141
void logs_set_domain_logging(int enabled)
Definition: log.c:988
void log_set_application_name(const char *name)
Definition: log.c:248
void flush_log_messages_from_startup(void)
Definition: log.c:1114
void tor_log_get_logfile_names(struct smartlist_t *out)
Definition: log.c:738
int log_global_min_severity_
Definition: log.c:224
void tor_log(int severity, log_domain_mask_t domain, const char *format,...)
Definition: log.c:633
void set_log_severity_config(int minSeverity, int maxSeverity, log_severity_list_t *severity_out)
Definition: log.c:922
void change_callback_log_severity(int loglevelMin, int loglevelMax, log_callback cb)
Definition: log.c:1053
void logs_close_sigsafe(void)
Definition: log.c:846
Integer definitions used throughout Tor.
void(* log_callback)(int severity, log_domain_mask_t domain, const char *msg)
Definition: log.h:145
void add_temp_log(int min_severity)
Definition: log.c:999
void logs_free_all(void)
Definition: log.c:801
void truncate_logs(void)
Definition: log.c:1545
const int LOG_WARN_
Definition: log.c:71
int add_callback_log(const log_severity_list_t *severity, log_callback cb)
Definition: log.c:1033
Utility macros to handle different features and behavior in different compilers.
Global definition for types used by logging systems.
void log_fn_(int severity, log_domain_mask_t domain, const char *fn, const char *format,...)
Definition: log.c:760
struct log_severity_list_t log_severity_list_t
void int log_message_is_interesting(int severity, log_domain_mask_t domain)
Definition: log.c:259
#define LOG_ERR
Definition: log.h:54
int parse_log_severity_config(const char **cfg, log_severity_list_t *severity_out)
Definition: log.c:1410
void logs_set_pending_callback_callback(pending_callback_callback cb)
Definition: log.c:1023
void set_log_time_granularity(int granularity_msec)
Definition: log.c:284
uint64_t log_domain_mask_t
Definition: logging_types.h:21
void tor_log_string(int severity, log_domain_mask_t domain, const char *function, const char *string)
Definition: log.c:272
int add_file_log(const log_severity_list_t *severity, const char *filename, int fd)
Definition: log.c:1203
void void tor_log_update_sigsafe_err_fds(void)
Definition: log.c:664
Macros to implement mocking and selective exposure for the test code.
const char * log_level_to_string(int level)
Definition: log.c:1321
#define MOCK_DECL(rv, funcname, arglist)
Definition: testsupport.h:94
#define LOG_DEBUG
Definition: log.h:40
void add_stream_log(const log_severity_list_t *severity, const char *name, int fd)
Definition: log.c:956
static bool debug_logging_enabled(void)
Definition: log.h:208
void mark_logs_temp(void)
Definition: log.c:1188
void rollback_log_changes(void)
Definition: log.c:1176
void switch_logs_debug(void)
Definition: log.c:1530
int parse_log_level(const char *level)
Definition: log.c:1304
void flush_pending_log_callbacks(void)
Definition: log.c:1072
int get_min_log_level(void)
Definition: log.c:1515