log.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. /**
  7. * \file log.h
  8. *
  9. * \brief Headers for log.c
  10. **/
  11. #ifndef TOR_TORLOG_H
  12. #define TOR_TORLOG_H
  13. #include <stdarg.h>
  14. #include "lib/cc/torint.h"
  15. #include "lib/cc/compat_compiler.h"
  16. #include "lib/defs/logging_types.h"
  17. #include "lib/testsupport/testsupport.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 /* !(defined(HAVE_SYSLOG_H)) */
  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.
  29. *
  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".
  32. */
  33. /** Debug-level severity: for hyper-verbose messages of no interest to
  34. * anybody but developers. */
  35. #define LOG_DEBUG 7
  36. /** Info-level severity: for messages that appear frequently during normal
  37. * operation. */
  38. #define LOG_INFO 6
  39. /** Notice-level severity: for messages that appear infrequently
  40. * during normal operation; that the user will probably care about;
  41. * and that are not errors.
  42. */
  43. #define LOG_NOTICE 5
  44. /** Warn-level severity: for messages that only appear when something has gone
  45. * wrong. */
  46. #define LOG_WARN 4
  47. /** Error-level severity: for messages that only appear when something has gone
  48. * very wrong, and the Tor process can no longer proceed. */
  49. #define LOG_ERR 3
  50. #endif /* defined(HAVE_SYSLOG_H) */
  51. /* Logging domains */
  52. /** Catch-all for miscellaneous events and fatal errors. */
  53. #define LD_GENERAL (UINT64_C(1)<<0)
  54. /** The cryptography subsystem. */
  55. #define LD_CRYPTO (UINT64_C(1)<<1)
  56. /** Networking. */
  57. #define LD_NET (UINT64_C(1)<<2)
  58. /** Parsing and acting on our configuration. */
  59. #define LD_CONFIG (UINT64_C(1)<<3)
  60. /** Reading and writing from the filesystem. */
  61. #define LD_FS (UINT64_C(1)<<4)
  62. /** Other servers' (non)compliance with the Tor protocol. */
  63. #define LD_PROTOCOL (UINT64_C(1)<<5)
  64. /** Memory management. */
  65. #define LD_MM (UINT64_C(1)<<6)
  66. /** HTTP implementation. */
  67. #define LD_HTTP (UINT64_C(1)<<7)
  68. /** Application (socks) requests. */
  69. #define LD_APP (UINT64_C(1)<<8)
  70. /** Communication via the controller protocol. */
  71. #define LD_CONTROL (UINT64_C(1)<<9)
  72. /** Building, using, and managing circuits. */
  73. #define LD_CIRC (UINT64_C(1)<<10)
  74. /** Hidden services. */
  75. #define LD_REND (UINT64_C(1)<<11)
  76. /** Internal errors in this Tor process. */
  77. #define LD_BUG (UINT64_C(1)<<12)
  78. /** Learning and using information about Tor servers. */
  79. #define LD_DIR (UINT64_C(1)<<13)
  80. /** Learning and using information about Tor servers. */
  81. #define LD_DIRSERV (UINT64_C(1)<<14)
  82. /** Onion routing protocol. */
  83. #define LD_OR (UINT64_C(1)<<15)
  84. /** Generic edge-connection functionality. */
  85. #define LD_EDGE (UINT64_C(1)<<16)
  86. #define LD_EXIT LD_EDGE
  87. /** Bandwidth accounting. */
  88. #define LD_ACCT (UINT64_C(1)<<17)
  89. /** Router history */
  90. #define LD_HIST (UINT64_C(1)<<18)
  91. /** OR handshaking */
  92. #define LD_HANDSHAKE (UINT64_C(1)<<19)
  93. /** Heartbeat messages */
  94. #define LD_HEARTBEAT (UINT64_C(1)<<20)
  95. /** Abstract channel_t code */
  96. #define LD_CHANNEL (UINT64_C(1)<<21)
  97. /** Scheduler */
  98. #define LD_SCHED (UINT64_C(1)<<22)
  99. /** Guard nodes */
  100. #define LD_GUARD (UINT64_C(1)<<23)
  101. /** Generation and application of consensus diffs. */
  102. #define LD_CONSDIFF (UINT64_C(1)<<24)
  103. /** Denial of Service mitigation. */
  104. #define LD_DOS (UINT64_C(1)<<25)
  105. /** Processes */
  106. #define LD_PROCESS (UINT64_C(1)<<26)
  107. /** Pluggable Transports. */
  108. #define LD_PT (UINT64_C(1)<<27)
  109. /** Bootstrap tracker. */
  110. #define LD_BTRACK (UINT64_C(1)<<28)
  111. /** Message-passing backend. */
  112. #define LD_MESG (UINT64_C(1)<<29)
  113. #define N_LOGGING_DOMAINS 30
  114. /** First bit that is reserved in log_domain_mask_t for non-domain flags. */
  115. #define LOWEST_RESERVED_LD_FLAG_ (UINT64_C(1)<<61)
  116. #ifdef TOR_UNIT_TESTS
  117. /** This log message should not be intercepted by mock_saving_logv */
  118. #define LD_NO_MOCK (UINT64_C(1)<<61)
  119. #endif
  120. /** This log message is not safe to send to a callback-based logger
  121. * immediately. Used as a flag, not a log domain. */
  122. #define LD_NOCB (UINT64_C(1)<<62)
  123. /** This log message should not include a function name, even if it otherwise
  124. * would. Used as a flag, not a log domain. */
  125. #define LD_NOFUNCNAME (UINT64_C(1)<<63)
  126. /** Configures which severities are logged for each logging domain for a given
  127. * log target. */
  128. typedef struct log_severity_list_t {
  129. /** For each log severity, a bitmask of which domains a given logger is
  130. * logging. */
  131. log_domain_mask_t masks[LOG_DEBUG-LOG_ERR+1];
  132. } log_severity_list_t;
  133. /** Callback type used for add_callback_log. */
  134. typedef void (*log_callback)(int severity, log_domain_mask_t domain,
  135. const char *msg);
  136. void init_logging(int disable_startup_queue);
  137. int parse_log_level(const char *level);
  138. const char *log_level_to_string(int level);
  139. int parse_log_severity_config(const char **cfg,
  140. log_severity_list_t *severity_out);
  141. void set_log_severity_config(int minSeverity, int maxSeverity,
  142. log_severity_list_t *severity_out);
  143. void add_stream_log(const log_severity_list_t *severity, const char *name,
  144. int fd);
  145. int add_file_log(const log_severity_list_t *severity,
  146. const char *filename,
  147. int fd);
  148. #ifdef HAVE_SYSLOG_H
  149. int add_syslog_log(const log_severity_list_t *severity,
  150. const char* syslog_identity_tag);
  151. #endif // HAVE_SYSLOG_H.
  152. #ifdef HAVE_ANDROID_LOG_H
  153. int add_android_log(const log_severity_list_t *severity,
  154. const char *android_identity_tag);
  155. #endif // HAVE_ANDROID_LOG_H.
  156. int add_callback_log(const log_severity_list_t *severity, log_callback cb);
  157. typedef void (*pending_callback_callback)(void);
  158. void logs_set_pending_callback_callback(pending_callback_callback cb);
  159. void logs_set_domain_logging(int enabled);
  160. int get_min_log_level(void);
  161. void switch_logs_debug(void);
  162. void logs_free_all(void);
  163. void logs_close_sigsafe(void);
  164. void add_temp_log(int min_severity);
  165. void close_temp_logs(void);
  166. void rollback_log_changes(void);
  167. void mark_logs_temp(void);
  168. void change_callback_log_severity(int loglevelMin, int loglevelMax,
  169. log_callback cb);
  170. void flush_pending_log_callbacks(void);
  171. void flush_log_messages_from_startup(void);
  172. void log_set_application_name(const char *name);
  173. void set_log_time_granularity(int granularity_msec);
  174. void truncate_logs(void);
  175. void tor_log(int severity, log_domain_mask_t domain, const char *format, ...)
  176. CHECK_PRINTF(3,4);
  177. void tor_log_update_sigsafe_err_fds(void);
  178. struct smartlist_t;
  179. void tor_log_get_logfile_names(struct smartlist_t *out);
  180. extern int log_global_min_severity_;
  181. #ifdef TOR_COVERAGE
  182. /* For coverage builds, we try to avoid our log_debug optimization, since it
  183. * can have weird effects on internal macro coverage. */
  184. #define debug_logging_enabled() (1)
  185. #else
  186. static inline bool debug_logging_enabled(void);
  187. /**
  188. * Return true iff debug logging is enabled for at least one domain.
  189. */
  190. static inline bool debug_logging_enabled(void)
  191. {
  192. return PREDICT_UNLIKELY(log_global_min_severity_ == LOG_DEBUG);
  193. }
  194. #endif /* defined(TOR_COVERAGE) */
  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. int log_message_is_interesting(int severity, log_domain_mask_t domain);
  204. void tor_log_string(int severity, log_domain_mask_t domain,
  205. const char *function, const char *string);
  206. #if defined(__GNUC__) && __GNUC__ <= 3
  207. /* These are the GCC varidaic macros, so that older versions of GCC don't
  208. * break. */
  209. /** Log a message at level <b>severity</b>, using a pretty-printed version
  210. * of the current function name. */
  211. #define log_fn(severity, domain, args...) \
  212. log_fn_(severity, domain, __FUNCTION__, args)
  213. /** As log_fn, but use <b>ratelim</b> (an instance of ratelim_t) to control
  214. * the frequency at which messages can appear.
  215. */
  216. #define log_fn_ratelim(ratelim, severity, domain, args...) \
  217. log_fn_ratelim_(ratelim, severity, domain, __FUNCTION__, args)
  218. #define log_debug(domain, args...) \
  219. STMT_BEGIN \
  220. if (debug_logging_enabled()) \
  221. log_fn_(LOG_DEBUG, domain, __FUNCTION__, args); \
  222. STMT_END
  223. #define log_info(domain, args...) \
  224. log_fn_(LOG_INFO, domain, __FUNCTION__, args)
  225. #define log_notice(domain, args...) \
  226. log_fn_(LOG_NOTICE, domain, __FUNCTION__, args)
  227. #define log_warn(domain, args...) \
  228. log_fn_(LOG_WARN, domain, __FUNCTION__, args)
  229. #define log_err(domain, args...) \
  230. log_fn_(LOG_ERR, domain, __FUNCTION__, args)
  231. #else /* !(defined(__GNUC__) && __GNUC__ <= 3) */
  232. /* Here are the c99 variadic macros, to work with non-GCC compilers */
  233. #define log_debug(domain, args, ...) \
  234. STMT_BEGIN \
  235. if (debug_logging_enabled()) \
  236. log_fn_(LOG_DEBUG, domain, __FUNCTION__, args, ##__VA_ARGS__); \
  237. STMT_END
  238. #define log_info(domain, args,...) \
  239. log_fn_(LOG_INFO, domain, __FUNCTION__, args, ##__VA_ARGS__)
  240. #define log_notice(domain, args,...) \
  241. log_fn_(LOG_NOTICE, domain, __FUNCTION__, args, ##__VA_ARGS__)
  242. #define log_warn(domain, args,...) \
  243. log_fn_(LOG_WARN, domain, __FUNCTION__, args, ##__VA_ARGS__)
  244. #define log_err(domain, args,...) \
  245. log_fn_(LOG_ERR, domain, __FUNCTION__, args, ##__VA_ARGS__)
  246. /** Log a message at level <b>severity</b>, using a pretty-printed version
  247. * of the current function name. */
  248. #define log_fn(severity, domain, args,...) \
  249. log_fn_(severity, domain, __FUNCTION__, args, ##__VA_ARGS__)
  250. /** As log_fn, but use <b>ratelim</b> (an instance of ratelim_t) to control
  251. * the frequency at which messages can appear.
  252. */
  253. #define log_fn_ratelim(ratelim, severity, domain, args,...) \
  254. log_fn_ratelim_(ratelim, severity, domain, __FUNCTION__, \
  255. args, ##__VA_ARGS__)
  256. #endif /* defined(__GNUC__) && __GNUC__ <= 3 */
  257. /** This defines log levels that are linked in the Rust log module, rather
  258. * than re-defining these in both Rust and C.
  259. *
  260. * C_RUST_COUPLED src/rust/tor_log LogSeverity, LogDomain
  261. */
  262. extern const int LOG_WARN_;
  263. extern const int LOG_NOTICE_;
  264. extern const log_domain_mask_t LD_NET_;
  265. extern const log_domain_mask_t LD_GENERAL_;
  266. #ifdef LOG_PRIVATE
  267. MOCK_DECL(STATIC void, logv, (int severity, log_domain_mask_t domain,
  268. const char *funcname, const char *suffix, const char *format,
  269. va_list ap) CHECK_PRINTF(5,0));
  270. #endif
  271. #endif /* !defined(TOR_TORLOG_H) */