00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _plog_h_
00021 #define _plog_h_
00022
00023 #include <pclasses/pexport.h>
00024 #include <pclasses/pexception.h>
00025 #include <pclasses/pcriticalsection.h>
00026 #include <pclasses/pthreadkey.h>
00027 #include <pclasses/ptime.h>
00028 #include <set>
00029 #include <list>
00030 #include <string>
00031 #include <iostream>
00032
00033 namespace P {
00034
00036
00040 enum LogLevel {
00041 LOG_DEBUG = 0,
00042 LOG_INFO,
00043 LOG_NOTICE,
00044 LOG_WARNING,
00045 LOG_ERROR,
00046 LOG_CRITICAL,
00047 LOG_EMERGENCY
00048 };
00049
00051
00056 class PCORE_EXPORT Logger {
00057 public:
00058 Logger();
00059 virtual ~Logger();
00060
00061 inline void setLogLevel(LogLevel l)
00062 { m_level = l; }
00063
00064 inline LogLevel logLevel() const
00065 { return m_level; }
00066
00067 virtual void start(const std::string& path) = 0;
00068 virtual void stop() = 0;
00069 virtual void restart() = 0;
00070
00071 virtual bool isActive() const = 0;
00072
00073 virtual void output(const DateTime& t, LogLevel l, const std::string& ident, const std::string& subsys, const char* msg) = 0;
00074
00075 static std::string logLevel2Str(LogLevel l);
00076 static LogLevel str2LogLevel(const std::string& str);
00077
00078 private:
00079 LogLevel m_level;
00080 };
00081
00083
00092 class PCORE_EXPORT SystemLog: protected std::streambuf, public std::ostream
00093 {
00094 public:
00096
00099 SystemLog(const std::string& ident);
00100 ~SystemLog();
00101
00103 inline const std::string& ident() const
00104 { return m_ident; }
00105
00107 void addLogger(Logger* log);
00108
00110 bool removeLogger(Logger* log);
00111
00113 void restart();
00114
00116 void enableSubsys(const std::string& name);
00117
00119 void disableSubsys(const std::string& name);
00120
00122 SystemLog& operator()(const std::string& subsys, LogLevel l);
00123
00125 SystemLog& operator()(LogLevel l);
00126
00127 inline const std::list<Logger*> loggers() const
00128 { return m_loggers; }
00129
00130 protected:
00131 int overflow(int c);
00132
00133 private:
00134 struct LogState;
00135 LogState* state();
00136
00137 std::string m_ident;
00138 ThreadKey<LogState> m_state;
00139 CriticalSection m_lock;
00140 std::list<Logger*> m_loggers;
00141 std::set<std::string> m_disabled;
00142 };
00143
00149 }
00150
00151 #endif