tor  0.4.1.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 
15 #include <stdarg.h>
16 #include "lib/cc/torint.h"
17 #include "lib/cc/compat_compiler.h"
19 
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.
31  *
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".
34  */
35 
38 #define LOG_DEBUG 7
39 
41 #define LOG_INFO 6
42 
46 #define LOG_NOTICE 5
47 
49 #define LOG_WARN 4
50 
52 #define LOG_ERR 3
53 #endif /* defined(HAVE_SYSLOG_H) */
54 
55 /* Logging domains */
56 
58 #define LD_GENERAL (1u<<0)
59 
60 #define LD_CRYPTO (1u<<1)
61 
62 #define LD_NET (1u<<2)
63 
64 #define LD_CONFIG (1u<<3)
65 
66 #define LD_FS (1u<<4)
67 
68 #define LD_PROTOCOL (1u<<5)
69 
70 #define LD_MM (1u<<6)
71 
72 #define LD_HTTP (1u<<7)
73 
74 #define LD_APP (1u<<8)
75 
76 #define LD_CONTROL (1u<<9)
77 
78 #define LD_CIRC (1u<<10)
79 
80 #define LD_REND (1u<<11)
81 
82 #define LD_BUG (1u<<12)
83 
84 #define LD_DIR (1u<<13)
85 
86 #define LD_DIRSERV (1u<<14)
87 
88 #define LD_OR (1u<<15)
89 
90 #define LD_EDGE (1u<<16)
91 #define LD_EXIT LD_EDGE
92 
93 #define LD_ACCT (1u<<17)
94 
95 #define LD_HIST (1u<<18)
96 
97 #define LD_HANDSHAKE (1u<<19)
98 
99 #define LD_HEARTBEAT (1u<<20)
100 
101 #define LD_CHANNEL (1u<<21)
102 
103 #define LD_SCHED (1u<<22)
104 
105 #define LD_GUARD (1u<<23)
106 
107 #define LD_CONSDIFF (1u<<24)
108 
109 #define LD_DOS (1u<<25)
110 
111 #define LD_PROCESS (1u<<26)
112 
113 #define LD_PT (1u<<27)
114 
115 #define LD_BTRACK (1u<<28)
116 
117 #define N_LOGGING_DOMAINS 29
118 
121 #define LD_NOCB (1u<<31)
122 
124 #define LD_NOFUNCNAME (1u<<30)
125 
126 #ifdef TOR_UNIT_TESTS
127 
128 #define LD_NO_MOCK (1u<<29)
129 #endif
130 
132 typedef uint32_t log_domain_mask_t;
133 
136 typedef struct log_severity_list_t {
141 
143 typedef void (*log_callback)(int severity, uint32_t domain, const char *msg);
144 
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);
157 
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);
181 void log_set_application_name(const char *name);
182 void set_log_time_granularity(int granularity_msec);
183 void truncate_logs(void);
184 
185 void tor_log(int severity, log_domain_mask_t domain, const char *format, ...)
186  CHECK_PRINTF(3,4);
187 
189 
190 struct smartlist_t;
191 void tor_log_get_logfile_names(struct smartlist_t *out);
192 
193 extern int log_global_min_severity_;
194 
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);
203 
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);
207 
208 #if defined(__GNUC__) && __GNUC__ <= 3
209 
210 /* These are the GCC varidaic macros, so that older versions of GCC don't
211  * break. */
212 
215 #define log_fn(severity, domain, args...) \
216  log_fn_(severity, domain, __FUNCTION__, args)
217 
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)
235 
236 #else /* !(defined(__GNUC__) && __GNUC__ <= 3) */
237 
238 /* Here are the c99 variadic macros, to work with non-GCC compilers */
239 
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 
255 #define log_fn(severity, domain, args,...) \
256  log_fn_(severity, domain, __FUNCTION__, args, ##__VA_ARGS__)
257 
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 */
264 
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_;
274 
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
280 
281 # define TOR_TORLOG_H
282 #endif /* !defined(TOR_TORLOG_H) */
void init_logging(int disable_startup_queue)
Definition: log.c:901
void close_temp_logs(void)
Definition: log.c:1088
log_domain_mask_t masks[LOG_DEBUG-LOG_ERR+1]
Definition: log.h:139
void logs_set_domain_logging(int enabled)
Definition: log.c:924
void log_set_application_name(const char *name)
Definition: log.c:246
void flush_log_messages_from_startup(void)
Definition: log.c:1050
void tor_log_get_logfile_names(struct smartlist_t *out)
Definition: log.c:711
uint32_t log_domain_mask_t
Definition: log.h:132
void(* log_callback)(int severity, uint32_t domain, const char *msg)
Definition: log.h:143
int log_global_min_severity_
Definition: log.c:223
void tor_log(int severity, log_domain_mask_t domain, const char *format,...)
Definition: log.c:631
void set_log_severity_config(int minSeverity, int maxSeverity, log_severity_list_t *severity_out)
Definition: log.c:858
void change_callback_log_severity(int loglevelMin, int loglevelMax, log_callback cb)
Definition: log.c:989
Integer definitions used throughout Tor.
void add_temp_log(int min_severity)
Definition: log.c:935
void logs_free_all(void)
Definition: log.c:774
void truncate_logs(void)
Definition: log.c:1476
const int LOG_WARN_
Definition: log.c:70
int add_callback_log(const log_severity_list_t *severity, log_callback cb)
Definition: log.c:969
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:733
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:257
#define LOG_ERR
Definition: log.h:52
int parse_log_severity_config(const char **cfg, log_severity_list_t *severity_out)
Definition: log.c:1341
void logs_set_pending_callback_callback(pending_callback_callback cb)
Definition: log.c:959
void set_log_time_granularity(int granularity_msec)
Definition: log.c:282
void tor_log_string(int severity, log_domain_mask_t domain, const char *function, const char *string)
Definition: log.c:270
int add_file_log(const log_severity_list_t *severity, const char *filename, int fd)
Definition: log.c:1139
void void tor_log_update_sigsafe_err_fds(void)
Definition: log.c:662
Macros to implement mocking and selective exposure for the test code.
const char * log_level_to_string(int level)
Definition: log.c:1257
#define MOCK_DECL(rv, funcname, arglist)
Definition: testsupport.h:94
#define LOG_DEBUG
Definition: log.h:38
void add_stream_log(const log_severity_list_t *severity, const char *name, int fd)
Definition: log.c:892
void mark_logs_temp(void)
Definition: log.c:1124
void rollback_log_changes(void)
Definition: log.c:1112
void switch_logs_debug(void)
Definition: log.c:1461
int parse_log_level(const char *level)
Definition: log.c:1240
void flush_pending_log_callbacks(void)
Definition: log.c:1008
int get_min_log_level(void)
Definition: log.c:1446