log.h 6.2 KB

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