r11606@catbus: nickm | 2007-01-30 16:52:23 -0500
[tor.git] / src / common / log.h
blob8122a94aa69c9b101b42b16114e6f224cd155146
1 /* Copyright (c) 2001 Matej Pfajfar.
2 * Copyright (c) 2001-2004, Roger Dingledine.
3 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. */
4 /* See LICENSE for licensing information */
5 /* $Id$ */
7 /**
8 * \file log.h
10 * \brief Headers for log.c
11 **/
13 #ifndef __LOG_H
14 #define LOG_H_ID "$Id$"
16 #include "../common/compat.h"
18 #ifdef HAVE_SYSLOG_H
19 #include <syslog.h>
20 #define LOG_WARN LOG_WARNING
21 #if LOG_DEBUG < LOG_ERR
22 #error "Your syslog.h thinks high numbers are more important. " \
23 "We aren't prepared to deal with that."
24 #endif
25 #else
26 /* Note: Syslog's logging code refers to priorities, with 0 being the most
27 * important. Thus, all our comparisons needed to be reversed when we added
28 * syslog support.
30 * The upshot of this is that comments about log levels may be messed up: for
31 * "maximum severity" read "most severe" and "numerically *lowest* severity".
34 /** Debug-level severity: for hyper-verbose messages of no interest to
35 * anybody but developers. */
36 #define LOG_DEBUG 7
37 /** Info-level severity: for messages that appear frequently during normal
38 * operation. */
39 #define LOG_INFO 6
40 /** Notice-level severity: for messages that appear infrequently
41 * during normal operation; that the user will probably care about;
42 * and that are not errors.
44 #define LOG_NOTICE 5
45 /** Warn-level severity: for messages that only appear when something has gone
46 * wrong. */
47 #define LOG_WARN 4
48 /** Error-level severity: for messages that only appear when something has gone
49 * very wrong, and the Tor process can no longer proceed. */
50 #define LOG_ERR 3
51 #endif
53 /* Logging domains */
55 /** Catch-all for miscellaneous events and fatal errors. */
56 #define LD_GENERAL (1u<<0)
57 /** The cryptography subsytem. */
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)
83 /** Learning and using information about Tor servers. */
84 #define LD_DIRSERV (1u<<14)
85 /** Onion routing protocol. */
86 #define LD_OR (1u<<15)
87 /** Generic edge-connection functionality. */
88 #define LD_EDGE (1u<<16)
89 #define LD_EXIT LD_EDGE
90 /** Bandwidth accounting. */
91 #define LD_ACCT (1u<<17)
93 typedef void (*log_callback)(int severity, uint32_t domain, const char *msg);
95 int parse_log_level(const char *level);
96 const char *log_level_to_string(int level);
97 void add_stream_log(int severityMin, int severityMax, const char *name,
98 FILE *stream);
99 int add_file_log(int severityMin, int severityMax, const char *filename);
100 #ifdef HAVE_SYSLOG_H
101 int add_syslog_log(int loglevelMin, int loglevelMax);
102 #endif
103 int add_callback_log(int loglevelMin, int loglevelMax, log_callback cb);
104 int get_min_log_level(void);
105 void switch_logs_debug(void);
106 void close_logs(void);
107 void add_temp_log(void);
108 void close_temp_logs(void);
109 void rollback_log_changes(void);
110 void mark_logs_temp(void);
111 void configure_libevent_logging(void);
112 void suppress_libevent_log_msg(const char *msg);
113 void change_callback_log_severity(int loglevelMin, int loglevelMax,
114 log_callback cb);
116 /* Outputs a message to stdout */
117 void _log(int severity, uint32_t domain, const char *format, ...)
118 CHECK_PRINTF(3,4);
119 #define log _log /* hack it so we don't conflict with log() as much */
121 #ifdef __GNUC__
122 void _log_fn(int severity, uint32_t domain,
123 const char *funcname, const char *format, ...)
124 CHECK_PRINTF(4,5);
125 /** Log a message at level <b>severity</b>, using a pretty-printed version
126 * of the current function name. */
127 #define log_fn(severity, domain, args...) \
128 _log_fn(severity, domain, __PRETTY_FUNCTION__, args)
129 #define log_debug(domain, args...) \
130 _log_fn(LOG_DEBUG, domain, __PRETTY_FUNCTION__, args)
131 #define log_info(domain, args...) \
132 _log_fn(LOG_INFO, domain, __PRETTY_FUNCTION__, args)
133 #define log_notice(domain, args...) \
134 _log_fn(LOG_NOTICE, domain, __PRETTY_FUNCTION__, args)
135 #define log_warn(domain, args...) \
136 _log_fn(LOG_WARN, domain, __PRETTY_FUNCTION__, args)
137 #define log_err(domain, args...) \
138 _log_fn(LOG_ERR, domain, __PRETTY_FUNCTION__, args)
140 #else /* ! defined(__GNUC__) */
142 void _log_fn(int severity, uint32_t domain, const char *format, ...);
143 void _log_debug(uint32_t domain, const char *format, ...);
144 void _log_info(uint32_t domain, const char *format, ...);
145 void _log_notice(uint32_t domain, const char *format, ...);
146 void _log_warn(uint32_t domain, const char *format, ...);
147 void _log_err(uint32_t domain, const char *format, ...);
149 #if defined(_MSC_VER) && _MSC_VER < 1300
150 /* MSVC 6 and earlier don't have __func__, or even __LINE__. */
151 #define log_fn _log_fn
152 #define log_debug _log_debug
153 #define log_info _log_info
154 #define log_notice _log_notice
155 #define log_warn _log_warn
156 #define log_err _log_err
158 #define debug _debug
159 #define info _info
160 #define notice _notice
161 #define warn _warn
162 #define err _err
164 #else
165 /* We don't have GCC's varargs macros, so use a global variable to pass the
166 * function name to log_fn */
167 extern const char *_log_fn_function_name;
168 /* We abuse the comma operator here, since we can't use the standard
169 * do {...} while (0) trick to wrap this macro, since the macro can't take
170 * arguments. */
171 #define log_fn (_log_fn_function_name=__func__),_log_fn
172 #define log_debug (_log_fn_function_name=__func__),_log_debug
173 #define log_info (_log_fn_function_name=__func__),_log_info
174 #define log_notice (_log_fn_function_name=__func__),_log_notice
175 #define log_warn (_log_fn_function_name=__func__),_log_warn
176 #define log_err (_log_fn_function_name=__func__),_log_err
178 #define debug (_log_fn_function_name=__func__),_debug
179 #define info (_log_fn_function_name=__func__),_info
180 #define notice (_log_fn_function_name=__func__),_notice
181 #define warn (_log_fn_function_name=__func__),_warn
182 #define err (_log_fn_function_name=__func__),_err
184 #endif
186 #endif /* !GNUC */
188 # define __LOG_H
189 #endif