Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

wvlog.h

Go to the documentation of this file.
00001 /*
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
00004  * 
00005  * A generic data-logger class with support for multiple receivers.  If
00006  * no WvLogRcv objects have been created (see wvlogrcv.h) the default is
00007  * to log to stderr.
00008  * 
00009  * WvLog supports partial- and multiple-line log messages.  For example,
00010  *        log.print("test ");
00011  *        log.print("string\nfoo");
00012  * will print:
00013  *        appname(lvl): test string
00014  *        appname(lvl): foo
00015  */
00016 #ifndef __WVLOG_H
00017 #define __WVLOG_H
00018 
00019 #include "wvstream.h"
00020 #include <errno.h>
00021 
00022 class WvLog;
00023 
00024 // a WvLogRcv registers itself with WvLog and prints, captures,
00025 // or transmits log messages.
00026 class WvLogRcvBase
00027 {
00028     friend class WvLog;
00029 protected:
00030     const char *appname(const WvLog *log) const;
00031     virtual void log(const WvLog *source, int loglevel,
00032                      const char *_buf, size_t len) = 0;
00033 public:
00034     WvLogRcvBase();
00035     virtual ~WvLogRcvBase();
00036 };
00037 
00038 
00039 DeclareWvList(WvLogRcvBase);
00040 
00041 
00046 class WvLog : public WvStream
00047 {
00048     friend class WvLogRcvBase;
00049 public:
00050     enum LogLevel {
00051         Critical = 0,
00052         Error,
00053         Warning,
00054         Notice,
00055         Info,
00056         Debug, Debug1=Debug,
00057         Debug2,
00058         Debug3,
00059         Debug4,
00060         Debug5,
00061         
00062         NUM_LOGLEVELS
00063     };
00064     WvString app;
00065 
00066 protected:
00067     const WvLog *parent;
00068     LogLevel loglevel;
00069     static WvLogRcvBaseList receivers;
00070     static int num_receivers, num_logs;
00071     static WvLogRcvBase *default_receiver;
00072 
00073 public:
00074     WvLog(const WvString &_app, LogLevel _loglevel = Info,
00075           const WvLog *par = NULL);
00076     WvLog(const WvLog &l);
00077     virtual ~WvLog();
00078 
00082     virtual bool isok() const;
00083     
00088     WvLog &lvl(LogLevel _loglevel)
00089         { loglevel = _loglevel; return *this; }
00090     
00094     size_t operator() (LogLevel _loglevel, const WvString &s)
00095     { 
00096         LogLevel l = loglevel; 
00097         size_t x = lvl(_loglevel).write(s);
00098         lvl(l);
00099         return x;
00100     }
00101     
00105     inline size_t operator() (LogLevel _loglevel, WVSTRING_FORMAT_DECL)
00106     { 
00107         LogLevel l = loglevel;
00108         size_t x = lvl(_loglevel).print(WVSTRING_FORMAT_CALL);
00109         lvl(l);
00110         return x;
00111     }
00112     
00117     size_t operator() (const WvString &s)
00118         { return WvStream::operator()(s); }
00119     inline size_t operator() (WVSTRING_FORMAT_DECL)
00120         { return WvStream::operator()(WVSTRING_FORMAT_CALL); }
00121     
00127     WvLog split(LogLevel _loglevel) const
00128         { return WvLog(app, _loglevel, this); }
00129     
00134     virtual size_t uwrite(const void *buf, size_t len);
00135     
00139     void perror(const WvString &s)
00140         { print("%s: %s\n", s, strerror(errno)); }
00141 };
00142 
00143 
00144 #endif // __WVLOG_H

Generated on Sun Aug 25 12:42:15 2002 for WvStreams by doxygen1.2.15