00001 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- 00002 00003 this file is part of rcssserver3D 00004 Fri May 9 2003 00005 Copyright (C) 2002,2003 Koblenz University 00006 Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group 00007 $Id: logserverstreambuf.h,v 1.4 2005/05/18 14:55:21 jamu Exp $ 00008 00009 This program is free software; you can redistribute it and/or modify 00010 it under the terms of the GNU General Public License as published by 00011 the Free Software Foundation; version 2 of the License. 00012 00013 This program is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with this program; if not, write to the Free Software 00020 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00021 */ 00022 #ifndef ZEITGEIST_LOGSERVERSTREAMBUF_H 00023 #define ZEITGEIST_LOGSERVERSTREAMBUF_H 00024 00048 #include <functional> 00049 #include <vector> 00050 #include <streambuf> 00051 00052 namespace zeitgeist 00053 { 00054 00055 class LogServerStreamBuf : public std::streambuf 00056 { 00057 // types 00058 // 00059 protected: 00060 typedef std::char_traits<char> TTraitsType; 00061 typedef traits_type::int_type TIntType; 00062 00063 private: 00064 typedef std::pair<unsigned int, std::ostream*> TMaskStream; 00065 typedef std::vector<TMaskStream> TMaskStreams; 00066 00067 // functions 00068 // 00069 public: 00070 LogServerStreamBuf(unsigned int size); 00071 virtual ~LogServerStreamBuf(); 00072 00082 void AddStream(std::ostream *stream, unsigned int mask); 00087 bool RemoveStream(const std::ostream *stream); 00093 bool SetPriorityMask(const std::ostream *stream, unsigned int mask); 00098 unsigned int GetPriorityMask(const std::ostream *stream) const; 00099 00108 void SetCurrentPriority(unsigned int priority); 00109 00110 protected: 00111 // these functions implement the streambuf interface ... handle with care 00112 TIntType overflow(TIntType c); 00113 int sync(); 00114 00115 private: 00116 LogServerStreamBuf(const LogServerStreamBuf &obj); 00117 LogServerStreamBuf& operator=(const LogServerStreamBuf &obj); 00118 00120 void Forward(const char *buffer, unsigned int length); 00122 void PutBuffer(); 00124 void PutChar(TIntType chr); 00125 00127 class MaskStreamEQ : public std::unary_function<TMaskStream, bool> 00128 { 00129 private: 00130 const std::ostream *stream; 00131 public: 00132 explicit MaskStreamEQ(const std::ostream *str) : stream(str) {} 00133 bool operator ()(const TMaskStream &ms) 00134 { return ms.second == stream; } 00135 }; 00136 00137 // members 00138 // 00139 private: 00141 unsigned int mSize; 00142 unsigned int mCurrentPriority; 00143 TMaskStreams mStreams; 00144 }; 00145 00146 } //namespace 00147 00148 #endif // ZEITGEIST_LOGSERVERSTREAMBUF_H