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 namespace log4cpp { 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 template<typename T> 00093 CategoryStream& operator<<(const std::string& t) { 00094 if (getPriority() != Priority::NOTSET) { 00095 if (!_buffer) { 00096 if (!(_buffer = new std::ostringstream)) { 00097 // XXX help help help 00098 } 00099 } 00100 (*_buffer) << t; 00101 } 00102 return *this; 00103 } 00104 #if LOG4CPP_HAS_WCHAR_T != 0 00105 template<typename T> 00106 CategoryStream& operator<<(const std::wstring& t) { 00107 if (getPriority() != Priority::NOTSET) { 00108 if (!_wbuffer) { 00109 if (!(_wbuffer = new std::wostringstream)) { 00110 // XXX help help help 00111 } 00112 } 00113 (*_wbuffer) << t; 00114 } 00115 return *this; 00116 } 00117 #endif 00118 00121 std::streamsize width(std::streamsize wide ); 00122 00123 00124 private: 00125 Category& _category; 00126 Priority::Value _priority; 00127 union { 00128 std::ostringstream* _buffer; 00129 #if LOG4CPP_HAS_WCHAR_T != 0 00130 std::wostringstream* _wbuffer; 00131 #endif 00132 }; 00133 00134 public: 00135 typedef CategoryStream& (*cspf) (CategoryStream&); 00136 00137 CategoryStream& operator<< (cspf); 00138 LOG4CPP_EXPORT friend CategoryStream& eol (CategoryStream& os); 00139 LOG4CPP_EXPORT friend CategoryStream& left (CategoryStream& os); 00140 }; 00141 } 00142 #endif // _LOG4CPP_CATEGORYSTREAM_HH