/home/fwph/code/wurde/rde/core/Logger.H

Go to the documentation of this file.
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 // $Id: Logger.H 68 2007-02-01 17:56:57Z fwph $
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                 //             Logger & operator <<(ostringstream & right);
00072                 //             Logger & operator <<(string right);
00073                 
00077                 /*             void log(){
00078                                info(m_stream.str());
00079                                }*/
00080                 
00084                 /*             std::stringstream& operator<<(const std::string& s){
00085                                m_stream.str(std::string());
00086                                m_stream << s;
00087                                return m_stream;
00088                                }*/
00089                 
00094                 //             std::ostream &flush();
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                 /* Gives the output file, and whether or not to log to one. Default is no.
00184                    Currently have not decided how to output the data, or how to deal with format strings.
00185                    There will be a sensible default. */
00186                 
00187                 std::string m_outFilename;
00188                 bool m_toFile;
00189                 
00190                 /* Specifies whether to log to the network. Default is yes. */
00191                 bool m_toNetwork;
00192                 
00193                 /* Specifies whether to log to syslog. Default is yes. Data arrays will *not* be sent, only pure strings. */
00194                 bool m_toSyslog;
00195                 
00196                 /* Specifies whether to have syslog also send to stderr. Default is yes */
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         /*       inline void theCallback(std::ios_base::event ev, std::ios_base& ios, int index){
00259                  if(ev==std::ios_base::copyfmt_event){
00260                  if(g_robotLoggerPtr){
00261                  g_robotLoggerPtr->log();
00262                  }
00263                  }
00264               }*/
00265         
00266         class LoggerStreamBuf : 
00267                 public std::basic_streambuf<char, std::char_traits<char> >{
00268         private:
00269                 //            std::vector<char> m_buffer;
00270                 std::stringstream m_out;           
00271                 int m_priority;
00272                 std::string m_buffer;
00273                 std::vector<char> m_vector;
00274                 //typedef typename std::basic_streambuf<charT, traits>::int_type int_type;
00275                 //std::basic_streambuf<charT, traits>* out_;
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                                 //                          m_buffer += (char) c;
00284                                 //                          m_buffer.push_back(c);
00285                                 //                          m_out.put(c);
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                         //                          m_out.sputn(&buffer_[0], static_cast<std::streamsize>(m_buffer.size()));
00300                         // m_buffer.clear();
00301                         if(m_vector.size()>0){
00302                                 std::vector<char>::iterator iter;
00303                                 //                          m_vector.push_back('\0');
00304                                 for(iter=m_vector.begin();iter!=m_vector.end();iter++){
00305                                         m_buffer+=*iter;
00306                                 }
00307                                 //                          iter=m_vector.begin();
00308                                 //m_buffer=&(*iter);
00309                                 // m_out.flush();
00310                                 g_log(m_buffer,m_priority);
00311                                 //  delete m_out;
00312                                 // m_out = new std::stringstream;
00313                                 //m_out.str(std::string());
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                         //char buf[num+1];
00327                         //memcpy(buf,s,num);
00328                         //buf[num]='\0';
00329                         //                   m_buffer += buf;
00330                         // m_out << s;
00331                         //                   m_out.write(s,num);
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                 //            public std::ostream<char, std::char_traits<char> >{
00372         public:
00373                 LoggerStream(int priority) :
00374                         LoggerStreamBuf_init(priority),
00375                         std::ostream(LoggerStreamBuf_init::buf()) { }
00376                 //                   std::basic_ostream<char, std::char_traits<char> >(LoggerStreamBuf_init::buf()) { }
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

Generated on Thu Feb 1 15:31:54 2007 for WURDE by  doxygen 1.5.1