log.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. Copyright (c) 2003-2006 by Juliusz Chroboczek
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #define L_ERROR 0x1
  20. #define L_WARN 0x2
  21. #define L_INFO 0x4
  22. #define L_FORBIDDEN 0x8
  23. #define L_UNCACHEABLE 0x10
  24. #define L_SUPERSEDED 0x20
  25. #define L_VARY 0x40
  26. #define L_TUNNEL 0x80
  27. #define D_SERVER_CONN 0x100
  28. #define D_SERVER_REQ 0x200
  29. #define D_CLIENT_CONN 0x400
  30. #define D_CLIENT_REQ 0x800
  31. #define D_ATOM_REFCOUNT 0x1000
  32. #define D_REFCOUNT 0x2000
  33. #define D_LOCK 0x4000
  34. #define D_OBJECT 0x8000
  35. #define D_OBJECT_DATA 0x10000
  36. #define D_SERVER_OFFSET 0x20000
  37. #define D_CLIENT_DATA 0x40000
  38. #define D_DNS 0x80000
  39. #define D_CHILD 0x100000
  40. #define D_IO 0x200000
  41. #define LOGGING_DEFAULT (L_ERROR | L_WARN | L_INFO)
  42. #define LOGGING_MAX 0xFF
  43. extern int scrubLogs;
  44. void preinitLog(void);
  45. void initLog(void);
  46. void reopenLog(void);
  47. void flushLog(void);
  48. int loggingToStderr(void);
  49. void really_do_log(int type, const char *f, ...)
  50. ATTRIBUTE ((format (printf, 2, 3)));
  51. void really_do_log_v(int type, const char *f, va_list args)
  52. ATTRIBUTE ((format (printf, 2, 0)));
  53. void really_do_log_n(int type, const char *s, int n);
  54. void really_do_log_error(int type, int e, const char *f, ...)
  55. ATTRIBUTE ((format (printf, 3, 4)));
  56. void really_do_log_error_v(int type, int e, const char *f, va_list args)
  57. ATTRIBUTE ((format (printf, 3, 0)));
  58. const char *scrub(const char *message);
  59. #ifdef __GNUC__
  60. #define DO_BACKTRACE() \
  61. do { \
  62. int n; \
  63. void *buffer[10]; \
  64. n = backtrace(buffer, 5); \
  65. fflush(stderr); \
  66. backtrace_symbols_fd(buffer, n, 2); \
  67. } while(0)
  68. #else
  69. #define DO_BACKTRACE() /* */
  70. #endif
  71. /* These are macros because it's important that they should be
  72. optimised away. */
  73. #if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
  74. #define do_log(_type, ...) \
  75. do { \
  76. if((_type) & (LOGGING_MAX)) really_do_log((_type), __VA_ARGS__); \
  77. } while(0)
  78. #define do_log_error(_type, _e, ...) \
  79. do { \
  80. if((_type) & (LOGGING_MAX)) \
  81. really_do_log_error((_type), (_e), __VA_ARGS__); \
  82. } while(0)
  83. #elif defined(__GNUC__)
  84. #define do_log(_type, _args...) \
  85. do { \
  86. if((_type) & (LOGGING_MAX)) really_do_log((_type), _args); \
  87. } while(0)
  88. #define do_log_error(_type, _e, _args...) \
  89. do { \
  90. if((_type) & (LOGGING_MAX)) \
  91. really_do_log_error((_type), (_e), _args); \
  92. } while(0)
  93. #else
  94. /* No variadic macros -- let's hope inline works. */
  95. static inline void
  96. do_log(int type, const char *f, ...)
  97. {
  98. va_list args;
  99. va_start(args, f);
  100. if((type & (LOGGING_MAX)) != 0)
  101. really_do_log_v(type, f, args);
  102. va_end(args);
  103. }
  104. static inline void
  105. do_log_error(int type, int e, const char *f, ...)
  106. {
  107. va_list args;
  108. va_start(args, f);
  109. if((type & (LOGGING_MAX)) != 0)
  110. really_do_log_error_v(type, e, f, args);
  111. va_end(args);
  112. }
  113. #endif
  114. #define do_log_n(_type, _s, _n) \
  115. do { \
  116. if((_type) & (LOGGING_MAX)) really_do_log_n((_type), (_s), (_n)); \
  117. } while(0)