WvStreams
wvlogbuffer.h
00001 /* -*- Mode: C++ -*-
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
00004  *
00005  */ 
00006 #ifndef __WVLOGBUFFER_H
00007 #define __WVLOGBUFFER_H
00008 
00009 #include "wvlogrcv.h"
00010 #include "wvhashtable.h"
00011 
00017 class WvLogBuffer : public WvLogRcv
00018 {
00019 public:
00020     // An actual message
00021     class Msg
00022     {
00023         public:
00024             time_t timestamp;
00025             WvLog::LogLevel level;
00026             WvString source, message; 
00027 
00028             Msg(WvLog::LogLevel _level, WvStringParm _source, WvString _message);
00029     };
00030 
00031     DeclareWvList(Msg); 
00032     
00033     /*
00034      * Maps a string describing msg type of the form "source:level"
00035      * to a list of pointers to all messages of this type
00036      */
00037     class MsgCounter
00038     {
00039         public:
00040             MsgCounter(WvString _src_lvl) : src_lvl(_src_lvl) {};
00041             Msg* add(Msg* msg, int max);
00042             WvString src_lvl;
00043         private:
00044             MsgList list;
00045     };
00046     
00047     DeclareWvDict(MsgCounter, WvString, src_lvl);
00048     
00049 protected:
00050     /*
00051      * "Owns" all the msges
00052      */
00053     MsgList msgs;
00054     /*
00055      * Used for keeping track of how many messages from a given source/level
00056      *      there are. Just stores the pointers.
00057      */
00058     MsgCounterDict counters;
00059     WvDynBuf current;
00060     int max_lines;
00061 
00062     void handle_msg(Msg *lastmsg);
00063     
00064     virtual void _begin_line() {};
00065     virtual void _mid_line(const char *str, size_t len);
00066     virtual void _end_line();
00067 
00068 public:
00069     WvLogBuffer(int _max_lines, 
00070                 WvLog::LogLevel _max_level = WvLog::NUM_LOGLEVELS);
00071     virtual ~WvLogBuffer();
00072 
00073     MsgList &messages()
00074         { end_line(); return msgs; }
00075 
00076     void feed_receiver(WvLogRcv& receiver);
00077 
00078     void dump(WvStream &s);
00079 };
00080 
00081 #endif // __WVLOGBUFFER_H