rad  5.1.0
Log.h
Go to the documentation of this file.
1 
11 #ifndef SCXML4CPP_LOG_H
12 #define SCXML4CPP_LOG_H
13 
14 #include <log4cplus/logger.h>
15 #include <log4cplus/loggingmacros.h>
16 #include <log4cplus/configurator.h>
17 #include <log4cplus/version.h>
18 #if LOG4CPLUS_VERSION >= LOG4CPLUS_MAKE_VERSION(2,0,0)
19 #include <log4cplus/initializer.h>
20 #endif
21 #include <string>
22 
23 #ifdef NDEBUG
24 #define SCXML4CPP_LOG_TRACE()
25 #else
26 #define SCXML4CPP_LOG_TRACE() LOG4CPLUS_TRACE(scxml4cpp::GetLogger(), __FILE__ << " " << __FUNCTION__ << " " << __LINE__)
27 #endif
28 
29 #define SCXML4CPP_LOG_INFO(msg) LOG4CPLUS_INFO(scxml4cpp::GetLogger(), msg)
30 #define SCXML4CPP_LOG_DEBUG(msg) LOG4CPLUS_DEBUG(scxml4cpp::GetLogger(), msg)
31 #define SCXML4CPP_LOG_WARNING(msg) LOG4CPLUS_WARN(scxml4cpp::GetLogger(), msg)
32 #define SCXML4CPP_LOG_ERROR(msg) LOG4CPLUS_ERROR(scxml4cpp::GetLogger(), msg)
33 #define SCXML4CPP_LOG_FATAL(msg) LOG4CPLUS_FATAL(scxml4cpp::GetLogger(), msg)
34 
35 
36 namespace scxml4cpp {
37 
38 const std::string LOGGER_NAME = "scxml4cpp";
39 
46 void LogSetLevel(const std::string& levelName);
47 
51 log4cplus::Logger& GetLogger();
52 
64 public:
69  inline LogInitializer() {
70 #if LOG4CPLUS_VERSION < LOG4CPLUS_MAKE_VERSION(2,0,0)
71  log4cplus::initialize();
72 #endif
73  log4cplus::BasicConfigurator().configure();
74  GetLogger().setLogLevel(log4cplus::INFO_LOG_LEVEL);
75  }
76 
81  inline ~LogInitializer() {
82 #if LOG4CPLUS_VERSION < LOG4CPLUS_MAKE_VERSION(2,0,0)
83  log4cplus::Logger::shutdown();
84 #endif
85  }
86 
87  LogInitializer(const LogInitializer&) = delete;
89 
90 private:
91 #if LOG4CPLUS_VERSION >= LOG4CPLUS_MAKE_VERSION(2,0,0)
92  log4cplus::Initializer mInitializer;
93 #endif
94 };
95 
96 } // namespace scxml4cpp
97 
98 
99 /*
100  * Obsolete logging.
101  */
102 #if 0
103 #include <sstream>
104 #include <string>
105 #include <stdio.h>
106 #include <sys/time.h>
107 
108 namespace scxml4cpp {
109 /*
110  * Supported Log Levels.
111  */
112 const int LOG_LEVEL_ERROR = 0;
113 const int LOG_LEVEL_WARNING = 1;
114 const int LOG_LEVEL_INFO = 2;
115 const int LOG_LEVEL_DEBUG = 3;
116 const int LOG_LEVEL_DEBUG1 = 4;
117 const int LOG_LEVEL_DEBUG2 = 5;
118 const int LOG_LEVEL_DEBUG3 = 6;
119 const int LOG_LEVEL_DEBUG4 = 7;
120 const int LOG_LEVEL_TRACE = 8;
121 const int LOG_MAX_LEVEL = LOG_LEVEL_TRACE;
122 
123 inline std::string LogNowTime();
124 
125 /*
126  * Log class.
127  * typename T is the output policy: stderr, stdout, Output2File, etc.
128  */
129 template <typename T> class Log
130 {
131  public:
132  Log() {}
133 
137  virtual ~Log() {
138  os << std::endl;
139  T::Output(os.str());
140  }
141 
146  inline std::ostringstream& Get(const int level = LOG_LEVEL_INFO) {
147  os << LogNowTime();
148  os << " " << ToString(level) << " ";
149  return os;
150  }
151 
155  inline static int& ReportingLevel() {
156  static int reportingLevel = LOG_LEVEL_INFO;
157  return reportingLevel;
158  }
159 
163  inline static std::string ToString(const int level) {
164  static const char* const buffer[] = {
165  "ERROR",
166  "WARNING",
167  "INFO",
168  "DEBUG",
169  "DEBUG1",
170  "DEBUG2",
171  "DEBUG3",
172  "DEBUG4",
173  "TRACE"
174  };
175  return (level >= LOG_LEVEL_ERROR && level <= LOG_MAX_LEVEL) ? buffer[level] : "INFO";
176  }
177 
181  inline static int FromString(const std::string& level) {
182  if (level == "TRACE")
183  return LOG_LEVEL_TRACE;
184  if (level == "DEBUG4")
185  return LOG_LEVEL_DEBUG4;
186  if (level == "DEBUG3")
187  return LOG_LEVEL_DEBUG3;
188  if (level == "DEBUG2")
189  return LOG_LEVEL_DEBUG2;
190  if (level == "DEBUG1")
191  return LOG_LEVEL_DEBUG1;
192  if (level == "DEBUG")
193  return LOG_LEVEL_DEBUG;
194  if (level == "INFO")
195  return LOG_LEVEL_INFO;
196  if (level == "WARNING")
197  return LOG_LEVEL_WARNING;
198  if (level == "ERROR")
199  return LOG_LEVEL_ERROR;
200  Log<T>().Get(LOG_LEVEL_WARNING) << "Unknown logging level '" << level << "'. Using INFO level as default.";
201  return LOG_LEVEL_INFO;
202  }
203 
204  protected:
205  std::ostringstream os;
206 
207  private:
208  Log(const Log&);
209  Log& operator =(const Log&);
210 };
211 
215 class Output2FILE
216 {
217  public:
218  inline static FILE*& Stream() {
219  static FILE* pStream = stderr;
220  return pStream;
221  }
222 
223  static void Output(const std::string& msg) {
224  FILE* pStream = Stream();
225  if (!pStream) return;
226 
227  fprintf(pStream, "%s", msg.c_str());
228  fflush(pStream);
229  }
230 };
231 
235 inline void LogSetLevel(const std::string& levelName) {
236  Log<scxml4cpp::Output2FILE>::ReportingLevel() = Log<scxml4cpp::Output2FILE>::FromString(levelName);
237 }
238 
244 inline std::string LogNowTime()
245 {
246  char buffer[64];
247  time_t t;
248  time(&t);
249  tm r = {0};
250  strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", localtime_r(&t, &r));
251  struct timeval tv;
252  gettimeofday(&tv, 0);
253  char result[100] = {0};
254  sprintf(result, "%s.%06ld", buffer, (long)tv.tv_usec / 1000);
255  return result;
256 }
257 
258 } // namespace scxml4cpp
259 
263 #define SCXML4CPP_LOG(level) \
264  if (level > scxml4cpp::LOG_MAX_LEVEL) ; \
265  else if (level > scxml4cpp::Log<scxml4cpp::Output2FILE>::ReportingLevel() || !scxml4cpp::Output2FILE::Stream()) ; \
266  else scxml4cpp::Log<scxml4cpp::Output2FILE>().Get(level) << "scxml4cpp " << __FILE__ << " " << __FUNCTION__ << " " << __LINE__ << " "
267 
268 #define SCXML4CPP_LOG_TRACE() SCXML4CPP_LOG(scxml4cpp::LOG_LEVEL_TRACE)
269 #define SCXML4CPP_LOG_DEBUG() SCXML4CPP_LOG(scxml4cpp::LOG_LEVEL_DEBUG)
270 #define SCXML4CPP_LOG_DEBUG1() SCXML4CPP_LOG(scxml4cpp::LOG_LEVEL_DEBUG1)
271 #define SCXML4CPP_LOG_DEBUG2() SCXML4CPP_LOG(scxml4cpp::LOG_LEVEL_DEBUG2)
272 #define SCXML4CPP_LOG_DEBUG3() SCXML4CPP_LOG(scxml4cpp::LOG_LEVEL_DEBUG3)
273 #define SCXML4CPP_LOG_DEBUG4() SCXML4CPP_LOG(scxml4cpp::LOG_LEVEL_DEBUG4)
274 #define SCXML4CPP_LOG_INFO() SCXML4CPP_LOG(scxml4cpp::LOG_LEVEL_INFO)
275 #define SCXML4CPP_LOG_WARNING() SCXML4CPP_LOG(scxml4cpp::LOG_LEVEL_WARNING)
276 #define SCXML4CPP_LOG_ERROR() SCXML4CPP_LOG(scxml4cpp::LOG_LEVEL_ERROR)
277 #endif
278 
279 #endif //SCXML4CPP_LOG_H
Definition: Log.h:63
LogInitializer()
Definition: Log.h:69
LogInitializer & operator=(const LogInitializer &)=delete
~LogInitializer()
Definition: Log.h:81
LogInitializer(const LogInitializer &)=delete
level
Definition: hellorad.py:30
const int LOG_MAX_LEVEL
Definition: logger.hpp:215
@ LOG_LEVEL_DEBUG
Definition: logger.hpp:207
@ LOG_LEVEL_DEBUG3
Definition: logger.hpp:210
@ LOG_LEVEL_INFO
Definition: logger.hpp:206
@ LOG_LEVEL_DEBUG2
Definition: logger.hpp:209
@ LOG_LEVEL_TRACE
Definition: logger.hpp:212
@ LOG_LEVEL_WARNING
Definition: logger.hpp:201
@ LOG_LEVEL_DEBUG4
Definition: logger.hpp:211
@ LOG_LEVEL_DEBUG1
Definition: logger.hpp:208
@ LOG_LEVEL_ERROR
Definition: logger.hpp:200
Definition: Action.h:40
log4cplus::Logger & GetLogger()
Definition: Log.cpp:46
void LogSetLevel(const std::string &levelName)
logger used by scxml4cpp library
Definition: Log.cpp:38
const std::string LOGGER_NAME
Definition: Log.h:38