logger_stream.hpp

Go to the documentation of this file.
00001 // -*- mode: c++; indent-tabs-mode: nil; -*-
00002 //
00003 //The Biomolecule Toolkit (BTK) is a C++ library for use in the
00004 //modeling, analysis, and design of biological macromolecules.
00005 //Copyright (C) 2006, Tim Robertson <kid50@users.sourceforge.net>
00006 //
00007 //This program is free software; you can redistribute it and/or modify
00008 //it under the terms of the GNU Lesser General Public License as published
00009 //by the Free Software Foundation; either version 2.1 of the License, or (at
00010 //your option) any later version.
00011 //
00012 //This program is distributed in the hope that it will be useful,  but
00013 //WITHOUT ANY WARRANTY; without even the implied warranty of
00014 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 //Lesser General Public License for more details.
00016 //
00017 //You should have received a copy of the GNU Lesser General Public License
00018 //along with this program; if not, write to the Free Software Foundation,
00019 //Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020 
00021 #ifndef BTK_IO_LOGGER_STREAM_HPP
00022 #define BTK_IO_LOGGER_STREAM_HPP
00023 
00024 #include <ostream>
00025 #include <sstream>
00026 #include <string>
00027 
00028 #include <btk/core/io/logger.hpp>
00029 
00030 namespace BTK {
00031 namespace IO {
00032 namespace LOGGING {
00033 
00034 template <typename CharType, typename TraitsType = std::char_traits<CharType> >
00035   class LoggerStreambuf : public std::basic_stringbuf<CharType,TraitsType>
00036 {
00037 public:
00038   typedef std::basic_stringbuf<CharType,TraitsType> base_buf;
00039   
00040   LoggerStreambuf(LoggerInterface const & logger,
00041                   LoggerLevel level) : 
00042     _logger(logger), _log_level(level) {}
00043   
00044   ~LoggerStreambuf() 
00045   {
00046     sync();
00047   }
00048   
00049 protected:
00050   
00051   int sync()
00052   {
00053     _logger.log(base_buf::str(),_log_level);
00054     base_buf::str(std::string());
00055     return 0;
00056   }
00057   
00058 private:
00059   LoggerInterface const & _logger;
00060   LoggerLevel _log_level;
00061 };
00062 
00063 template <typename CharType, typename TraitsType = std::char_traits<CharType> >
00064 class LoggerStream : public std::basic_ostream<CharType,TraitsType>
00065 {
00066 public:
00067   typedef std::basic_ostream<CharType,TraitsType> base_stream;
00068   typedef LoggerStreambuf<CharType,TraitsType> LoggerStreambuf;
00069 
00070   LoggerStream(LoggerLevel level) : 
00071     base_stream(new LoggerStreambuf(Logger::instance(),level)) {}
00072 
00073   LoggerStream(LoggerInterface const & logger,
00074                LoggerLevel level) :
00075     base_stream(new LoggerStreambuf(logger,level)) {}
00076 
00077   ~LoggerStream() { delete base_stream::rdbuf(); }
00078 };
00079 
00080 } // namespace LOGGING
00081 } // namespace IO
00082 } // namespace BTK
00083 
00084 #endif 

Generated on Sun Jul 15 20:46:25 2007 for BTK Core by  doxygen 1.5.1