log.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. /* Outputs a message to stdout */
  63. void _log(int severity, const char *format, ...) CHECK_PRINTF(2,3);
  64. #ifdef __GNUC__
  65. void _log_fn(int severity, const char *funcname, const char *format, ...)
  66. CHECK_PRINTF(3,4);
  67. /** Log a message at level <b>severity</b>, using a pretty-printed version
  68. * of the current function name. */
  69. #define log_fn(severity, args...) \
  70. _log_fn(severity, __PRETTY_FUNCTION__, args)
  71. #elif defined(_MSC_VER) && _MSC_VER < 1300
  72. /* MSVC 6 and earlier don't have __FUNCTION__, or even __LINE__. */
  73. #define log_fn _log
  74. #else
  75. /* We don't have GCC's varargs macros, so use a global variable to pass the
  76. * function name to log_fn */
  77. extern const char *_log_fn_function_name;
  78. void _log_fn(int severity, const char *format, ...);
  79. /* We abuse the comma operator here, since we can't use the standard
  80. * do {...} while (0) trick to wrap this macro, since the macro can't take
  81. * arguments. */
  82. #define log_fn (_log_fn_function_name=__FUNCTION__),_log_fn
  83. #endif
  84. #define log _log /* hack it so we don't conflict with log() as much */
  85. # define __LOG_H
  86. #endif