LogChannel.hpp

Go to the documentation of this file.
00001 #pragma ident "$Id: LogChannel.hpp 2942 2011-10-25 16:43:21Z yanweignss $"
00002 
00008 #ifndef GPSTK_LOGCHANNEL_HPP
00009 #define GPSTK_LOGCHANNEL_HPP
00010 
00011 //============================================================================
00012 //
00013 //  This file is part of GPSTk, the GPS Toolkit.
00014 //
00015 //  The GPSTk is free software; you can redistribute it and/or modify
00016 //  it under the terms of the GNU Lesser General Public License as published
00017 //  by the Free Software Foundation; either version 2.1 of the License, or
00018 //  any later version.
00019 //
00020 //  The GPSTk is distributed in the hope that it will be useful,
00021 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 //  GNU Lesser General Public License for more details.
00024 //
00025 //  You should have received a copy of the GNU Lesser General Public
00026 //  License along with GPSTk; if not, write to the Free Software Foundation,
00027 //  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00028 //
00029 //  Wei Yan - Chinese Academy of Sciences . 2011
00030 //
00031 //============================================================================
00032 
00033 #include <string>
00034 #include <fstream>
00035 #include "LogMessage.hpp"
00036 
00037 namespace gpstk
00038 {
00039  
00067 
00068    class LogChannel   
00069    {
00070    public:
00071 
00072       LogChannel(const std::string& fmt = "%t") 
00073          : ostrm(std::clog), pattern(fmt) {}      
00074       
00075       LogChannel(std::ostream& strm, const std::string& fmt="%t") 
00076          : ostrm(strm), pattern(fmt) {}      
00077       
00078       virtual ~LogChannel(){}
00079 
00080       std::string getLogText(const LogMessage& msg);
00081 
00082       virtual void log(const LogMessage& msg){}
00083 
00084       void setPattern(const std::string& fmt="%t")
00085       {
00086          pattern = fmt;
00087       }
00088       
00089    protected:
00090       std::ostream& ostrm;
00091       std::string pattern;
00092 
00093    };   // End of class 'LogChannel'
00094 
00095    class ConsoleLogChannel : public LogChannel   
00096    {
00097    public:
00098 
00099       ConsoleLogChannel(const std::string& fmt = "%t") 
00100          : LogChannel(fmt), ostrm(std::clog) {}      
00101 
00102       ConsoleLogChannel(std::ostream& strm, const std::string& fmt="%t") 
00103          : LogChannel(fmt), ostrm(strm) {}      
00104 
00105       virtual ~ConsoleLogChannel(){}
00106 
00107       virtual void log(const LogMessage& msg)
00108       {
00109          ostrm << getLogText(msg) << std::endl;
00110       }
00111 
00112       void setPattern(const std::string& fmt="%t")
00113       {
00114          pattern = fmt;
00115       }
00116 
00117    public:
00118       std::ostream& ostrm;
00119       std::string pattern;
00120 
00121    };   // End of class 'LogChannel'
00122 
00123 
00124    class FileLogChannel : public LogChannel
00125    {
00126    public:
00127       FileLogChannel(const std::string& filename, const std::string& fmt = "%t")
00128          : LogChannel(fmt)  { setFile(filename); }      
00129 
00130       virtual ~FileLogChannel(){fstrm.close();}
00131 
00132 
00133       virtual void setFile(const std::string& filename)
00134       {
00135          fstrm.open(filename.c_str());
00136          logFile = filename;
00137       }
00138 
00139       virtual void log(const LogMessage& msg)
00140       {
00141          fstrm << getLogText(msg) << std::endl;        
00142       }
00143 
00144    protected:
00145       std::string logFile;
00146       std::ofstream fstrm;
00147    };
00148 }   // End of namespace gpstk
00149 
00150 
00151 #endif  //GPSTK_LOGCHANNEL_HPP
00152 

Generated on Tue May 22 03:30:59 2012 for GPS ToolKit Software Library by  doxygen 1.3.9.1