ostream_appender.cpp

Go to the documentation of this file.
00001 
00008 /**************************************************************************
00009 
00010    begin                : Sat Oct 13 2007
00011    copyright            : (C) 2007 by Ewald Arnold
00012    email                : log4sendpp at ewald-arnold dot de
00013 
00014    This program is free software; you can redistribute it and/or modify
00015    it under the terms of the GNU Lesser General Public License as
00016    published by the Free Software Foundation; either version 2 of the License,
00017    or (at your option) any later version.
00018 
00019    This program is distributed in the hope that it will be useful,
00020    but WITHOUT ANY WARRANTY; without even the implied warranty of
00021    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022    GNU General Public License for more details.
00023 
00024    You should have received a copy of the GNU Lesser General Public License
00025    along with this program; if not, write to the Free Software
00026    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00027 
00028  **/
00029 
00031 #define LOG4SENDPP_NEED_EXPORTS
00032 #include <log4sendpp/log4sendpp.h> // always first
00033 
00034 #include <iosfwd>
00035 
00036 #include <log4sendpp/ostream_appender.h>
00037 #include <log4sendpp/formatter.h>
00038 
00039 
00040 LOG4SENDPP_NS_START
00041 
00042 
00046 class ostream_Appender::StreamBuffer
00047   : public LOG4SENDPP_STD_NS::basic_streambuf<char>
00048 {
00049   public:
00050 
00054     StreamBuffer(ostream_Appender *in_appender)
00055       : appender(in_appender)
00056     {}
00057 
00062     virtual LOG4SENDPP_STD_NS::streamsize xsputn(const char *s, LOG4SENDPP_STD_NS::streamsize n)
00063     {
00064       buffer.append(s, n);
00065       return n;
00066     }
00067 
00071     virtual int_type overflow(int_type c = 0)
00072     {
00073       buffer += c;
00074       return c;
00075     }
00076 
00079     int sync( )
00080     {
00081       try
00082       {
00083         appender->writeString(buffer);
00084       }
00085       catch(...)
00086       {
00087         appender->setstate(LOG4SENDPP_STD_NS::ios_base::failbit);
00088       }
00089       buffer.clear();
00090       return 0;
00091     }
00092 
00093   private:
00094 
00095     ostream_Appender           *appender;
00096     LOG4SENDPP_STD_NS::string   buffer;
00097 };
00098 
00099 
00100 LOG4SENDPP_API_IMPL0
00101   ostream_Appender::ostream_Appender(Appender *in_appender)
00102   : Appender(0)
00103   , LOG4SENDPP_STD_NS::basic_ostream<char>((sbuffer = new StreamBuffer(this)))
00104   , appender(in_appender)
00105 {
00106 }
00107 
00108 
00109 LOG4SENDPP_API_IMPL0
00110   ostream_Appender::~ostream_Appender()
00111 {
00112   delete appender;
00113   delete sbuffer;
00114 }
00115 
00116 
00117 LOG4SENDPP_API_IMPL(void)
00118   ostream_Appender::append (Logger::Level level,
00119                             LOG4SENDPP_STD_NS::string msg,
00120                             LOG4SENDPP_STD_NS::string category,
00121                             LOG4SENDPP_INT64 stamp,
00122                             const LocationInformation *loginfo,
00123                             const Logger::DiagnosticInformation *diaginfo)
00124 {
00125   appender->append(level, msg, category, stamp, loginfo, diaginfo);
00126 }
00127 
00128 
00129 LOG4SENDPP_API_IMPL(void)
00130   ostream_Appender::writeString(const LOG4SENDPP_STD_NS::string &data)
00131 {
00132   appender->writeString(data);
00133 }
00134 
00135 
00136 LOG4SENDPP_API_IMPL(bool)
00137   ostream_Appender::isWorking() const
00138 {
00139   return appender->isWorking();
00140 }
00141 
00142 
00143 LOG4SENDPP_NS_END
00144 

Generated on Sat Nov 24 14:41:22 2007 for log4sendpp by  doxygen 1.5.3