00001 #ifndef _LOGGER_H
00002 #define _LOGGER_H
00003
00029 #include <Capability.H>
00030 #include <LoggerTransport.H>
00031 #include <queue>
00032 #include <syslog.h>
00033 #include <sstream>
00034 #include <streambuf>
00035 #include <iostream>
00036 #include <vector>
00037 #include <pthread.h>
00038
00039
00040
00041 #define LOGGER "Logger"
00042
00043 namespace WURDE {
00044
00045
00046 const char *priorityToString(int priority);
00047
00053 class Logger : public LoggerTransport{
00054
00055 public:
00059 Logger(std::string somename);
00060
00065 Logger(std::string somename, std::string filename);
00066
00067 ~Logger(){};
00068
00069 void initLogger();
00070
00071
00072
00073
00077
00078
00079
00080
00084
00085
00086
00087
00088
00089
00094
00095
00121 void log(std::string somestring, int priority);
00122
00125 void debug(std::string somestring) { log(somestring, LOG_DEBUG); };
00126
00129 void info(std::string somestring) { log(somestring, LOG_INFO);};
00130
00133 void notice(std::string somestring) { log(somestring, LOG_NOTICE);};
00134
00137 void warn(std::string somestring) { log(somestring, LOG_WARNING);};
00138
00141 void error(std::string somestring) { log(somestring,LOG_ERR);};
00142
00143
00146 void fatal(std::string somestring) { log(somestring,LOG_CRIT);};
00147
00151 void log(std::string somestring) { log(somestring,LOG_INFO); };
00152
00156 void setNetworkLogging(bool value);
00157
00161 void setSysLogging(bool value);
00162
00166 void setStderr(bool value);
00167
00171 void logData(std::string description, std::string format, std::string data);
00172
00177 void setLogLevel(int priority){m_logLevel=priority;}
00178
00179 private:
00180 void init(std::string somename);
00181
00182
00183
00184
00185
00186
00187 std::string m_outFilename;
00188 bool m_toFile;
00189
00190
00191 bool m_toNetwork;
00192
00193
00194 bool m_toSyslog;
00195
00196
00197 bool m_toStderr;
00198
00199 int m_logLevel;
00200
00201 bool m_init;
00202 std::string m_logName;
00203
00204 pthread_mutex_t mutex;
00205 };
00206
00213 extern Logger *g_robotLoggerPtr;
00214
00220 inline void g_log(std::string logString, int priority){
00221 if(g_robotLoggerPtr){
00222 g_robotLoggerPtr->log(logString,priority);
00223 }else{
00224 std::cerr << priorityToString(priority) << logString;
00225 }
00226 }
00227
00231 inline void g_debug(std::string logString){g_log(logString,LOG_DEBUG);};
00232
00236 inline void g_info(std::string logString){g_log(logString,LOG_INFO);};
00237
00241 inline void g_notice(std::string logString){g_log(logString,LOG_NOTICE);};
00242
00246 inline void g_warn(std::string logString){g_log(logString,LOG_WARNING);};
00247
00251 inline void g_error(std::string logString){g_log(logString,LOG_ERR);};
00252
00256 inline void g_fatal(std::string logString){g_log(logString,LOG_CRIT);};
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266 class LoggerStreamBuf :
00267 public std::basic_streambuf<char, std::char_traits<char> >{
00268 private:
00269
00270 std::stringstream m_out;
00271 int m_priority;
00272 std::string m_buffer;
00273 std::vector<char> m_vector;
00274
00275
00276 pthread_mutex_t mutex;
00277 public:
00278
00279 virtual int overflow(int c){
00280 pthread_mutex_lock(&mutex);
00281 if(std::char_traits<char>::not_eof(c)){
00282 m_vector.push_back(c);
00283
00284
00285
00286 }
00287 pthread_mutex_unlock(&mutex);
00288 return std::char_traits<char>::not_eof(c);
00289 }
00290
00291 LoggerStreamBuf(int priority){
00292 m_priority=priority;
00293 m_buffer= " ";
00294 mutex=(pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
00295 }
00296
00297 virtual int sync(){
00298 pthread_mutex_lock(&mutex);
00299
00300
00301 if(m_vector.size()>0){
00302 std::vector<char>::iterator iter;
00303
00304 for(iter=m_vector.begin();iter!=m_vector.end();iter++){
00305 m_buffer+=*iter;
00306 }
00307
00308
00309
00310 g_log(m_buffer,m_priority);
00311
00312
00313
00314 m_buffer=" ";
00315 m_vector.clear();
00316 }
00317 pthread_mutex_unlock(&mutex);
00318 return 0;
00319 }
00320
00321 ~LoggerStreamBuf(){
00322 sync();
00323 }
00324
00325 virtual std::streamsize xsputn(const char* s, std::streamsize num){
00326
00327
00328
00329
00330
00331
00332 pthread_mutex_lock(&mutex);
00333 std::copy(s, s+num,std::back_inserter<std::vector<char> >(m_vector));
00334 pthread_mutex_unlock(&mutex);
00335 return num;
00336 }
00337 };
00338
00339 class LoggerStringBuf : virtual public std::stringbuf{
00340 public:
00341 LoggerStringBuf(int priority){
00342 m_priority=priority;
00343 }
00344 ~LoggerStringBuf(){sync();}
00345
00346 virtual int sync(){
00347 g_log(str(),m_priority);
00348 str(std::string());
00349 return 0;
00350 }
00351
00352 private:
00353 int m_priority;
00354 };
00355
00356 class LoggerStreamBuf_init{
00357 private:
00358 LoggerStreamBuf buf_;
00359 public:
00360 LoggerStreamBuf_init(int priority):
00361 buf_(priority){}
00362 LoggerStreamBuf* buf()
00363 {
00364 return &buf_;
00365 }
00366 };
00367
00368 class LoggerStream :
00369 private LoggerStreamBuf_init,
00370 public std::ostream{
00371
00372 public:
00373 LoggerStream(int priority) :
00374 LoggerStreamBuf_init(priority),
00375 std::ostream(LoggerStreamBuf_init::buf()) { }
00376
00377 };
00378
00379 extern LoggerStream g_logdebug;
00380 extern LoggerStream g_loginfo;
00381 extern LoggerStream g_lognotice;
00382 extern LoggerStream g_logwarn;
00383 extern LoggerStream g_logerror;
00384 extern LoggerStream g_logfatal;
00385
00386 }
00387
00388
00389 #endif