00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef OST_LOG_SINK_HH
00020 #define OST_LOG_SINK_HH
00021
00022 #include <ostream>
00023 #include <sstream>
00024 #include <iostream>
00025 #include <fstream>
00026 #include <stack>
00027 #include <vector>
00028
00029 #include <boost/shared_ptr.hpp>
00030
00031 #include <ost/module_config.hh>
00032
00033 namespace ost {
00034
00035 class DLLEXPORT LogSink {
00036 public:
00037 LogSink(){};
00038 virtual ~LogSink() { }
00039 virtual void LogMessage(const String& message, int severity=0) {};
00040 };
00041
00042 typedef boost::shared_ptr<LogSink> LogSinkPtr;
00043
00044 class DLLEXPORT StreamLogSink : public LogSink {
00045 public:
00046 StreamLogSink(std::ostream& stream):stream_(stream){}
00047 virtual void LogMessage(const String& message, int severity){
00048 stream_ << message;
00049 }
00050
00051 private:
00052 std::ostream& stream_;
00053 };
00054
00055 class DLLEXPORT StringLogSink : public LogSink {
00056 public:
00057 StringLogSink():LogSink(),stream_(){}
00058 virtual void LogMessage(const String& message, int severity){
00059 stream_ << message;
00060 }
00061 String GetLog() const
00062 {
00063 return stream_.str();
00064 }
00065
00066 private:
00067 std::ostringstream stream_;
00068 };
00069
00070 typedef boost::shared_ptr<StringLogSink> StringLogSinkPtr;
00071
00072 class DLLEXPORT FileLogSink : public LogSink {
00073 public:
00074 FileLogSink(const String& file_name):stream_(file_name.c_str(), std::ios::out){}
00075 virtual void LogMessage(const String& message, int severity){
00076 if (stream_.is_open()){
00077 stream_ << message;
00078 stream_.flush();
00079 }
00080 }
00081
00082 ~FileLogSink(){
00083 stream_.flush();
00084 }
00085 private:
00086 std::ofstream stream_;
00087 };
00088
00089 typedef boost::shared_ptr<FileLogSink> FileLogSinkPtr;
00090
00091
00092 class DLLEXPORT_OST_BASE MultiLogSink : public LogSink {
00093 public:
00094 MultiLogSink();
00095 bool AddSink(LogSinkPtr& observer);
00096 bool RemoveSink(LogSinkPtr& observer);
00097 void LogMessage(const String& message, int severity);
00098 private:
00099 std::vector<LogSinkPtr> sinks_;
00100 };
00101
00102 typedef boost::shared_ptr<MultiLogSink> MultiLogSinkPtr;
00103
00104 }
00105 #endif