/Log.h

https://github.com/privacore/open-source-search-engine · C Header · 175 lines · 65 code · 42 blank · 68 comment · 6 complexity · 43feb6ceab91cdcab9892d6a4de1b95b MD5 · raw file

  1. // Matt Wells, copyright Feb 2001
  2. // . a great way to record errors encountered during
  3. // . we store the errorMsg, it's length, the type of message and time.
  4. // . when our buf gets full we dump half the messages to the log file (if any)
  5. // . netLogdb can send error msgs for you with it's sendError cmd
  6. // . sendError ( UdpSlot *slot , char *errFormat , ...); (also logs it)
  7. #ifndef GB_LOG_H
  8. #define GB_LOG_H
  9. #include <stdarg.h>
  10. #include <stdint.h>
  11. // THE TYPES OF LOG MESSAGES
  12. // logs information pertaining to more complicated procedures, like
  13. // the merging and dumping of data for the "db" component, or what urls are
  14. // being spidered for the "build" component.
  15. #define LOG_INFO 0x0001
  16. // the default log message type. also logs slow performance.
  17. #define LOG_WARN 0x0004
  18. // the default log message type. also logs slow performance.
  19. #define LOG_ERROR 0x0008
  20. // programmer error. sanity check. always on.
  21. #define LOG_LOGIC 0x0010
  22. // Reminders to fix the code. generally disabled.
  23. #define LOG_REMIND 0x0020
  24. // for debugging. generally disabled.
  25. #define LOG_DEBUG 0x0040
  26. // for tracing. generally disabled. Enabled for specific code
  27. // sections through config UI
  28. #define LOG_TRACE 0x0080
  29. // times various subroutines for debugging performance.
  30. #define LOG_TIMING 0x0100
  31. // initialization (and shutdown) information. also print routines. always on.
  32. #define LOG_INIT 0x0400
  33. // if a url or link gets truncated, uses this in the "build" context. (Url.cpp
  34. // and Links.cpp)
  35. // also used if a document not added due to quota breech. (Msg16.cpp)
  36. // also used if too many nested tags to parse doc correctly (Xml.cpp)
  37. // in the "query" context for serps too big to be cached. (Msg17.cpp)
  38. #define LOG_LIMIT 0x2000
  39. // It is convenient to divide everything into components and allow the admin
  40. // to toggle logging for various aspects, such as performance or timing
  41. // messages, of these components:
  42. // addurls related to adding urls
  43. // admin related to administrative things, sync file, collections
  44. // build related to indexing (high level)
  45. // conf configuration issues
  46. // disk disk reads and writes
  47. // dns dns networking
  48. // http http networking
  49. // loop
  50. // net network later: multicast pingserver. sits atop udpserver.
  51. // query related to querying (high level)
  52. // rdb generic rdb things
  53. // spcache related to determining what urls to spider next
  54. // speller query spell checking
  55. // thread calling threads
  56. // udp udp networking
  57. // example log:
  58. //456456454 0 INIT Gigablast Version 1.234
  59. //454544444 0 INIT thread Allocated 435333 bytes for thread stacks.
  60. //123456789 0 WARN mem Failed to alloc 360000 bytes.
  61. //123456789 0 WARN query Failed to intersect lists. Out of memory.
  62. //123456789 0 WARN query Too many words. Query truncated.
  63. //234234324 0 REQST http 1.2.3.4 GET /index.html User-Agent
  64. //234234324 0 REPLY http 1.2.3.4 sent 34536 bytes
  65. //345989494 0 REQST build GET http://hohum.com/foobar.html
  66. //345989494 0 INFO build http://hohum.com/foobar.html ip=4.5.6.7 : Success
  67. //324234324 0 DEBUG build Skipping xxx.com, would hammer IP.
  68. #define MAX_LOG_MSGS 1024 // in memory
  69. // may also syslog and fprintf the msg.
  70. // ALWAYS returns FALSE (i.e. 0)!!!! so you can say return log.log(...)
  71. void log ( int32_t type , const char *formatString , ... )
  72. __attribute__ ((format(printf, 2, 3)));
  73. // this defaults to type of LOG_WARN
  74. void log ( const char *formatString , ... )
  75. __attribute__ ((format(printf, 1, 2)));
  76. // force it to be logged, even if off on log controls panel
  77. void logf ( int32_t type , const char *formatString , ... )
  78. __attribute__ ((format(printf, 2, 3)));
  79. void loghex( int32_t type, void const *data, const unsigned int len, const char *formatString , ...)
  80. __attribute__ ((format(printf, 4, 5)));
  81. #define logError(msg, ...) \
  82. logf(LOG_ERROR, "%s:%s:%d: " msg, __FILE__, __func__, __LINE__, ##__VA_ARGS__)
  83. #define logDebug(condition, ...) \
  84. do { \
  85. if (condition) { \
  86. logf(LOG_DEBUG, __VA_ARGS__); \
  87. } \
  88. } while (0)
  89. #define logTrace(condition, msg, ...) \
  90. do { \
  91. if (condition) { \
  92. logf(LOG_TRACE, "%s:%s:%d: " msg, __FILE__, __func__, __LINE__, ##__VA_ARGS__); \
  93. } \
  94. } while (0)
  95. #define logHexTrace(condition, data, len, msg, ...) \
  96. do { \
  97. if (condition) { \
  98. loghex(LOG_TRACE, data, len, "%s:%s:%d: " msg, __FILE__, __func__, __LINE__, ##__VA_ARGS__); \
  99. } \
  100. } while (0)
  101. class Log {
  102. public:
  103. // just initialize with no file
  104. Log();
  105. ~Log();
  106. // returns true if opened log file successfully, otherwise false
  107. bool init(const char *filename);
  108. bool registerLogRotation();
  109. // . log this msg
  110. // . "msg" must be NULL terminated
  111. // . now is the time of day in milliseconds since the epoch
  112. // . if "now" is 0 we insert the timestamp for you
  113. // . if "asterisk" is true we print an asterisk to indicate that
  114. // the msg was actually logged earlier but only printed now because
  115. // we were in a signal handler at the time
  116. bool logR(int64_t now, int32_t type, const char *msg, bool forced = false);
  117. // returns false if msg should not be logged, true if it should
  118. bool shouldLog(int32_t type, const char *msg);
  119. void reset();
  120. // save before exiting
  121. void close() {}
  122. bool m_disabled;
  123. bool m_logTimestamps;
  124. bool m_logReadableTimestamps;
  125. bool m_logPrefix;
  126. private:
  127. bool makeNewLogFile();
  128. static void rotateLog(int fd, void *state);
  129. const char *m_filename;
  130. int m_fd;
  131. };
  132. extern class Log g_log;
  133. #endif // GB_LOG_H