Fix several warnings that appear in gcc 4.3.2.
[wvstreams.git] / include / wvlog.h
blobd6c47f063885ec862541db83cffc58aa16dcc415
1 /* -*- Mode: C++ -*-
2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
5 * A generic data-logger class with support for multiple receivers. If
6 * no WvLogRcv objects have been created (see wvlogrcv.h) the default is
7 * to log to stderr.
8 *
9 * WvLog supports partial- and multiple-line log messages. For example,
10 * log.print("test ");
11 * log.print("string\nfoo");
12 * will print:
13 * appname(lvl): test string
14 * appname(lvl): foo
16 #ifndef __WVLOG_H
17 #define __WVLOG_H
19 #include "wvstream.h"
20 #include <errno.h>
21 #ifdef _WIN32
22 typedef int pid_t;
23 #endif
25 class WvLog;
27 // a WvLogRcv registers itself with WvLog and prints, captures,
28 // or transmits log messages.
29 class WvLogRcvBase
31 friend class WvLog;
32 protected:
33 const char *appname(WvStringParm log) const;
34 virtual void log(WvStringParm source, int loglevel,
35 const char *_buf, size_t len) = 0;
37 private:
38 static void cleanup_on_fork(pid_t p);
39 static void static_init();
41 public:
42 bool force_new_line;
43 WvLogRcvBase();
44 virtual ~WvLogRcvBase();
48 DeclareWvList(WvLogRcvBase);
50 typedef wv::function<WvString(WvStringParm)> WvLogFilter;
52 /**
53 * A WvLog stream accepts log messages from applications and forwards them
54 * to all registered WvLogRcv's.
56 class WvLog : public WvStream
58 friend class WvLogRcvBase;
59 public:
60 enum LogLevel {
61 Critical = 0,
62 Error,
63 Warning,
64 Notice,
65 Info,
66 Debug, Debug1=Debug,
67 Debug2,
68 Debug3,
69 Debug4,
70 Debug5,
72 NUM_LOGLEVELS
74 WvString app;
76 protected:
77 LogLevel loglevel;
78 static WvLogRcvBaseList *receivers;
79 static int num_receivers, num_logs;
80 static WvLogRcvBase *default_receiver;
81 WvLogFilter* filter;
83 public:
84 WvLog(WvStringParm _app, LogLevel _loglevel = Info,
85 WvLogFilter* filter = 0);
86 WvLog(const WvLog &l);
87 virtual ~WvLog();
89 /** fd==-1, but this stream is always ok */
90 virtual bool isok() const;
92 /* always writable */
93 virtual void pre_select(SelectInfo &si);
94 virtual bool post_select(SelectInfo &si);
96 /**
97 * change the loglevel. This returns the object again, so you can
98 * make convenient statements like log.lvl(WvLog::Warning).print(...)
100 WvLog &lvl(LogLevel _loglevel)
101 { loglevel = _loglevel; return *this; }
103 /** change the loglevel and then print a message. */
104 size_t operator() (LogLevel _loglevel, WvStringParm s)
106 LogLevel l = loglevel;
107 size_t x = lvl(_loglevel).write(filter ? (*filter)(s) : s);
108 lvl(l);
109 return x;
112 /** change the loglevel and then print a formatted message */
113 size_t operator() (LogLevel _loglevel, WVSTRING_FORMAT_DECL)
115 LogLevel l = loglevel;
116 size_t x;
117 if (filter)
118 x = lvl(_loglevel).print((*filter)(WvString(WVSTRING_FORMAT_CALL)));
119 else
120 x = lvl(_loglevel).print(WVSTRING_FORMAT_CALL);
121 lvl(l);
122 return x;
126 * although these appear in WvStream, they need to be re-listed here
127 * since the above operator()s caused them to be hidden
129 size_t operator() (WvStringParm s)
130 { return WvStream::operator()(filter ? (*filter)(s) : s); }
131 size_t operator() (WVSTRING_FORMAT_DECL)
132 { return (filter ?
133 WvStream::operator()((*filter)(WvString(WVSTRING_FORMAT_CALL))) :
134 WvStream::operator()(WVSTRING_FORMAT_CALL) );
138 * split off a new WvLog object with the requested loglevel. This way
139 * you can have log at two or more levels without having to retype
140 * log.lvl(WvLog::blahblah) all the time.
142 WvLog split(LogLevel _loglevel) const
143 { return WvLog(app, _loglevel, filter); }
146 * we override the unbuffered write function, so lines also include the
147 * application and log level.
149 virtual size_t uwrite(const void *buf, size_t len);
151 /** a useful substitute for the normal C perror() function */
152 void perror(WvStringParm s)
153 { print("%s: %s\n", s, strerror(errno)); }
155 public:
156 const char *wstype() const { return "WvLog"; }
160 #endif // __WVLOG_H