00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef OST_LOG_HH
00020 #define OST_LOG_HH
00021
00022 #include <ostream>
00023 #include <stack>
00024
00025 #include <ost/log_sink.hh>
00026 #include <ost/module_config.hh>
00027
00028 namespace ost {
00029
00030 typedef std::stack<LogSinkPtr> LogSinkStack;
00031
00032
00033 class DLLEXPORT_OST_BASE Logger {
00034 public:
00035 enum LogLevel {
00036 QUIET =0,
00037 WARNING =1,
00038 SCRIPT =2,
00039 INFO =3,
00040 VERBOSE =4,
00041 DEBUG =5,
00042 TRACE =6
00043 };
00044
00045 void PushVerbosityLevel(int level);
00046 void PopVerbosityLevel();
00047 void PushSink(LogSinkPtr& sink);
00048 void PushFile(const String& filename);
00050 void PopFile();
00051 void PopSink();
00052
00053 static Logger& Instance();
00054 LogSinkPtr GetCurrentSink() { return sink_stack_.top(); }
00055 int GetVerbosityLevel() const {return level_;}
00056
00057 void ResetSinks() {
00058 while (sink_stack_.size()>1) {
00059 sink_stack_.pop();
00060 }
00061 }
00062 protected:
00063 Logger();
00064 Logger(const Logger&);
00065 Logger& operator=(const Logger&);
00066
00067 private:
00068 int level_;
00069 std::stack<int> level_stack_;
00070 LogSinkStack sink_stack_;
00071 };
00072
00073
00074 #define OST_DO_LOGGING_(m, l) if (::ost::Logger::Instance().GetVerbosityLevel()>=l) {\
00075 std::stringstream tmp_s__; \
00076 tmp_s__ << m << std::endl; \
00077 ::ost::Logger::Instance().GetCurrentSink()->LogMessage(tmp_s__.str(), l); \
00078 }
00079
00080 #define WARN_DEPRECATED(m) OST_DO_LOGGING_(m, ::ost::Logger::WARNING)
00081 #define PUSH_VERBOSITY(n) ::ost::Logger::Instance().PushVerbosityLevel(n)
00082 #define POP_VERBOSITY(n) ::ost::Logger::Instance().PopVerbosityLevel()
00083
00084 #define LOG_ERROR(m) OST_DO_LOGGING_(m, ::ost::Logger::QUIET)
00085 #define LOG_WARNING(m) OST_DO_LOGGING_(m, ::ost::Logger::WARNING)
00086 #define LOG_SCRIPT(m) OST_DO_LOGGING_(m, ::ost::Logger::SCRIPT)
00087 #define LOG_INFO(m) OST_DO_LOGGING_(m, ::ost::Logger::INFO)
00088 #define LOG_VERBOSE(m) OST_DO_LOGGING_(m, ::ost::Logger::VERBOSE)
00089 #ifdef NDEBUG
00090 # define LOG_DEBUG(m)
00091 # define LOG_TRACE(m)
00092 #else
00093 # define LOG_DEBUG(m) OST_DO_LOGGING_(m, ::ost::Logger::DEBUG)
00094 # define LOG_TRACE(m) OST_DO_LOGGING_(m, ::ost::Logger::TRACE)
00095 #endif
00096
00097 }
00098
00099 #endif