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