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
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
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 };
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 };
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 }
00149
00150
00151 #endif //GPSTK_LOGCHANNEL_HPP
00152