Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

wvlog.h

00001 /* -*- Mode: C++ -*-
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 #ifdef _WIN32
00022 typedef int pid_t;
00023 #endif
00024 
00025 class WvLog;
00026 
00027 // a WvLogRcv registers itself with WvLog and prints, captures,
00028 // or transmits log messages.
00029 class WvLogRcvBase
00030 {
00031     friend class WvLog;
00032 protected:
00033     const char *appname(const WvLog *log) const;
00034     virtual void log(const WvLog *source, int loglevel,
00035                      const char *_buf, size_t len) = 0;
00036 
00037 private:
00038     static void cleanup_on_fork(pid_t p);
00039     static void static_init();
00040 
00041 public:
00042     bool force_new_line;
00043     WvLogRcvBase();
00044     virtual ~WvLogRcvBase();
00045 };
00046 
00047 
00048 DeclareWvList(WvLogRcvBase);
00049 
00050 
00055 class WvLog : public WvStream
00056 {
00057     friend class WvLogRcvBase;
00058 public:
00059     enum LogLevel {
00060         Critical = 0,
00061         Error,
00062         Warning,
00063         Notice,
00064         Info,
00065         Debug, Debug1=Debug,
00066         Debug2,
00067         Debug3,
00068         Debug4,
00069         Debug5,
00070         
00071         NUM_LOGLEVELS
00072     };
00073     WvString app;
00074 
00075 protected:
00076     const WvLog *parent;
00077     LogLevel loglevel;
00078     static WvLogRcvBaseList receivers;
00079     static int num_receivers, num_logs;
00080     static WvLogRcvBase *default_receiver;
00081 
00082 public:
00083     WvLog(WvStringParm _app, LogLevel _loglevel = Info,
00084           const WvLog *par = NULL);
00085     WvLog(const WvLog &l);
00086     virtual ~WvLog();
00087 
00089     virtual bool isok() const;
00090     
00091     /* always writable */
00092     virtual bool pre_select(SelectInfo &si);
00093 
00098     WvLog &lvl(LogLevel _loglevel)
00099         { loglevel = _loglevel; return *this; }
00100     
00102     size_t operator() (LogLevel _loglevel, WvStringParm s)
00103     { 
00104         LogLevel l = loglevel; 
00105         size_t x = lvl(_loglevel).write(s);
00106         lvl(l);
00107         return x;
00108     }
00109     
00111     size_t operator() (LogLevel _loglevel, WVSTRING_FORMAT_DECL)
00112     { 
00113         LogLevel l = loglevel;
00114         size_t x = lvl(_loglevel).print(WVSTRING_FORMAT_CALL);
00115         lvl(l);
00116         return x;
00117     }
00118     
00123     size_t operator() (WvStringParm s)
00124         { return WvStream::operator()(s); }
00125     size_t operator() (WVSTRING_FORMAT_DECL)
00126         { return WvStream::operator()(WVSTRING_FORMAT_CALL); }
00127     
00133     WvLog split(LogLevel _loglevel) const
00134         { return WvLog(app, _loglevel, this); }
00135     
00140     virtual size_t uwrite(const void *buf, size_t len);
00141     
00143     void perror(WvStringParm s)
00144         { print("%s: %s\n", s, strerror(errno)); }
00145 };
00146 
00147 
00148 #endif // __WVLOG_H

Generated on Sun Jul 10 17:30:56 2005 for WvStreams by  doxygen 1.4.0