log.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. /** The number of log domains. */
  114. #define N_LOGGING_DOMAINS 30
  115. /** The highest log domain */
  116. #define HIGHEST_RESERVED_LD_DOMAIN_ (UINT64_C(1)<<(N_LOGGING_DOMAINS - 1))
  117. /** All log domains. */
  118. #define LD_ALL_DOMAINS ((~(UINT64_C(0)))>>(64 - N_LOGGING_DOMAINS))
  119. /** The number of log flags. */
  120. #define N_LOGGING_FLAGS 3
  121. /** First bit that is reserved in log_domain_mask_t for non-domain flags. */
  122. #define LOWEST_RESERVED_LD_FLAG_ (UINT64_C(1)<<(64 - N_LOGGING_FLAGS))
  123. /** All log flags. */
  124. #define LD_ALL_FLAGS ((~(UINT64_C(0)))<<(64 - N_LOGGING_FLAGS))
  125. #ifdef TOR_UNIT_TESTS
  126. /** This log message should not be intercepted by mock_saving_logv */
  127. #define LD_NO_MOCK (UINT64_C(1)<<61)
  128. #endif
  129. /** This log message is not safe to send to a callback-based logger
  130. * immediately. Used as a flag, not a log domain. */
  131. #define LD_NOCB (UINT64_C(1)<<62)
  132. /** This log message should not include a function name, even if it otherwise
  133. * would. Used as a flag, not a log domain. */
  134. #define LD_NOFUNCNAME (UINT64_C(1)<<63)
  135. /** Configures which severities are logged for each logging domain for a given
  136. * log target. */
  137. typedef struct log_severity_list_t {
  138. /** For each log severity, a bitmask of which domains a given logger is
  139. * logging. */
  140. log_domain_mask_t masks[LOG_DEBUG-LOG_ERR+1];
  141. } log_severity_list_t;
  142. /** Callback type used for add_callback_log. */
  143. typedef void (*log_callback)(int severity, log_domain_mask_t domain,
  144. const char *msg);
  145. void init_logging(int disable_startup_queue);
  146. int parse_log_level(const char *level);
  147. const char *log_level_to_string(int level);
  148. int parse_log_severity_config(const char **cfg,
  149. log_severity_list_t *severity_out);
  150. void set_log_severity_config(int minSeverity, int maxSeverity,
  151. log_severity_list_t *severity_out);
  152. void add_stream_log(const log_severity_list_t *severity, const char *name,
  153. int fd);
  154. int add_file_log(const log_severity_list_t *severity,
  155. const char *filename,
  156. int fd);
  157. #ifdef HAVE_SYSLOG_H
  158. int add_syslog_log(const log_severity_list_t *severity,
  159. const char* syslog_identity_tag);
  160. #endif // HAVE_SYSLOG_H.
  161. #ifdef HAVE_ANDROID_LOG_H
  162. int add_android_log(const log_severity_list_t *severity,
  163. const char *android_identity_tag);
  164. #endif // HAVE_ANDROID_LOG_H.
  165. int add_callback_log(const log_severity_list_t *severity, log_callback cb);
  166. typedef void (*pending_callback_callback)(void);
  167. void logs_set_pending_callback_callback(pending_callback_callback cb);
  168. void logs_set_domain_logging(int enabled);
  169. int get_min_log_level(void);
  170. void switch_logs_debug(void);
  171. void logs_free_all(void);
  172. void logs_close_sigsafe(void);
  173. void add_temp_log(int min_severity);
  174. void close_temp_logs(void);
  175. void rollback_log_changes(void);
  176. void mark_logs_temp(void);
  177. void change_callback_log_severity(int loglevelMin, int loglevelMax,
  178. log_callback cb);
  179. void flush_pending_log_callbacks(void);
  180. void flush_log_messages_from_startup(void);
  181. void log_set_application_name(const char *name);
  182. void set_log_time_granularity(int granularity_msec);
  183. void truncate_logs(void);
  184. void tor_log(int severity, log_domain_mask_t domain, const char *format, ...)
  185. CHECK_PRINTF(3,4);
  186. void tor_log_update_sigsafe_err_fds(void);
  187. struct smartlist_t;
  188. void tor_log_get_logfile_names(struct smartlist_t *out);
  189. extern int log_global_min_severity_;
  190. #ifdef TOR_COVERAGE
  191. /* For coverage builds, we try to avoid our log_debug optimization, since it
  192. * can have weird effects on internal macro coverage. */
  193. #define debug_logging_enabled() (1)
  194. #else
  195. static inline bool debug_logging_enabled(void);
  196. /**
  197. * Return true iff debug logging is enabled for at least one domain.
  198. */
  199. static inline bool debug_logging_enabled(void)
  200. {
  201. return PREDICT_UNLIKELY(log_global_min_severity_ == LOG_DEBUG);
  202. }
  203. #endif /* defined(TOR_COVERAGE) */
  204. void log_fn_(int severity, log_domain_mask_t domain,
  205. const char *funcname, const char *format, ...)
  206. CHECK_PRINTF(4,5);
  207. struct ratelim_t;
  208. void log_fn_ratelim_(struct ratelim_t *ratelim, int severity,
  209. log_domain_mask_t domain, const char *funcname,
  210. const char *format, ...)
  211. CHECK_PRINTF(5,6);
  212. int log_message_is_interesting(int severity, log_domain_mask_t domain);
  213. void tor_log_string(int severity, log_domain_mask_t domain,
  214. const char *function, const char *string);
  215. #if defined(__GNUC__) && __GNUC__ <= 3
  216. /* These are the GCC varidaic macros, so that older versions of GCC don't
  217. * break. */
  218. /** Log a message at level <b>severity</b>, using a pretty-printed version
  219. * of the current function name. */
  220. #define log_fn(severity, domain, args...) \
  221. log_fn_(severity, domain, __FUNCTION__, args)
  222. /** As log_fn, but use <b>ratelim</b> (an instance of ratelim_t) to control
  223. * the frequency at which messages can appear.
  224. */
  225. #define log_fn_ratelim(ratelim, severity, domain, args...) \
  226. log_fn_ratelim_(ratelim, severity, domain, __FUNCTION__, args)
  227. #define log_debug(domain, args...) \
  228. STMT_BEGIN \
  229. if (debug_logging_enabled()) \
  230. log_fn_(LOG_DEBUG, domain, __FUNCTION__, args); \
  231. STMT_END
  232. #define log_info(domain, args...) \
  233. log_fn_(LOG_INFO, domain, __FUNCTION__, args)
  234. #define log_notice(domain, args...) \
  235. log_fn_(LOG_NOTICE, domain, __FUNCTION__, args)
  236. #define log_warn(domain, args...) \
  237. log_fn_(LOG_WARN, domain, __FUNCTION__, args)
  238. #define log_err(domain, args...) \
  239. log_fn_(LOG_ERR, domain, __FUNCTION__, args)
  240. #else /* !(defined(__GNUC__) && __GNUC__ <= 3) */
  241. /* Here are the c99 variadic macros, to work with non-GCC compilers */
  242. #define log_debug(domain, args, ...) \
  243. STMT_BEGIN \
  244. if (debug_logging_enabled()) \
  245. log_fn_(LOG_DEBUG, domain, __FUNCTION__, args, ##__VA_ARGS__); \
  246. STMT_END
  247. #define log_info(domain, args,...) \
  248. log_fn_(LOG_INFO, domain, __FUNCTION__, args, ##__VA_ARGS__)
  249. #define log_notice(domain, args,...) \
  250. log_fn_(LOG_NOTICE, domain, __FUNCTION__, args, ##__VA_ARGS__)
  251. #define log_warn(domain, args,...) \
  252. log_fn_(LOG_WARN, domain, __FUNCTION__, args, ##__VA_ARGS__)
  253. #define log_err(domain, args,...) \
  254. log_fn_(LOG_ERR, domain, __FUNCTION__, args, ##__VA_ARGS__)
  255. /** Log a message at level <b>severity</b>, using a pretty-printed version
  256. * of the current function name. */
  257. #define log_fn(severity, domain, args,...) \
  258. log_fn_(severity, domain, __FUNCTION__, args, ##__VA_ARGS__)
  259. /** As log_fn, but use <b>ratelim</b> (an instance of ratelim_t) to control
  260. * the frequency at which messages can appear.
  261. */
  262. #define log_fn_ratelim(ratelim, severity, domain, args,...) \
  263. log_fn_ratelim_(ratelim, severity, domain, __FUNCTION__, \
  264. args, ##__VA_ARGS__)
  265. #endif /* defined(__GNUC__) && __GNUC__ <= 3 */
  266. /** This defines log levels that are linked in the Rust log module, rather
  267. * than re-defining these in both Rust and C.
  268. *
  269. * C_RUST_COUPLED src/rust/tor_log LogSeverity, LogDomain
  270. */
  271. extern const int LOG_WARN_;
  272. extern const int LOG_NOTICE_;
  273. extern const log_domain_mask_t LD_NET_;
  274. extern const log_domain_mask_t LD_GENERAL_;
  275. #ifdef LOG_PRIVATE
  276. MOCK_DECL(STATIC void, logv, (int severity, log_domain_mask_t domain,
  277. const char *funcname, const char *suffix, const char *format,
  278. va_list ap) CHECK_PRINTF(5,0));
  279. #endif
  280. #if defined(LOG_PRIVATE) || defined(TOR_UNIT_TESTS)
  281. /** Given a severity, yields an index into log_severity_list_t.masks to use
  282. * for that severity. */
  283. #define SEVERITY_MASK_IDX(sev) ((sev) - LOG_ERR)
  284. #endif
  285. #endif /* !defined(TOR_TORLOG_H) */