log.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. /**
  5. * \file log.h
  6. *
  7. * \brief Headers for logging functions.
  8. **/
  9. #ifndef __LOG_H
  10. #define LOG_H_ID "$Id$"
  11. #include "../common/compat.h"
  12. #ifdef HAVE_SYSLOG_H
  13. #include <syslog.h>
  14. #define LOG_WARN LOG_WARNING
  15. #if LOG_DEBUG < LOG_ERR
  16. #error "Your syslog.h thinks high numbers are more important. We aren't prepared to deal with that."
  17. #endif
  18. #else
  19. /* XXXX Note: The code was originally written to refer to severities,
  20. * with 0 being the least severe; while syslog's logging code refers to
  21. * priorities, with 0 being the most important. Thus, all our comparisons
  22. * needed to be reversed when we added syslog support.
  23. *
  24. * The upshot of this is that comments about log levels may be messed
  25. * up: for "maximum severity" read "most severe" and "numerically
  26. * *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. typedef void (*log_callback)(int severity, const char *msg);
  47. int parse_log_level(const char *level);
  48. const char *log_level_to_string(int level);
  49. void add_stream_log(int severityMin, int severityMax, const char *name, FILE *stream);
  50. int add_file_log(int severityMin, int severityMax, const char *filename);
  51. #ifdef HAVE_SYSLOG_H
  52. int add_syslog_log(int loglevelMin, int loglevelMax);
  53. #endif
  54. int add_callback_log(int loglevelMin, int loglevelMax, log_callback cb);
  55. int get_min_log_level(void);
  56. void switch_logs_debug(void);
  57. void close_logs(void);
  58. void reset_logs(void);
  59. void add_temp_log(void);
  60. void close_temp_logs(void);
  61. void mark_logs_temp(void);
  62. void configure_libevent_logging(void);
  63. void suppress_libevent_log_msg(const char *msg);
  64. /* Outputs a message to stdout */
  65. void _log(int severity, const char *format, ...) CHECK_PRINTF(2,3);
  66. #ifdef __GNUC__
  67. void _log_fn(int severity, const char *funcname, const char *format, ...)
  68. CHECK_PRINTF(3,4);
  69. /** Log a message at level <b>severity</b>, using a pretty-printed version
  70. * of the current function name. */
  71. #define log_fn(severity, args...) \
  72. _log_fn(severity, __PRETTY_FUNCTION__, args)
  73. #elif defined(_MSC_VER) && _MSC_VER < 1300
  74. /* MSVC 6 and earlier don't have __FUNCTION__, or even __LINE__. */
  75. #define log_fn _log
  76. #else
  77. /* We don't have GCC's varargs macros, so use a global variable to pass the
  78. * function name to log_fn */
  79. extern const char *_log_fn_function_name;
  80. void _log_fn(int severity, const char *format, ...);
  81. /* We abuse the comma operator here, since we can't use the standard
  82. * do {...} while (0) trick to wrap this macro, since the macro can't take
  83. * arguments. */
  84. #define log_fn (_log_fn_function_name=__FUNCTION__),_log_fn
  85. #endif
  86. #define log _log /* hack it so we don't conflict with log() as much */
  87. # define __LOG_H
  88. #endif