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