Make new logging stuff work on windows; fix a couple of windows typos.
[tor.git] / src / common / log.h
blob40d66aab2ad642de8bd533ea62b11556c3c3a49d
1 /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar.
2 * Copyright 2004-2005 Roger Dingledine, Nick Mathewson */
3 /* See LICENSE for licensing information */
4 /* $Id$ */
6 /**
7 * \file log.h
9 * \brief Headers for log.c
10 **/
12 #ifndef __LOG_H
13 #define LOG_H_ID "$Id$"
15 #include "../common/compat.h"
17 #ifdef HAVE_SYSLOG_H
18 #include <syslog.h>
19 #define LOG_WARN LOG_WARNING
20 #if LOG_DEBUG < LOG_ERR
21 #error "Your syslog.h thinks high numbers are more important. We aren't prepared to deal with that."
22 #endif
23 #else
24 /* XXXX Note: The code was originally written to refer to severities,
25 * with 0 being the least severe; while syslog's logging code refers to
26 * priorities, with 0 being the most important. Thus, all our comparisons
27 * needed to be reversed when we added syslog support.
29 * The upshot of this is that comments about log levels may be messed
30 * up: for "maximum severity" read "most severe" and "numerically
31 * *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, FILE *stream);
98 int add_file_log(int severityMin, int severityMax, const char *filename);
99 #ifdef HAVE_SYSLOG_H
100 int add_syslog_log(int loglevelMin, int loglevelMax);
101 #endif
102 int add_callback_log(int loglevelMin, int loglevelMax, log_callback cb);
103 int get_min_log_level(void);
104 void switch_logs_debug(void);
105 void close_logs(void);
106 void reset_logs(void);
107 void add_temp_log(void);
108 void close_temp_logs(void);
109 void mark_logs_temp(void);
110 void configure_libevent_logging(void);
111 void suppress_libevent_log_msg(const char *msg);
112 void change_callback_log_severity(int loglevelMin, int loglevelMax,
113 log_callback cb);
115 /* Outputs a message to stdout */
116 void _log(int severity, uint32_t domain, const char *format, ...) CHECK_PRINTF(3,4);
117 #define log _log /* hack it so we don't conflict with log() as much */
119 #ifdef __GNUC__
120 void _log_fn(int severity, uint32_t domain,
121 const char *funcname, const char *format, ...)
122 CHECK_PRINTF(4,5);
123 /** Log a message at level <b>severity</b>, using a pretty-printed version
124 * of the current function name. */
125 #define log_fn(severity, domain, args...) \
126 _log_fn(severity, domain, __PRETTY_FUNCTION__, args)
127 #define debug(domain, args...) \
128 _log_fn(LOG_DEBUG, domain, __PRETTY_FUNCTION__, args)
129 #define info(domain, args...) \
130 _log_fn(LOG_INFO, domain, __PRETTY_FUNCTION__, args)
131 #define notice(domain, args...) \
132 _log_fn(LOG_NOTICE, domain, __PRETTY_FUNCTION__, args)
133 #define warn(domain, args...) \
134 _log_fn(LOG_WARN, domain, __PRETTY_FUNCTION__, args)
135 #define err(domain, args...) \
136 _log_fn(LOG_ERR, domain, __PRETTY_FUNCTION__, args)
138 #else /* ! defined(__GNUC__) */
140 void _log_fn(int severity, uint32_t domain, const char *format, ...);
141 void debug(uint32_t domain, const char *format, ...);
142 void info(uint32_t domain, const char *format, ...);
143 void notice(uint32_t domain, const char *format, ...);
144 void warn(uint32_t domain, const char *format, ...);
145 void err(uint32_t domain, const char *format, ...);
147 #if defined(_MSC_VER) && _MSC_VER < 1300
148 /* MSVC 6 and earlier don't have __FUNCTION__, or even __LINE__. */
149 #define log_fn _log_fn
151 #define debug _debug
152 #define info _info
153 #define notice _notice
154 #define warn _warn
155 #define err _err
157 #else
158 /* We don't have GCC's varargs macros, so use a global variable to pass the
159 * function name to log_fn */
160 extern const char *_log_fn_function_name;
161 /* We abuse the comma operator here, since we can't use the standard
162 * do {...} while (0) trick to wrap this macro, since the macro can't take
163 * arguments. */
164 #define log_fn (_log_fn_function_name=__FUNCTION__),_log_fn
166 #define debug (_log_fn_function_name=__FUNCTION__),_debug
167 #define info (_log_fn_function_name=__FUNCTION__),_info
168 #define notice (_log_fn_function_name=__FUNCTION__),_notice
169 #define warn (_log_fn_function_name=__FUNCTION__),_warn
170 #define err (_log_fn_function_name=__FUNCTION__),_err
172 #endif
174 #endif /* !GNUC */
176 # define __LOG_H
177 #endif