log.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /* $Id$ */
  7. /**
  8. * \file log.h
  9. *
  10. * \brief Headers for log.c
  11. **/
  12. #ifndef __LOG_H
  13. #define LOG_H_ID "$Id$"
  14. #include "compat.h"
  15. #ifdef HAVE_SYSLOG_H
  16. #include <syslog.h>
  17. #define LOG_WARN LOG_WARNING
  18. #if LOG_DEBUG < LOG_ERR
  19. #error "Your syslog.h thinks high numbers are more important. " \
  20. "We aren't prepared to deal with that."
  21. #endif
  22. #else
  23. /* Note: Syslog's logging code refers to priorities, with 0 being the most
  24. * important. Thus, all our comparisons needed to be reversed when we added
  25. * syslog support.
  26. *
  27. * The upshot of this is that comments about log levels may be messed up: for
  28. * "maximum severity" read "most severe" and "numerically *lowest* severity".
  29. */
  30. /** Debug-level severity: for hyper-verbose messages of no interest to
  31. * anybody but developers. */
  32. #define LOG_DEBUG 7
  33. /** Info-level severity: for messages that appear frequently during normal
  34. * operation. */
  35. #define LOG_INFO 6
  36. /** Notice-level severity: for messages that appear infrequently
  37. * during normal operation; that the user will probably care about;
  38. * and that are not errors.
  39. */
  40. #define LOG_NOTICE 5
  41. /** Warn-level severity: for messages that only appear when something has gone
  42. * wrong. */
  43. #define LOG_WARN 4
  44. /** Error-level severity: for messages that only appear when something has gone
  45. * very wrong, and the Tor process can no longer proceed. */
  46. #define LOG_ERR 3
  47. #endif
  48. /* Logging domains */
  49. /** Catch-all for miscellaneous events and fatal errors. */
  50. #define LD_GENERAL (1u<<0)
  51. /** The cryptography subsytem. */
  52. #define LD_CRYPTO (1u<<1)
  53. /** Networking. */
  54. #define LD_NET (1u<<2)
  55. /** Parsing and acting on our configuration. */
  56. #define LD_CONFIG (1u<<3)
  57. /** Reading and writing from the filesystem. */
  58. #define LD_FS (1u<<4)
  59. /** Other servers' (non)compliance with the Tor protocol. */
  60. #define LD_PROTOCOL (1u<<5)
  61. /** Memory management. */
  62. #define LD_MM (1u<<6)
  63. /** HTTP implementation. */
  64. #define LD_HTTP (1u<<7)
  65. /** Application (socks) requests. */
  66. #define LD_APP (1u<<8)
  67. /** Communication via the controller protocol. */
  68. #define LD_CONTROL (1u<<9)
  69. /** Building, using, and managing circuits. */
  70. #define LD_CIRC (1u<<10)
  71. /** Hidden services. */
  72. #define LD_REND (1u<<11)
  73. /** Internal errors in this Tor process. */
  74. #define LD_BUG (1u<<12)
  75. /** Learning and using information about Tor servers. */
  76. #define LD_DIR (1u<<13)
  77. /** Learning and using information about Tor servers. */
  78. #define LD_DIRSERV (1u<<14)
  79. /** Onion routing protocol. */
  80. #define LD_OR (1u<<15)
  81. /** Generic edge-connection functionality. */
  82. #define LD_EDGE (1u<<16)
  83. #define LD_EXIT LD_EDGE
  84. /** Bandwidth accounting. */
  85. #define LD_ACCT (1u<<17)
  86. /** Callback type used for add_callback_log. */
  87. typedef void (*log_callback)(int severity, uint32_t domain, const char *msg);
  88. int parse_log_level(const char *level);
  89. const char *log_level_to_string(int level);
  90. void add_stream_log(int severityMin, int severityMax, const char *name,
  91. FILE *stream);
  92. int add_file_log(int severityMin, int severityMax, const char *filename);
  93. #ifdef HAVE_SYSLOG_H
  94. int add_syslog_log(int loglevelMin, int loglevelMax);
  95. #endif
  96. int add_callback_log(int loglevelMin, int loglevelMax, log_callback cb);
  97. int get_min_log_level(void);
  98. void switch_logs_debug(void);
  99. void logs_free_all(void);
  100. void add_temp_log(void);
  101. void close_temp_logs(void);
  102. void rollback_log_changes(void);
  103. void mark_logs_temp(void);
  104. void configure_libevent_logging(void);
  105. void suppress_libevent_log_msg(const char *msg);
  106. void change_callback_log_severity(int loglevelMin, int loglevelMax,
  107. log_callback cb);
  108. void log_set_application_name(const char *name);
  109. /* Outputs a message to stdout */
  110. void _log(int severity, uint32_t domain, const char *format, ...)
  111. CHECK_PRINTF(3,4);
  112. #define log _log /* hack it so we don't conflict with log() as much */
  113. #ifdef __GNUC__
  114. extern int _log_global_min_severity;
  115. void _log_fn(int severity, uint32_t domain,
  116. const char *funcname, const char *format, ...)
  117. CHECK_PRINTF(4,5);
  118. /** Log a message at level <b>severity</b>, using a pretty-printed version
  119. * of the current function name. */
  120. #define log_fn(severity, domain, args...) \
  121. _log_fn(severity, domain, __PRETTY_FUNCTION__, args)
  122. #define log_debug(domain, args...) \
  123. STMT_BEGIN \
  124. if (PREDICT_UNLIKELY(_log_global_min_severity == LOG_DEBUG)) \
  125. _log_fn(LOG_DEBUG, domain, __PRETTY_FUNCTION__, args); \
  126. STMT_END
  127. #define log_info(domain, args...) \
  128. _log_fn(LOG_INFO, domain, __PRETTY_FUNCTION__, args)
  129. #define log_notice(domain, args...) \
  130. _log_fn(LOG_NOTICE, domain, __PRETTY_FUNCTION__, args)
  131. #define log_warn(domain, args...) \
  132. _log_fn(LOG_WARN, domain, __PRETTY_FUNCTION__, args)
  133. #define log_err(domain, args...) \
  134. _log_fn(LOG_ERR, domain, __PRETTY_FUNCTION__, args)
  135. #else /* ! defined(__GNUC__) */
  136. void _log_fn(int severity, uint32_t domain, const char *format, ...);
  137. void _log_debug(uint32_t domain, const char *format, ...);
  138. void _log_info(uint32_t domain, const char *format, ...);
  139. void _log_notice(uint32_t domain, const char *format, ...);
  140. void _log_warn(uint32_t domain, const char *format, ...);
  141. void _log_err(uint32_t domain, const char *format, ...);
  142. #if defined(_MSC_VER) && _MSC_VER < 1300
  143. /* MSVC 6 and earlier don't have __func__, or even __LINE__. */
  144. #define log_fn _log_fn
  145. #define log_debug _log_debug
  146. #define log_info _log_info
  147. #define log_notice _log_notice
  148. #define log_warn _log_warn
  149. #define log_err _log_err
  150. #else
  151. /* We don't have GCC's varargs macros, so use a global variable to pass the
  152. * function name to log_fn */
  153. extern const char *_log_fn_function_name;
  154. /* We abuse the comma operator here, since we can't use the standard
  155. * do {...} while (0) trick to wrap this macro, since the macro can't take
  156. * arguments. */
  157. #define log_fn (_log_fn_function_name=__func__),_log_fn
  158. #define log_debug (_log_fn_function_name=__func__),_log_debug
  159. #define log_info (_log_fn_function_name=__func__),_log_info
  160. #define log_notice (_log_fn_function_name=__func__),_log_notice
  161. #define log_warn (_log_fn_function_name=__func__),_log_warn
  162. #define log_err (_log_fn_function_name=__func__),_log_err
  163. #endif
  164. #endif /* !GNUC */
  165. # define __LOG_H
  166. #endif