PageRenderTime 64ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/Log.h

https://github.com/gigablast/open-source-search-engine
C Header | 176 lines | 62 code | 37 blank | 77 comment | 0 complexity | 6aa4cc234b1f6d673087a49f3bea9f36 MD5 | raw file
Possible License(s): Apache-2.0
  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 _MYLOG_H_
  8. #define _MYLOG_H_
  9. // THE TYPES OF LOG MESSAGES
  10. // logs information pertaining to more complicated procedures, like
  11. // the merging and dumping of data for the "db" component, or what urls are
  12. // being spidered for the "build" component.
  13. #define LOG_INFO 0x0001
  14. // the default log message type. also logs slow performance.
  15. #define LOG_WARN 0x0004
  16. // programmer error. sanity check. always on.
  17. #define LOG_LOGIC 0x0010
  18. // Reminders to fix the code. generally disabled.
  19. #define LOG_REMIND 0x0020
  20. // for debugging. generally disabled.
  21. #define LOG_DEBUG 0x0040
  22. // times various subroutines for debugging performance.
  23. #define LOG_TIMING 0x0100
  24. // initialization (and shutdown) information. also print routines. always on.
  25. #define LOG_INIT 0x0400
  26. // if a url or link gets truncated, uses this in the "build" context. (Url.cpp
  27. // and Links.cpp)
  28. // also used if a document not added due to quota breech. (Msg16.cpp)
  29. // also used if too many nested tags to parse doc correctly (Xml.cpp)
  30. // in the "query" context for serps too big to be cached. (Msg17.cpp)
  31. #define LOG_LIMIT 0x2000
  32. // It is convenient to divide everything into components and allow the admin
  33. // to toggle logging for various aspects, such as performance or timing
  34. // messages, of these components:
  35. // addurls related to adding urls
  36. // admin related to administrative things, sync file, collections
  37. // build related to indexing (high level)
  38. // conf configuration issues
  39. // disk disk reads and writes
  40. // dns dns networking
  41. // http http networking
  42. // loop
  43. // net network later: multicast pingserver. sits atop udpserver.
  44. // query related to querying (high level)
  45. // rdb generic rdb things
  46. // spcache related to determining what urls to spider next
  47. // speller query spell checking
  48. // thread calling threads
  49. // topics related topics
  50. // udp udp networking
  51. // example log:
  52. //456456454 0 INIT Gigablast Version 1.234
  53. //454544444 0 INIT thread Allocated 435333 bytes for thread stacks.
  54. //123456789 0 WARN mem Failed to alloc 360000 bytes.
  55. //123456789 0 WARN query Failed to intersect lists. Out of memory.
  56. //123456789 0 WARN query Too many words. Query truncated.
  57. //234234324 0 REQST http 1.2.3.4 GET /index.html User-Agent
  58. //234234324 0 REPLY http 1.2.3.4 sent 34536 bytes
  59. //345989494 0 REQST build GET http://hohum.com/foobar.html
  60. //345989494 0 INFO build http://hohum.com/foobar.html ip=4.5.6.7 : Success
  61. //324234324 0 DEBUG build Skipping xxx.com, would hammer IP.
  62. #define MAX_LOG_MSGS 1024 // in memory
  63. // this is for printing out how a page is parsed by PageParser.cpp
  64. /* extern char *g_pbuf ; */
  65. /* extern char *g_pbufPtr ; */
  66. /* extern char *g_pterms ; */
  67. /* extern char *g_ptermPtr ; */
  68. /* extern char *g_pend; */
  69. extern char *g_dbuf;
  70. extern int32_t g_dbufSize;
  71. #ifdef _CHECK_FORMAT_STRING_
  72. bool log ( int32_t type , char *formatString , ... )
  73. __attribute__ ((format(printf, 2, 3)));
  74. bool log ( char *formatString , ... )
  75. __attribute__ ((format(printf, 1, 2)));
  76. bool logf ( int32_t type , char *formatString , ... )
  77. __attribute__ ((format(printf, 2, 3)));
  78. #else
  79. // may also syslog and fprintf the msg.
  80. // ALWAYS returns FALSE (i.e. 0)!!!! so you can say return log.log(...)
  81. bool log ( int32_t type , char *formatString , ... ) ;
  82. // this defaults to type of LOG_WARN
  83. bool log ( char *formatString , ... ) ;
  84. // force it to be logged, even if off on log controls panel
  85. bool logf ( int32_t type , char *formatString , ... ) ;
  86. #endif
  87. class Log {
  88. public:
  89. // returns true if opened log file successfully, otherwise false
  90. bool init ( char *filename );
  91. // . log this msg
  92. // . "msg" must be NULL terminated
  93. // . now is the time of day in milliseconds since the epoch
  94. // . if "now" is 0 we insert the timestamp for you
  95. // . if "asterisk" is true we print an asterisk to indicate that
  96. // the msg was actually logged earlier but only printed now because
  97. // we were in a signal handler at the time
  98. bool logR ( int64_t now, int32_t type, char *msg, bool asterisk ,
  99. bool forced = false );
  100. // returns false if msg should not be logged, true if it should
  101. bool shouldLog ( int32_t type , char *msg ) ;
  102. // just initialize with no file
  103. Log () ;
  104. ~Log () ;
  105. void reset ( );
  106. void setPid();
  107. // save before exiting
  108. void close () { dumpLog(); };
  109. // do we need to print out the msgs we've saved while in sig handler?
  110. bool needsPrinting ( ) { return m_needsPrinting; };
  111. // print the stuff that needs printing
  112. void printBuf ( );
  113. // this is only called when in a signal handler
  114. bool logLater ( int64_t now , int32_t type , char *formatString ,
  115. va_list ap );
  116. bool m_disabled;
  117. bool m_logTimestamps;
  118. char *getFilename() { return m_filename; };
  119. private:
  120. bool dumpLog ( ); // make room for the new ones
  121. char *m_filename;
  122. int m_fd;
  123. char *m_hostname;
  124. int m_port;
  125. int64_t m_logFileSize;
  126. bool makeNewLogFile ( );
  127. char *m_errorMsg [ MAX_LOG_MSGS ];
  128. int16_t m_errorMsgLen [ MAX_LOG_MSGS ];
  129. int64_t m_errorTime [ MAX_LOG_MSGS ];
  130. unsigned char m_errorType [ MAX_LOG_MSGS ];
  131. int m_numErrors;
  132. // m_erroMsg's point into this buf.
  133. char m_buf [ 1024 * 32];
  134. int m_bufPtr;
  135. // do we need to print out the msgs we've saved while in sig handler?
  136. bool m_needsPrinting;
  137. };
  138. extern class Log g_log;
  139. #endif