Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

wvlogbuffer.cc

Go to the documentation of this file.
00001 /*
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
00004  * 
00005  * WvLogBuffer is a descendant of WvLogRcv that buffers log messages for
00006  * later use.  It only keeps up to max_lines log entries of _each_ log
00007  * level max_level or lower.
00008  */
00009 #include "wvlogbuffer.h"
00010 #include "strutils.h"
00011 #include <time.h>
00012 
00013 WvLogBuffer::Msg::Msg(WvLog::LogLevel _level, const WvString &_source)
00014                 : level(_level), source(_source)
00015 {
00016     time(&timestamp);
00017 }
00018 
00019 
00020 WvLogBuffer::WvLogBuffer(int _max_lines, WvLog::LogLevel _max_level)
00021         : WvLogRcv(_max_level)
00022 {
00023     max_lines = _max_lines;
00024     memset(numlines, 0, sizeof(numlines));
00025     lastmsg = NULL;
00026 }
00027 
00028 
00029 WvLogBuffer::~WvLogBuffer()
00030 {
00031     end_line();
00032 }
00033 
00034 
00035 void WvLogBuffer::_begin_line()
00036 {
00037     lastmsg = new Msg(last_level, last_source->app);
00038 }
00039 
00040 
00041 void WvLogBuffer::_mid_line(const char *str, size_t len)
00042 {
00043     current.put(str, len);
00044 }
00045 
00046 
00047 void WvLogBuffer::_end_line()
00048 {
00049     if (lastmsg)
00050     {
00051         current.put("", 1);  // terminating NULL
00052         lastmsg->message = trim_string((char *)current.get(current.used()));
00053         lastmsg->message.unique();
00054         
00055         if (lastmsg->level < WvLog::NUM_LOGLEVELS)
00056         {
00057             // when we add a log message to the buffer, we need to remove 
00058             // some from the top of the buffer, if it is fills...
00059             int &nl = numlines[lastmsg->level];
00060             
00061             MsgList::Iter i(msgs);
00062             i.rewind(); i.next();
00063             while (nl >= max_lines && i.cur())
00064             {
00065                 Msg &m = i;
00066                 if (m.level == lastmsg->level)
00067                 {
00068                     i.unlink();
00069                     nl--;
00070                 }
00071                 else
00072                     i.next();
00073             }
00074             
00075             msgs.append(lastmsg, true);
00076             nl++;
00077         }
00078         else
00079             delete lastmsg;
00080         lastmsg = NULL;
00081         
00082     }
00083 }
00084 
00085 
00086 void WvLogBuffer::dump(WvStream &s)
00087 {
00088     MsgList::Iter i(messages());
00089     
00090     for (i.rewind(); i.next(); )
00091     {
00092         Msg &m = i;
00093         s.print("%s %s<%s>: %s+\n",
00094                 m.timestamp, m.source, loglevels[m.level], m.message);
00095     }
00096 }

Generated on Sun Aug 25 12:42:16 2002 for WvStreams by doxygen1.2.15