torlog.h 9.27 KB
Newer Older
1
/* Copyright (c) 2001, Matej Pfajfar.
Roger Dingledine's avatar
Roger Dingledine committed
2
 * Copyright (c) 2001-2004, Roger Dingledine.
3
 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
Nick Mathewson's avatar
Nick Mathewson committed
4
 * Copyright (c) 2007-2016, The Tor Project, Inc. */
Roger Dingledine's avatar
Roger Dingledine committed
5
/* See LICENSE for licensing information */
Roger Dingledine's avatar
Roger Dingledine committed
6

Nick Mathewson's avatar
Nick Mathewson committed
7
/**
8
 * \file torlog.h
Nick Mathewson's avatar
Nick Mathewson committed
9
 *
10
 * \brief Headers for log.c
Nick Mathewson's avatar
Nick Mathewson committed
11
12
 **/

13
#ifndef TOR_TORLOG_H
14

15
#include "compat.h"
16
#include "testsupport.h"
17

18
#ifdef HAVE_SYSLOG_H
Roger Dingledine's avatar
Roger Dingledine committed
19
#include <syslog.h>
Roger Dingledine's avatar
Roger Dingledine committed
20
#define LOG_WARN LOG_WARNING
21
#if LOG_DEBUG < LOG_ERR
22
23
#error "Your syslog.h thinks high numbers are more important.  " \
       "We aren't prepared to deal with that."
24
#endif
25
#else
26
27
28
/* Note: Syslog's logging code refers to priorities, with 0 being the most
 * important.  Thus, all our comparisons needed to be reversed when we added
 * syslog support.
29
 *
30
31
 * The upshot of this is that comments about log levels may be messed up: for
 * "maximum severity" read "most severe" and "numerically *lowest* severity".
32
33
 */

Nick Mathewson's avatar
Nick Mathewson committed
34
35
/** Debug-level severity: for hyper-verbose messages of no interest to
 * anybody but developers. */
36
#define LOG_DEBUG   7
Nick Mathewson's avatar
Nick Mathewson committed
37
38
/** Info-level severity: for messages that appear frequently during normal
 * operation. */
39
#define LOG_INFO    6
Nick Mathewson's avatar
Nick Mathewson committed
40
41
42
43
/** Notice-level severity: for messages that appear infrequently
 * during normal operation; that the user will probably care about;
 * and that are not errors.
 */
44
#define LOG_NOTICE  5
Nick Mathewson's avatar
Nick Mathewson committed
45
46
/** Warn-level severity: for messages that only appear when something has gone
 * wrong. */
47
#define LOG_WARN    4
Roger Dingledine's avatar
Roger Dingledine committed
48
/** Error-level severity: for messages that only appear when something has gone
Nick Mathewson's avatar
Nick Mathewson committed
49
 * very wrong, and the Tor process can no longer proceed. */
50
#define LOG_ERR     3
51
#endif
Roger Dingledine's avatar
Roger Dingledine committed
52

53
/* Logging domains */
54

55
/** Catch-all for miscellaneous events and fatal errors. */
56
#define LD_GENERAL  (1u<<0)
Nick Mathewson's avatar
Nick Mathewson committed
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)
Nick Mathewson's avatar
Nick Mathewson committed
83
/** Learning and using information about Tor servers. */
84
#define LD_DIRSERV  (1u<<14)
Nick Mathewson's avatar
Nick Mathewson committed
85
/** Onion routing protocol. */
86
#define LD_OR       (1u<<15)
87
88
89
/** Generic edge-connection functionality. */
#define LD_EDGE     (1u<<16)
#define LD_EXIT     LD_EDGE
90
91
/** Bandwidth accounting. */
#define LD_ACCT     (1u<<17)
92
93
/** Router history */
#define LD_HIST     (1u<<18)
94
95
/** OR handshaking */
#define LD_HANDSHAKE (1u<<19)
96
97
/** Heartbeat messages */
#define LD_HEARTBEAT (1u<<20)
98
99
/** Abstract channel_t code */
#define LD_CHANNEL   (1u<<21)
100
101
/** Scheduler */
#define LD_SCHED     (1u<<22)
102
103
/** Denial of Service mitigation. */
#define LD_DOS       (1u<<23)
104
/** Number of logging domains in the code. */
105
#define N_LOGGING_DOMAINS 24
106

107
108
/** This log message is not safe to send to a callback-based logger
 * immediately.  Used as a flag, not a log domain. */
109
#define LD_NOCB (1u<<31)
110
111
112
/** This log message should not include a function name, even if it otherwise
 * would. Used as a flag, not a log domain. */
#define LD_NOFUNCNAME (1u<<30)
113

114
115
116
117
118
#ifdef TOR_UNIT_TESTS
/** This log message should not be intercepted by mock_saving_logv */
#define LD_NO_MOCK (1u<<29)
#endif

119
/** Mask of zero or more log domains, OR'd together. */
120
121
122
123
124
125
126
127
128
129
typedef uint32_t log_domain_mask_t;

/** Configures which severities are logged for each logging domain for a given
 * log target. */
typedef struct log_severity_list_t {
  /** For each log severity, a bitmask of which domains a given logger is
   * logging. */
  log_domain_mask_t masks[LOG_DEBUG-LOG_ERR+1];
} log_severity_list_t;

130
/** Callback type used for add_callback_log. */
131
typedef void (*log_callback)(int severity, uint32_t domain, const char *msg);
132

133
void init_logging(int disable_startup_queue);
134
int parse_log_level(const char *level);
135
const char *log_level_to_string(int level);
136
137
int parse_log_severity_config(const char **cfg,
                              log_severity_list_t *severity_out);
138
139
void set_log_severity_config(int minSeverity, int maxSeverity,
                             log_severity_list_t *severity_out);
140
void add_stream_log(const log_severity_list_t *severity, const char *name,
141
                    int fd);
Arlo Breault's avatar
Arlo Breault committed
142
143
int add_file_log(const log_severity_list_t *severity, const char *filename,
                 const int truncate);
144
#ifdef HAVE_SYSLOG_H
Nick Mathewson's avatar
Nick Mathewson committed
145
146
int add_syslog_log(const log_severity_list_t *severity,
                   const char* syslog_identity_tag);
147
#endif
148
int add_callback_log(const log_severity_list_t *severity, log_callback cb);
149
void logs_set_domain_logging(int enabled);
150
int get_min_log_level(void);
151
void switch_logs_debug(void);
152
void logs_free_all(void);
Peter Palfrader's avatar
Peter Palfrader committed
153
void add_temp_log(int min_severity);
154
void close_temp_logs(void);
155
void rollback_log_changes(void);
156
void mark_logs_temp(void);
157
158
void change_callback_log_severity(int loglevelMin, int loglevelMax,
                                  log_callback cb);
159
void flush_pending_log_callbacks(void);
160
void flush_log_messages_from_startup(void);
161
void log_set_application_name(const char *name);
162
void set_log_time_granularity(int granularity_msec);
Arlo Breault's avatar
Arlo Breault committed
163
void truncate_logs(void);
164

165
void tor_log(int severity, log_domain_mask_t domain, const char *format, ...)
166
  CHECK_PRINTF(3,4);
Roger Dingledine's avatar
Roger Dingledine committed
167

168
169
170
171
void tor_log_err_sigsafe(const char *m, ...);
int tor_log_get_sigsafe_err_fds(const int **out);
void tor_log_update_sigsafe_err_fds(void);

172
173
174
struct smartlist_t;
void tor_log_get_logfile_names(struct smartlist_t *out);

175
extern int log_global_min_severity_;
176

177
void log_fn_(int severity, log_domain_mask_t domain,
178
179
             const char *funcname, const char *format, ...)
  CHECK_PRINTF(4,5);
180
181
182
183
184
struct ratelim_t;
void log_fn_ratelim_(struct ratelim_t *ratelim, int severity,
                     log_domain_mask_t domain, const char *funcname,
                     const char *format, ...)
  CHECK_PRINTF(5,6);
185

186
#if defined(__GNUC__) && __GNUC__ <= 3
187
188
189
190

/* These are the GCC varidaic macros, so that older versions of GCC don't
 * break. */

Nick Mathewson's avatar
Nick Mathewson committed
191
192
/** Log a message at level <b>severity</b>, using a pretty-printed version
 * of the current function name. */
193
#define log_fn(severity, domain, args...)               \
194
  log_fn_(severity, domain, __FUNCTION__, args)
195
196
197
198
/** As log_fn, but use <b>ratelim</b> (an instance of ratelim_t) to control
 * the frequency at which messages can appear.
 */
#define log_fn_ratelim(ratelim, severity, domain, args...)      \
199
  log_fn_ratelim_(ratelim, severity, domain, __FUNCTION__, args)
200
#define log_debug(domain, args...)                                      \
201
  STMT_BEGIN                                                            \
202
    if (PREDICT_UNLIKELY(log_global_min_severity_ == LOG_DEBUG))        \
203
      log_fn_(LOG_DEBUG, domain, __FUNCTION__, args);            \
204
  STMT_END
205
#define log_info(domain, args...)                           \
206
  log_fn_(LOG_INFO, domain, __FUNCTION__, args)
207
#define log_notice(domain, args...)                         \
208
  log_fn_(LOG_NOTICE, domain, __FUNCTION__, args)
209
#define log_warn(domain, args...)                           \
210
  log_fn_(LOG_WARN, domain, __FUNCTION__, args)
211
#define log_err(domain, args...)                            \
212
  log_fn_(LOG_ERR, domain, __FUNCTION__, args)
213
214

#else /* ! defined(__GNUC__) */
215

216
217
218
219
220
/* Here are the c99 variadic macros, to work with non-GCC compilers */

#define log_debug(domain, args, ...)                                        \
  STMT_BEGIN                                                                \
    if (PREDICT_UNLIKELY(log_global_min_severity_ == LOG_DEBUG))            \
221
      log_fn_(LOG_DEBUG, domain, __FUNCTION__, args, ##__VA_ARGS__); \
222
223
  STMT_END
#define log_info(domain, args,...)                                      \
224
  log_fn_(LOG_INFO, domain, __FUNCTION__, args, ##__VA_ARGS__)
225
#define log_notice(domain, args,...)                                    \
226
  log_fn_(LOG_NOTICE, domain, __FUNCTION__, args, ##__VA_ARGS__)
227
#define log_warn(domain, args,...)                                      \
228
  log_fn_(LOG_WARN, domain, __FUNCTION__, args, ##__VA_ARGS__)
229
#define log_err(domain, args,...)                                       \
230
  log_fn_(LOG_ERR, domain, __FUNCTION__, args, ##__VA_ARGS__)
231
232
233
/** Log a message at level <b>severity</b>, using a pretty-printed version
 * of the current function name. */
#define log_fn(severity, domain, args,...)                              \
234
  log_fn_(severity, domain, __FUNCTION__, args, ##__VA_ARGS__)
235
236
237
238
/** As log_fn, but use <b>ratelim</b> (an instance of ratelim_t) to control
 * the frequency at which messages can appear.
 */
#define log_fn_ratelim(ratelim, severity, domain, args,...)      \
239
  log_fn_ratelim_(ratelim, severity, domain, __FUNCTION__, \
240
241
                  args, ##__VA_ARGS__)
#endif
242

243
244
245
246
247
248
#ifdef LOG_PRIVATE
MOCK_DECL(STATIC void, logv, (int severity, log_domain_mask_t domain,
    const char *funcname, const char *suffix, const char *format,
    va_list ap) CHECK_PRINTF(5,0));
#endif

249
# define TOR_TORLOG_H
Roger Dingledine's avatar
Roger Dingledine committed
250
#endif
Nick Mathewson's avatar
Nick Mathewson committed
251