rad  2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Log.h
Go to the documentation of this file.
1 /*
2  * Logging facility described by Petru Marginean at:
3  * http://www.drdobbs.com/cpp/logging-in-c/201804215
4  *
5  * Code adapted from
6  * ftp://ftp.drdobbs.com/sourcecode/ddj/2007/0710.zip
7  */
8 
9 /*
10  * $Id: Log.h 1139 2016-07-20 14:57:28Z landolfa $
11  */
12 
13 #ifndef SCXML4CPP_LOG_H
14 #define SCXML4CPP_LOG_H
15 
16 #include <log4cplus/logger.h>
17 #include <log4cplus/loggingmacros.h>
18 #include <string>
19 
20 #ifdef NDEBUG
21 #define SCXML4CPP_LOG_TRACE()
22 #else
23 #define SCXML4CPP_LOG_TRACE() LOG4CPLUS_TRACE(scxml4cpp::GetLogger(), __FILE__ << " " << __FUNCTION__ << " " << __LINE__)
24 #endif
25 
26 #define SCXML4CPP_LOG_INFO(msg) LOG4CPLUS_INFO(scxml4cpp::GetLogger(), msg)
27 #define SCXML4CPP_LOG_DEBUG(msg) LOG4CPLUS_DEBUG(scxml4cpp::GetLogger(), msg)
28 #define SCXML4CPP_LOG_WARNING(msg) LOG4CPLUS_WARN(scxml4cpp::GetLogger(), msg)
29 #define SCXML4CPP_LOG_ERROR(msg) LOG4CPLUS_ERROR(scxml4cpp::GetLogger(), msg)
30 #define SCXML4CPP_LOG_FATAL(msg) LOG4CPLUS_FATAL(scxml4cpp::GetLogger(), msg)
31 
32 
33 namespace scxml4cpp {
34 
35 const std::string LOGGER_NAME = "scxml4cpp";
36 
37 void LogInitialize();
38 void LogSetLevel(const std::string& levelName);
39 log4cplus::Logger& GetLogger();
40 
41 } // namespace scxml4cpp
42 
43 
44 /*
45  * Obsolete logging.
46  */
47 #if 0
48 #include <sstream>
49 #include <string>
50 #include <stdio.h>
51 #include <sys/time.h>
52 
53 namespace scxml4cpp {
54 /*
55  * Supported Log Levels.
56  */
57 const int LOG_LEVEL_ERROR = 0;
58 const int LOG_LEVEL_WARNING = 1;
59 const int LOG_LEVEL_INFO = 2;
60 const int LOG_LEVEL_DEBUG = 3;
61 const int LOG_LEVEL_DEBUG1 = 4;
62 const int LOG_LEVEL_DEBUG2 = 5;
63 const int LOG_LEVEL_DEBUG3 = 6;
64 const int LOG_LEVEL_DEBUG4 = 7;
65 const int LOG_LEVEL_TRACE = 8;
66 const int LOG_MAX_LEVEL = LOG_LEVEL_TRACE;
67 
68 inline std::string LogNowTime();
69 
70 /*
71  * Log class.
72  * typename T is the output policy: stderr, stdout, Output2File, etc.
73  */
74 template <typename T> class Log
75 {
76  public:
77  Log() {}
78 
82  virtual ~Log() {
83  os << std::endl;
84  T::Output(os.str());
85  }
86 
91  inline std::ostringstream& Get(const int level = LOG_LEVEL_INFO) {
92  os << LogNowTime();
93  os << " " << ToString(level) << " ";
94  return os;
95  }
96 
100  inline static int& ReportingLevel() {
101  static int reportingLevel = LOG_LEVEL_INFO;
102  return reportingLevel;
103  }
104 
108  inline static std::string ToString(const int level) {
109  static const char* const buffer[] = {
110  "ERROR",
111  "WARNING",
112  "INFO",
113  "DEBUG",
114  "DEBUG1",
115  "DEBUG2",
116  "DEBUG3",
117  "DEBUG4",
118  "TRACE"
119  };
120  return (level >= LOG_LEVEL_ERROR && level <= LOG_MAX_LEVEL) ? buffer[level] : "INFO";
121  }
122 
126  inline static int FromString(const std::string& level) {
127  if (level == "TRACE")
128  return LOG_LEVEL_TRACE;
129  if (level == "DEBUG4")
130  return LOG_LEVEL_DEBUG4;
131  if (level == "DEBUG3")
132  return LOG_LEVEL_DEBUG3;
133  if (level == "DEBUG2")
134  return LOG_LEVEL_DEBUG2;
135  if (level == "DEBUG1")
136  return LOG_LEVEL_DEBUG1;
137  if (level == "DEBUG")
138  return LOG_LEVEL_DEBUG;
139  if (level == "INFO")
140  return LOG_LEVEL_INFO;
141  if (level == "WARNING")
142  return LOG_LEVEL_WARNING;
143  if (level == "ERROR")
144  return LOG_LEVEL_ERROR;
145  Log<T>().Get(LOG_LEVEL_WARNING) << "Unknown logging level '" << level << "'. Using INFO level as default.";
146  return LOG_LEVEL_INFO;
147  }
148 
149  protected:
150  std::ostringstream os;
151 
152  private:
153  Log(const Log&);
154  Log& operator =(const Log&);
155 };
156 
160 class Output2FILE
161 {
162  public:
163  inline static FILE*& Stream() {
164  static FILE* pStream = stderr;
165  return pStream;
166  }
167 
168  static void Output(const std::string& msg) {
169  FILE* pStream = Stream();
170  if (!pStream) return;
171 
172  fprintf(pStream, "%s", msg.c_str());
173  fflush(pStream);
174  }
175 };
176 
180 inline void LogSetLevel(const std::string& levelName) {
181  Log<scxml4cpp::Output2FILE>::ReportingLevel() = Log<scxml4cpp::Output2FILE>::FromString(levelName);
182 }
183 
189 inline std::string LogNowTime()
190 {
191  char buffer[64];
192  time_t t;
193  time(&t);
194  tm r = {0};
195  strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", localtime_r(&t, &r));
196  struct timeval tv;
197  gettimeofday(&tv, 0);
198  char result[100] = {0};
199  sprintf(result, "%s.%06ld", buffer, (long)tv.tv_usec / 1000);
200  return result;
201 }
202 
203 } // namespace scxml4cpp
204 
208 #define SCXML4CPP_LOG(level) \
209  if (level > scxml4cpp::LOG_MAX_LEVEL) ; \
210  else if (level > scxml4cpp::Log<scxml4cpp::Output2FILE>::ReportingLevel() || !scxml4cpp::Output2FILE::Stream()) ; \
211  else scxml4cpp::Log<scxml4cpp::Output2FILE>().Get(level) << "scxml4cpp " << __FILE__ << " " << __FUNCTION__ << " " << __LINE__ << " "
212 
213 #define SCXML4CPP_LOG_TRACE() SCXML4CPP_LOG(scxml4cpp::LOG_LEVEL_TRACE)
214 #define SCXML4CPP_LOG_DEBUG() SCXML4CPP_LOG(scxml4cpp::LOG_LEVEL_DEBUG)
215 #define SCXML4CPP_LOG_DEBUG1() SCXML4CPP_LOG(scxml4cpp::LOG_LEVEL_DEBUG1)
216 #define SCXML4CPP_LOG_DEBUG2() SCXML4CPP_LOG(scxml4cpp::LOG_LEVEL_DEBUG2)
217 #define SCXML4CPP_LOG_DEBUG3() SCXML4CPP_LOG(scxml4cpp::LOG_LEVEL_DEBUG3)
218 #define SCXML4CPP_LOG_DEBUG4() SCXML4CPP_LOG(scxml4cpp::LOG_LEVEL_DEBUG4)
219 #define SCXML4CPP_LOG_INFO() SCXML4CPP_LOG(scxml4cpp::LOG_LEVEL_INFO)
220 #define SCXML4CPP_LOG_WARNING() SCXML4CPP_LOG(scxml4cpp::LOG_LEVEL_WARNING)
221 #define SCXML4CPP_LOG_ERROR() SCXML4CPP_LOG(scxml4cpp::LOG_LEVEL_ERROR)
222 #endif
223 
224 #endif //SCXML4CPP_LOG_H
log4cplus::Logger & GetLogger()
Definition: Log.cpp:46
Definition: logger.hpp:103
const int LOG_MAX_LEVEL
Definition: logger.hpp:118
void LogSetLevel(const std::string &levelName)
Definition: Log.cpp:38
Definition: logger.hpp:109
void LogInitialize()
Definition: Log.cpp:33
const std::string LOGGER_NAME
Definition: Log.h:35
Definition: logger.hpp:111
Definition: logger.hpp:112
Definition: logger.hpp:110
Definition: logger.hpp:115
Definition: logger.hpp:104
Definition: logger.hpp:113
Definition: logger.hpp:114