WvStreams
wvlogrcv.h
00001 /* -*- Mode: C++ -*-
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
00004  *
00005  * Enhanced "Log Receiver" classes for WvLog.
00006  * 
00007  * WvLogRcv-derived classes support automatic formatting of log lines with
00008  * a prefix, removing control characters, and so on.
00009  * 
00010  * WvLogRcv supports partial- and multiple-line log messages.  For example,
00011  *        log.print("test ");
00012  *        log.print("string\nfoo");
00013  * will print:
00014  *        appname(lvl): test string
00015  *        appname(lvl): foo
00016  */
00017 #ifndef __WVLOGRCV_H
00018 #define __WVLOGRCV_H
00019 
00020 #include "wvlog.h"
00021 #include "wvfdstream.h"
00022 #include "wvscatterhash.h"
00023 
00028 class WvLogRcv : public WvLogRcvBase
00029 {
00030 protected:  
00031     WvString last_source;
00032     WvLog::LogLevel max_level, last_level;
00033     time_t last_time;
00034     bool at_newline;
00035     WvString prefix;
00036     size_t prelen;
00037     
00038     class Src_Lvl
00039     {
00040     public:
00041         WvString src;
00042         WvLog::LogLevel lvl;
00043         Src_Lvl(WvString _src, int _lvl) : src(_src),
00044         lvl((WvLog::LogLevel)_lvl) {};
00045     };
00046     
00047     DeclareWvScatterDict(Src_Lvl, WvString, src);
00048     
00049     Src_LvlDict custom_levels;
00050     
00052     virtual void _make_prefix(time_t now);
00053     
00055     virtual void _begin_line();
00056     
00058     virtual void _end_line();
00059     
00065     virtual void _mid_line(const char *str, size_t len) = 0;
00066     
00067 private:
00068     void begin_line()
00069         { if (!at_newline) return; _begin_line(); at_newline = false; }
00070     void mid_line(const char *str, size_t len)
00071         { _mid_line(str, len); 
00072             if (len>0 && str[len-1] == '\n') at_newline = true; }
00073     
00074 public:
00075     virtual void log(WvStringParm source, int loglevel,
00076                      const char *_buf, size_t len);
00077     
00078     static const char *loglevels[WvLog::NUM_LOGLEVELS];
00079     
00080     WvLogRcv(WvLog::LogLevel _max_level = WvLog::NUM_LOGLEVELS);
00081     virtual ~WvLogRcv();
00082     
00083     void end_line()
00084         { if (at_newline) return;
00085             _mid_line("\n", 1); _end_line(); at_newline = true; };
00086     
00087     WvLog::LogLevel level() const
00088         { return max_level; }
00089     void level(WvLog::LogLevel lvl)
00090         { max_level = lvl; }
00091     
00092     /*
00093      * Allows you to override debug levels for specific sources
00094      *  example: mysql=9,Service Manager=9
00095      *  will get all the debug output from all the sources that
00096      *  contain (case insensitively) "mysql" or "Service Manager"
00097      *  in the source name regardless of the current debug level.
00098      */
00099     bool set_custom_levels(WvString descr);
00100 };
00101 
00102 
00107 class WvLogConsole : public WvFDStream, public WvLogRcv
00108 {
00109     public:
00110         WvLogConsole(int _fd,
00111                 WvLog::LogLevel _max_level = WvLog::NUM_LOGLEVELS);
00112         virtual ~WvLogConsole();
00113     protected:
00114         virtual void _mid_line(const char *str, size_t len);
00115 };
00116 
00117 #endif // __WVLOGRCV_H