00001 #include <algorithm>
00002 #include <iostream>
00003 #include "logserverstreambuf.h"
00004
00005 using namespace std;
00006 using namespace zeitgeist;
00007
00008 LogServerStreamBuf::LogServerStreamBuf(unsigned int size) :
00009 streambuf(), mSize(size), mCurrentPriority(0xffffffff)
00010 {
00011
00012 if (mSize)
00013 {
00014 char* ptr = new char[mSize];
00015 setp(ptr, ptr + mSize);
00016 }
00017 else
00018 {
00019 setp(0, 0);
00020 }
00021
00022
00023 setg(0, 0, 0);
00024 }
00025
00026 LogServerStreamBuf::~LogServerStreamBuf()
00027 {
00028
00029 sync();
00030
00031
00032 while (mStreams.size() != 0)
00033 {
00034 if (mStreams.back().second != &std::cout && mStreams.back().second != &std::cerr)
00035 delete mStreams.back().second;
00036 mStreams.pop_back();
00037 }
00038
00039
00040 delete[] pbase();
00041 }
00042
00043 void LogServerStreamBuf::AddStream(std::ostream *stream, unsigned int mask)
00044 {
00045 TMaskStreams::iterator i;
00046 i = find_if(mStreams.begin(), mStreams.end(), MaskStreamEQ(stream));
00047
00048 if (i == mStreams.end())
00049 {
00050 TMaskStream pstream(mask, stream);
00051 mStreams.push_back(pstream);
00052 }
00053 else
00054 {
00055 i->first |= mask;
00056 }
00057 }
00058
00059 bool LogServerStreamBuf::RemoveStream(const std::ostream *stream)
00060 {
00061
00062 sync();
00063
00064 TMaskStreams::iterator i;
00065 i = find_if(mStreams.begin(), mStreams.end(), MaskStreamEQ(stream));
00066
00067 if (i != mStreams.end())
00068 {
00069 mStreams.erase(i);
00070 return true;
00071 }
00072 return false;
00073 }
00074
00075 bool
00076 LogServerStreamBuf::SetPriorityMask(const std::ostream *stream, unsigned int mask)
00077 {
00078
00079 sync();
00080
00081 TMaskStreams::iterator i;
00082 i = find_if(mStreams.begin(), mStreams.end(), MaskStreamEQ(stream));
00083
00084 if (i != mStreams.end())
00085 {
00086 i->first = mask;
00087 return true;
00088 }
00089 return false;
00090 }
00091
00092 unsigned int LogServerStreamBuf::GetPriorityMask(const std::ostream *stream) const
00093 {
00094 TMaskStreams::const_iterator i;
00095 i = find_if(mStreams.begin(), mStreams.end(), MaskStreamEQ(stream));
00096
00097 if (i != mStreams.end())
00098 {
00099 return i->first;
00100 }
00101 return 0;
00102 }
00103
00104 void LogServerStreamBuf::SetCurrentPriority(unsigned int priority)
00105 {
00106 sync();
00107 mCurrentPriority = priority;
00108 }
00109
00114 LogServerStreamBuf::TIntType LogServerStreamBuf::overflow(TIntType c)
00115 {
00116
00117 PutBuffer();
00118
00119
00120 if (c != TTraitsType::eof())
00121 {
00122
00123 if (pbase() == epptr())
00124 {
00125
00126 PutChar(c);
00127 }
00128 else
00129 {
00130
00131 sputc(c);
00132 }
00133 }
00134 return 0;
00135 }
00136
00141 int LogServerStreamBuf::sync()
00142 {
00143 PutBuffer();
00144
00145 return 0;
00146 }
00147
00148 void LogServerStreamBuf::Forward(const char *buffer, unsigned int length)
00149 {
00150 TMaskStreams::iterator i;
00151 for (i=mStreams.begin(); i!= mStreams.end(); ++i)
00152 {
00153 if ((*i).first & mCurrentPriority)
00154 {
00155 (*i).second->write(buffer, length);
00156 }
00157 }
00158 }
00159
00160 void LogServerStreamBuf::PutBuffer()
00161 {
00162
00163 if (pbase() != pptr())
00164 {
00165 int len = (pptr() - pbase());
00166
00167
00168 Forward(pbase(), len);
00169
00170
00171 setp(pbase(), epptr());
00172 }
00173 }
00174
00175 void
00176 LogServerStreamBuf::PutChar(TIntType chr)
00177 {
00178 char a[1];
00179 a[0] = chr;
00180 Forward(a, 1);
00181 }