WvStreams
wvlog.h
1/* -*- Mode: C++ -*-
2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
4 *
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
15 */
16#ifndef __WVLOG_H
17#define __WVLOG_H
18
19#include "wvstream.h"
20#include <errno.h>
21#ifdef _WIN32
22typedef int pid_t;
23#endif
24
25class WvLog;
26
27// a WvLogRcv registers itself with WvLog and prints, captures,
28// or transmits log messages.
30{
31 friend class WvLog;
32protected:
33 const char *appname(WvStringParm log) const;
34 virtual void log(WvStringParm source, int loglevel,
35 const char *_buf, size_t len) = 0;
36
37private:
38 static void cleanup_on_fork(pid_t p);
39 static void static_init();
40
41public:
42 bool force_new_line;
44 virtual ~WvLogRcvBase();
45};
46
47
48DeclareWvList(WvLogRcvBase);
49
50typedef wv::function<WvString(WvStringParm)> WvLogFilter;
51
56class WvLog : public WvStream
57{
58 friend class WvLogRcvBase;
59public:
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,
71
72 NUM_LOGLEVELS
73 };
74 WvString app;
75
76protected:
77 LogLevel loglevel;
78 static WvLogRcvBaseList *receivers;
79 static int num_receivers, num_logs;
80 static WvLogRcvBase *default_receiver;
81 WvLogFilter* filter;
82
83public:
84 WvLog(WvStringParm _app, LogLevel _loglevel = Info,
85 WvLogFilter* filter = 0);
86 WvLog(const WvLog &l);
87 virtual ~WvLog();
88
90 virtual bool isok() const;
91
92 /* always writable */
93 virtual void pre_select(SelectInfo &si);
94 virtual bool post_select(SelectInfo &si);
95
100 WvLog &lvl(LogLevel _loglevel)
101 { loglevel = _loglevel; return *this; }
102
104 size_t operator() (LogLevel _loglevel, WvStringParm s)
105 {
106 LogLevel l = loglevel;
107 size_t x = lvl(_loglevel).write(filter ? (*filter)(s) : s);
108 lvl(l);
109 return x;
110 }
111
113 size_t operator() (LogLevel _loglevel, WVSTRING_FORMAT_DECL)
114 {
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;
123 }
124
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) );
135 }
136
142 WvLog split(LogLevel _loglevel) const
143 { return WvLog(app, _loglevel, filter); }
144
149 virtual size_t uwrite(const void *buf, size_t len);
150
153 { print("%s: %s\n", s, strerror(errno)); }
154
155public:
156 const char *wstype() const { return "WvLog"; }
157};
158
159
160#endif // __WVLOG_H
static WvString strerror(int errnum)
A replacement for the operating system strerror() function that can map more kinds of error strings (...
Definition wverror.cc:91
A WvFastString acts exactly like a WvString, but can take (const char *) strings without needing to a...
Definition wvstring.h:94
A WvLog stream accepts log messages from applications and forwards them to all registered WvLogRcv's.
Definition wvlog.h:57
WvLog & lvl(LogLevel _loglevel)
change the loglevel.
Definition wvlog.h:100
virtual bool isok() const
fd==-1, but this stream is always ok
Definition wvlog.cc:75
virtual bool post_select(SelectInfo &si)
post_select() is called after select(), and returns true if this object is now ready.
Definition wvlog.cc:91
WvLog split(LogLevel _loglevel) const
split off a new WvLog object with the requested loglevel.
Definition wvlog.h:142
size_t operator()(LogLevel _loglevel, WvStringParm s)
change the loglevel and then print a message.
Definition wvlog.h:104
virtual size_t uwrite(const void *buf, size_t len)
we override the unbuffered write function, so lines also include the application and log level.
Definition wvlog.cc:101
void perror(WvStringParm s)
a useful substitute for the normal C perror() function
Definition wvlog.h:152
virtual void pre_select(SelectInfo &si)
pre_select() sets up for eventually calling select().
Definition wvlog.cc:81
Unified support for streams, that is, sequences of bytes that may or may not be ready for read/write ...
Definition wvstream.h:25
virtual size_t write(const void *buf, size_t count)
Write data to the stream.
Definition wvstream.cc:532
WvString is an implementation of a simple and efficient printable-string class.
Definition wvstring.h:330
the data structure used by pre_select()/post_select() and internally by select().
Definition iwvstream.h:50