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