log.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. /**
  11. * \file log.h
  12. * \brief Headers for log.c
  13. */
  14. #include "../common/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. 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. typedef void (*log_callback)(int severity, const char *msg);
  50. int parse_log_level(const char *level);
  51. const char *log_level_to_string(int level);
  52. void add_stream_log(int severityMin, int severityMax, const char *name, FILE *stream);
  53. int add_file_log(int severityMin, int severityMax, const char *filename);
  54. #ifdef HAVE_SYSLOG_H
  55. int add_syslog_log(int loglevelMin, int loglevelMax);
  56. #endif
  57. int add_callback_log(int loglevelMin, int loglevelMax, log_callback cb);
  58. int get_min_log_level(void);
  59. void switch_logs_debug(void);
  60. void close_logs(void);
  61. void reset_logs(void);
  62. void add_temp_log(void);
  63. void close_temp_logs(void);
  64. void mark_logs_temp(void);
  65. /* Outputs a message to stdout */
  66. void _log(int severity, const char *format, ...) CHECK_PRINTF(2,3);
  67. #ifdef __GNUC__
  68. void _log_fn(int severity, const char *funcname, const char *format, ...)
  69. CHECK_PRINTF(3,4);
  70. /** Log a message at level <b>severity</b>, using a pretty-printed version
  71. * of the current function name. */
  72. #define log_fn(severity, args...) \
  73. _log_fn(severity, __PRETTY_FUNCTION__, args)
  74. #elif defined(_MSC_VER) && _MSC_VER < 1300
  75. /* MSVC 6 and earlier don't have __FUNCTION__, or even __LINE__. */
  76. #define log_fn _log
  77. #else
  78. /* We don't have GCC's varargs macros, so use a global variable to pass the
  79. * function name to log_fn */
  80. extern const char *_log_fn_function_name;
  81. void _log_fn(int severity, const char *format, ...);
  82. /* We abuse the comma operator here, since we can't use the standard
  83. * do {...} while(0) trick to wrap this macro, since the macro can't take
  84. * arguments. */
  85. #define log_fn (_log_fn_function_name=__FUNCTION__),_log_fn
  86. #endif
  87. #define log _log /* hack it so we don't conflict with log() as much */
  88. # define __LOG_H
  89. #endif