00001 /* 00002 * CategoryStream.hh 00003 * 00004 * Copyright 2001, LifeLine Networks BV (www.lifeline.nl). All rights reserved. 00005 * Copyright 2001, Bastiaan Bakker. All rights reserved. 00006 * 00007 * See the COPYING file for the terms of usage and distribution. 00008 */ 00009 00010 #ifndef _LOG4CPP_CATEGORYSTREAM_HH 00011 #define _LOG4CPP_CATEGORYSTREAM_HH 00012 00013 #include <log4cpp/Portability.hh> 00014 #include <log4cpp/Priority.hh> 00015 #include <ios> 00016 #ifdef LOG4CPP_HAVE_SSTREAM 00017 #include <sstream> 00018 #endif 00019 #include <log4cpp/Manipulator.hh> 00020 00021 LOG4CPP_NS_BEGIN 00022 00023 class LOG4CPP_EXPORT Category; 00024 class LOG4CPP_EXPORT CategoryStream; 00028 LOG4CPP_EXPORT CategoryStream& eol(CategoryStream& os); 00029 00033 LOG4CPP_EXPORT CategoryStream& left(CategoryStream& os); 00034 00039 class LOG4CPP_EXPORT CategoryStream { 00040 public: 00041 00048 CategoryStream(Category& category, Priority::Value priority); 00049 00053 ~CategoryStream(); 00054 00059 inline Category& getCategory() const { return _category; }; 00060 00065 inline Priority::Value getPriority() const throw() { 00066 return _priority; 00067 }; 00068 00073 void flush(); 00074 00080 template<typename T> CategoryStream& operator<<(const T& t) { 00081 if (getPriority() != Priority::NOTSET) { 00082 if (!_buffer) { 00083 if (!(_buffer = new std::ostringstream)) { 00084 // XXX help help help 00085 } 00086 } 00087 (*_buffer) << t; 00088 } 00089 return *this; 00090 } 00091 00092 CategoryStream& operator<<(const char* t); 00093 00094 template<typename T> 00095 CategoryStream& operator<<(const std::string& t) { 00096 if (getPriority() != Priority::NOTSET) { 00097 if (!_buffer) { 00098 if (!(_buffer = new std::ostringstream)) { 00099 // XXX help help help 00100 } 00101 } 00102 (*_buffer) << t; 00103 } 00104 return *this; 00105 } 00106 #if LOG4CPP_HAS_WCHAR_T != 0 00107 template<typename T> 00108 CategoryStream& operator<<(const std::wstring& t) { 00109 if (getPriority() != Priority::NOTSET) { 00110 if (!_wbuffer) { 00111 if (!(_wbuffer = new std::wostringstream)) { 00112 // XXX help help help 00113 } 00114 } 00115 (*_wbuffer) << t; 00116 } 00117 return *this; 00118 } 00119 #endif 00120 00123 std::streamsize width(std::streamsize wide); 00124 00125 00126 private: 00127 Category& _category; 00128 Priority::Value _priority; 00129 union { 00130 std::ostringstream* _buffer; 00131 #if LOG4CPP_HAS_WCHAR_T != 0 00132 std::wostringstream* _wbuffer; 00133 #endif 00134 }; 00135 00136 public: 00137 typedef CategoryStream& (*cspf) (CategoryStream&); 00138 00139 CategoryStream& operator<< (cspf); 00140 LOG4CPP_EXPORT friend CategoryStream& eol(CategoryStream& os); 00141 LOG4CPP_EXPORT friend CategoryStream& left(CategoryStream& os); 00142 00143 private: 00144 // suppress assignment operator 00145 CategoryStream & operator=(const CategoryStream &); 00146 00147 }; 00148 LOG4CPP_NS_END 00149 #endif // _LOG4CPP_CATEGORYSTREAM_HH