00001 #ifndef Logger_HH
00002 #define Logger_HH
00003
00004 #include <syslog.h>
00005 #include <pthread.h>
00006
00007 #include <sstream>
00008 #include <string>
00009
00010 #include <map>
00011 #include <vector>
00012
00013 #define SSTR(message) static_cast<std::ostringstream&>(std::ostringstream().flush() << message).str()
00014
00015 #define Log(lvl, mymask, where, what) \
00016 do{ \
00017 if (Logger::get()->getLevel() >= lvl && Logger::get()->isLogged(mymask)) \
00018 { \
00019 std::ostringstream outs; \
00020 outs << "{" << pthread_self() << "}" << "[" << lvl << "] dmlite " << where << " " << __func__ << " : " << what; \
00021 Logger::get()->log((Logger::Level)lvl, outs.str()); \
00022 } \
00023 }while(0) \
00024
00025
00026 #define Err(where, what) \
00027 do{ \
00028 std::ostringstream outs; \
00029 outs << "{" << pthread_self() << "}" << "!!! dmlite " << where << __func__ << " : " << what; \
00030 Logger::get()->log((Logger::Level)0, outs.str()); \
00031 }while(0)
00032
00033
00034
00035
00036 class Logger
00037 {
00038
00039 public:
00040
00041 typedef unsigned long long bitmask;
00042
00043 typedef std::string component;
00044
00045 static bitmask unregistered;
00046 static char *unregisteredname;
00047
00048
00049
00050 enum Level
00051 {
00052 Lvl0,
00053 Lvl1,
00054 Lvl2,
00055 Lvl3,
00056 Lvl4,
00057 Lvl5
00058 };
00059
00060
00061 ~Logger();
00062
00063 static Logger *instance;
00064
00065
00066 static Logger *get()
00067 {
00068 if (instance == 0)
00069 instance = new Logger();
00070 return instance;
00071 }
00072
00073 static void set(Logger *inst) {
00074 Logger *old = instance;
00075 instance = inst;
00076 if (old) delete old;
00077 }
00078
00079 short getLevel() const
00080 {
00081 return level;
00082 }
00083
00084
00085 void setLevel(Level lvl)
00086 {
00087 level = lvl;
00088 }
00089
00090
00091 bool isLogged(bitmask m) const
00092 {
00093 if (mask == 0) return mask & unregistered;
00094 return mask & m;
00095 }
00096
00097
00098 void registerComponent(component const & comp);
00099
00100
00101 void registerComponents(std::vector<component> const & components);
00102
00103
00104
00105
00106 void setLogged(component const &comp, bool tobelogged);
00107
00108
00109
00110
00111
00112
00113
00114
00115 void log(Level lvl, std::string const & msg) const;
00116
00117
00118
00119
00120
00121 void logAll()
00122 {
00123 mask = ~0;
00124 }
00125
00126
00127
00128
00129
00130
00131 bitmask getMask(component const & comp);
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141 static int getStackTrace(std::string &s);
00142
00143
00144 private:
00145
00146
00147 Logger();
00148
00149 Logger(Logger const &);
00150
00151 Logger & operator=(Logger const &);
00152
00153
00154 short level;
00155
00156 int size;
00157
00158 bitmask mask;
00159
00160 std::map<component, bitmask> mapping;
00161
00162
00163
00164 };
00165
00166
00167
00168 void LogCfgParm(int lvl, Logger::bitmask mymask, std::string where, std::string key, std::string value);
00169
00170
00171
00172
00173 #endif