torlog.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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-2013, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file torlog.h
  8. *
  9. * \brief Headers for log.c
  10. **/
  11. #ifndef TOR_TORLOG_H
  12. #include "compat.h"
  13. #ifdef HAVE_SYSLOG_H
  14. #include <syslog.h>
  15. #define LOG_WARN LOG_WARNING
  16. #if LOG_DEBUG < LOG_ERR
  17. #error "Your syslog.h thinks high numbers are more important. " \
  18. "We aren't prepared to deal with that."
  19. #endif
  20. #else
  21. /* Note: Syslog's logging code refers to priorities, with 0 being the most
  22. * important. Thus, all our comparisons needed to be reversed when we added
  23. * syslog support.
  24. *
  25. * The upshot of this is that comments about log levels may be messed up: for
  26. * "maximum severity" read "most severe" and "numerically *lowest* severity".
  27. */
  28. /** Debug-level severity: for hyper-verbose messages of no interest to
  29. * anybody but developers. */
  30. #define LOG_DEBUG 7
  31. /** Info-level severity: for messages that appear frequently during normal
  32. * operation. */
  33. #define LOG_INFO 6
  34. /** Notice-level severity: for messages that appear infrequently
  35. * during normal operation; that the user will probably care about;
  36. * and that are not errors.
  37. */
  38. #define LOG_NOTICE 5
  39. /** Warn-level severity: for messages that only appear when something has gone
  40. * wrong. */
  41. #define LOG_WARN 4
  42. /** Error-level severity: for messages that only appear when something has gone
  43. * very wrong, and the Tor process can no longer proceed. */
  44. #define LOG_ERR 3
  45. #endif
  46. /* Logging domains */
  47. /** Catch-all for miscellaneous events and fatal errors. */
  48. #define LD_GENERAL (1u<<0)
  49. /** The cryptography subsystem. */
  50. #define LD_CRYPTO (1u<<1)
  51. /** Networking. */
  52. #define LD_NET (1u<<2)
  53. /** Parsing and acting on our configuration. */
  54. #define LD_CONFIG (1u<<3)
  55. /** Reading and writing from the filesystem. */
  56. #define LD_FS (1u<<4)
  57. /** Other servers' (non)compliance with the Tor protocol. */
  58. #define LD_PROTOCOL (1u<<5)
  59. /** Memory management. */
  60. #define LD_MM (1u<<6)
  61. /** HTTP implementation. */
  62. #define LD_HTTP (1u<<7)
  63. /** Application (socks) requests. */
  64. #define LD_APP (1u<<8)
  65. /** Communication via the controller protocol. */
  66. #define LD_CONTROL (1u<<9)
  67. /** Building, using, and managing circuits. */
  68. #define LD_CIRC (1u<<10)
  69. /** Hidden services. */
  70. #define LD_REND (1u<<11)
  71. /** Internal errors in this Tor process. */
  72. #define LD_BUG (1u<<12)
  73. /** Learning and using information about Tor servers. */
  74. #define LD_DIR (1u<<13)
  75. /** Learning and using information about Tor servers. */
  76. #define LD_DIRSERV (1u<<14)
  77. /** Onion routing protocol. */
  78. #define LD_OR (1u<<15)
  79. /** Generic edge-connection functionality. */
  80. #define LD_EDGE (1u<<16)
  81. #define LD_EXIT LD_EDGE
  82. /** Bandwidth accounting. */
  83. #define LD_ACCT (1u<<17)
  84. /** Router history */
  85. #define LD_HIST (1u<<18)
  86. /** OR handshaking */
  87. #define LD_HANDSHAKE (1u<<19)
  88. /** Heartbeat messages */
  89. #define LD_HEARTBEAT (1u<<20)
  90. /** Abstract channel_t code */
  91. #define LD_CHANNEL (1u<<21)
  92. /** Number of logging domains in the code. */
  93. #define N_LOGGING_DOMAINS 22
  94. /** This log message is not safe to send to a callback-based logger
  95. * immediately. Used as a flag, not a log domain. */
  96. #define LD_NOCB (1u<<31)
  97. /** Mask of zero or more log domains, OR'd together. */
  98. typedef uint32_t log_domain_mask_t;
  99. /** Configures which severities are logged for each logging domain for a given
  100. * log target. */
  101. typedef struct log_severity_list_t {
  102. /** For each log severity, a bitmask of which domains a given logger is
  103. * logging. */
  104. log_domain_mask_t masks[LOG_DEBUG-LOG_ERR+1];
  105. } log_severity_list_t;
  106. /** Callback type used for add_callback_log. */
  107. typedef void (*log_callback)(int severity, uint32_t domain, const char *msg);
  108. void init_logging(void);
  109. int parse_log_level(const char *level);
  110. const char *log_level_to_string(int level);
  111. int parse_log_severity_config(const char **cfg,
  112. log_severity_list_t *severity_out);
  113. void set_log_severity_config(int minSeverity, int maxSeverity,
  114. log_severity_list_t *severity_out);
  115. void add_stream_log(const log_severity_list_t *severity, const char *name,
  116. int fd);
  117. int add_file_log(const log_severity_list_t *severity, const char *filename);
  118. #ifdef HAVE_SYSLOG_H
  119. int add_syslog_log(const log_severity_list_t *severity);
  120. #endif
  121. int add_callback_log(const log_severity_list_t *severity, log_callback cb);
  122. void logs_set_domain_logging(int enabled);
  123. int get_min_log_level(void);
  124. void switch_logs_debug(void);
  125. void logs_free_all(void);
  126. void add_temp_log(int min_severity);
  127. int get_err_logging_fd(void);
  128. void close_temp_logs(void);
  129. void rollback_log_changes(void);
  130. void mark_logs_temp(void);
  131. void change_callback_log_severity(int loglevelMin, int loglevelMax,
  132. log_callback cb);
  133. void flush_pending_log_callbacks(void);
  134. void log_set_application_name(const char *name);
  135. void set_log_time_granularity(int granularity_msec);
  136. void tor_log(int severity, log_domain_mask_t domain, const char *format, ...)
  137. CHECK_PRINTF(3,4);
  138. #if defined(__GNUC__) || defined(RUNNING_DOXYGEN)
  139. extern int log_global_min_severity_;
  140. void log_fn_(int severity, log_domain_mask_t domain,
  141. const char *funcname, const char *format, ...)
  142. CHECK_PRINTF(4,5);
  143. struct ratelim_t;
  144. void log_fn_ratelim_(struct ratelim_t *ratelim, int severity,
  145. log_domain_mask_t domain, const char *funcname,
  146. const char *format, ...)
  147. CHECK_PRINTF(5,6);
  148. /** Log a message at level <b>severity</b>, using a pretty-printed version
  149. * of the current function name. */
  150. #define log_fn(severity, domain, args...) \
  151. log_fn_(severity, domain, __PRETTY_FUNCTION__, args)
  152. /** As log_fn, but use <b>ratelim</b> (an instance of ratelim_t) to control
  153. * the frequency at which messages can appear.
  154. */
  155. #define log_fn_ratelim(ratelim, severity, domain, args...) \
  156. log_fn_ratelim_(ratelim, severity, domain, __PRETTY_FUNCTION__, args)
  157. #define log_debug(domain, args...) \
  158. STMT_BEGIN \
  159. if (PREDICT_UNLIKELY(log_global_min_severity_ == LOG_DEBUG)) \
  160. log_fn_(LOG_DEBUG, domain, __PRETTY_FUNCTION__, args); \
  161. STMT_END
  162. #define log_info(domain, args...) \
  163. log_fn_(LOG_INFO, domain, __PRETTY_FUNCTION__, args)
  164. #define log_notice(domain, args...) \
  165. log_fn_(LOG_NOTICE, domain, __PRETTY_FUNCTION__, args)
  166. #define log_warn(domain, args...) \
  167. log_fn_(LOG_WARN, domain, __PRETTY_FUNCTION__, args)
  168. #define log_err(domain, args...) \
  169. log_fn_(LOG_ERR, domain, __PRETTY_FUNCTION__, args)
  170. #else /* ! defined(__GNUC__) */
  171. void log_fn_(int severity, log_domain_mask_t domain, const char *format, ...);
  172. struct ratelim_t;
  173. void log_fn_ratelim_(struct ratelim_t *ratelim, int severity,
  174. log_domain_mask_t domain, const char *format, ...);
  175. void log_debug_(log_domain_mask_t domain, const char *format, ...);
  176. void log_info_(log_domain_mask_t domain, const char *format, ...);
  177. void log_notice_(log_domain_mask_t domain, const char *format, ...);
  178. void log_warn_(log_domain_mask_t domain, const char *format, ...);
  179. void log_err_(log_domain_mask_t domain, const char *format, ...);
  180. #if defined(_MSC_VER) && _MSC_VER < 1300
  181. /* MSVC 6 and earlier don't have __func__, or even __LINE__. */
  182. #define log_fn log_fn_
  183. #define log_fn_ratelim log_fn_ratelim_
  184. #define log_debug log_debug_
  185. #define log_info log_info_
  186. #define log_notice log_notice_
  187. #define log_warn log_warn_
  188. #define log_err log_err_
  189. #else
  190. /* We don't have GCC's varargs macros, so use a global variable to pass the
  191. * function name to log_fn */
  192. extern const char *log_fn_function_name_;
  193. /* We abuse the comma operator here, since we can't use the standard
  194. * do {...} while (0) trick to wrap this macro, since the macro can't take
  195. * arguments. */
  196. #define log_fn (log_fn_function_name_=__func__),log_fn_
  197. #define log_fn_ratelim (log_fn_function_name_=__func__),log_fn_ratelim_
  198. #define log_debug (log_fn_function_name_=__func__),log_debug_
  199. #define log_info (log_fn_function_name_=__func__),log_info_
  200. #define log_notice (log_fn_function_name_=__func__),log_notice_
  201. #define log_warn (log_fn_function_name_=__func__),log_warn_
  202. #define log_err (log_fn_function_name_=__func__),log_err_
  203. #endif
  204. #endif /* !GNUC */
  205. # define TOR_TORLOG_H
  206. #endif