Make WvStreams compile with gcc 4.4.
[wvstreams.git] / include / wvlogrcv.h
blobaf7af57a507cf2160c6b225c4f6b6b497387e980
1 /* -*- Mode: C++ -*-
2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
5 * Enhanced "Log Receiver" classes for WvLog.
6 *
7 * WvLogRcv-derived classes support automatic formatting of log lines with
8 * a prefix, removing control characters, and so on.
9 *
10 * WvLogRcv supports partial- and multiple-line log messages. For example,
11 * log.print("test ");
12 * log.print("string\nfoo");
13 * will print:
14 * appname(lvl): test string
15 * appname(lvl): foo
17 #ifndef __WVLOGRCV_H
18 #define __WVLOGRCV_H
20 #include "wvlog.h"
21 #include "wvfdstream.h"
22 #include "wvscatterhash.h"
24 /**
25 * WvLogRcv adds some intelligence to WvLogRcvBase, to keep
26 * track of line-prefix-printing and other formatting information.
28 class WvLogRcv : public WvLogRcvBase
30 protected:
31 WvString last_source;
32 WvLog::LogLevel max_level, last_level;
33 time_t last_time;
34 bool at_newline;
35 WvString prefix;
36 size_t prelen;
38 class Src_Lvl
40 public:
41 WvString src;
42 WvLog::LogLevel lvl;
43 Src_Lvl(WvString _src, int _lvl) : src(_src),
44 lvl((WvLog::LogLevel)_lvl) {};
47 DeclareWvScatterDict(Src_Lvl, WvString, src);
49 Src_LvlDict custom_levels;
51 /** Set the Prefix and Prefix Length (size_t prelen) */
52 virtual void _make_prefix(time_t now);
54 /** Start a new log line (print prefix) */
55 virtual void _begin_line();
57 /** End this (Guaranteed NonEmpty) log line */
58 virtual void _end_line();
60 /**
61 * add text to the current log line. 'str' may contain only
62 * one '\n' optional character at str[len-1] (the end); if it does,
63 * end_line will be called immediately after this function.
65 virtual void _mid_line(const char *str, size_t len) = 0;
67 private:
68 void begin_line()
69 { if (!at_newline) return; _begin_line(); at_newline = false; }
70 void mid_line(const char *str, size_t len)
71 { _mid_line(str, len);
72 if (len>0 && str[len-1] == '\n') at_newline = true; }
74 public:
75 virtual void log(WvStringParm source, int loglevel,
76 const char *_buf, size_t len);
78 static const char *loglevels[WvLog::NUM_LOGLEVELS];
80 WvLogRcv(WvLog::LogLevel _max_level = WvLog::NUM_LOGLEVELS);
81 virtual ~WvLogRcv();
83 void end_line()
84 { if (at_newline) return;
85 _mid_line("\n", 1); _end_line(); at_newline = true; };
87 WvLog::LogLevel level() const
88 { return max_level; }
89 void level(WvLog::LogLevel lvl)
90 { max_level = lvl; }
93 * Allows you to override debug levels for specific sources
94 * example: mysql=9,Service Manager=9
95 * will get all the debug output from all the sources that
96 * contain (case insensitively) "mysql" or "Service Manager"
97 * in the source name regardless of the current debug level.
99 bool set_custom_levels(WvString descr);
104 * Captures formatted log messages and outputs them to the
105 * specified file descriptor.
107 class WvLogConsole : public WvFDStream, public WvLogRcv
109 public:
110 WvLogConsole(int _fd,
111 WvLog::LogLevel _max_level = WvLog::NUM_LOGLEVELS);
112 virtual ~WvLogConsole();
113 protected:
114 virtual void _mid_line(const char *str, size_t len);
117 #endif // __WVLOGRCV_H