Whiteboard
An interface and tools for visualizing large and complex datasets
log.h
Go to the documentation of this file.
1 #ifndef __LOG_H__
2 #define __LOG_H__
3 
4 #include <sstream>
5 #include <string>
6 #include <stdio.h>
7 
8 inline std::string NowTime();
9 
11 
12 template <typename T>
13 class Log
14 {
15 public:
16  Log();
17  virtual ~Log();
18  std::ostringstream& Get(TLogLevel level = logINFO);
19 public:
20  static TLogLevel& ReportingLevel();
21  static std::string ToString(TLogLevel level);
22  static TLogLevel FromString(const std::string& level);
23 protected:
24  std::ostringstream os;
25 private:
26  Log(const Log&);
27  Log& operator =(const Log&);
28 };
29 
30 template <typename T>
32 {
33 }
34 
35 template <typename T>
36 std::ostringstream& Log<T>::Get(TLogLevel level)
37 {
38  os << "- " << NowTime();
39  os << " " << ToString(level) << ": ";
40  os << std::string(level > logDEBUG ? level - logDEBUG : 0, '\t');
41  return os;
42 }
43 
44 template <typename T>
46 {
47  os << std::endl;
48  T::Output(os.str());
49 }
50 
51 template <typename T>
53 {
54  static TLogLevel reportingLevel = logDEBUG4;
55  return reportingLevel;
56 }
57 
58 template <typename T>
59 std::string Log<T>::ToString(TLogLevel level)
60 {
61  static const char* const buffer[] = {"ERROR", "WARNING", "INFO", "DEBUG", "DEBUG1", "DEBUG2", "DEBUG3", "DEBUG4"};
62  return buffer[level];
63 }
64 
65 template <typename T>
66 TLogLevel Log<T>::FromString(const std::string& level)
67 {
68  if (level == "DEBUG4")
69  return logDEBUG4;
70  if (level == "DEBUG3")
71  return logDEBUG3;
72  if (level == "DEBUG2")
73  return logDEBUG2;
74  if (level == "DEBUG1")
75  return logDEBUG1;
76  if (level == "DEBUG")
77  return logDEBUG;
78  if (level == "INFO")
79  return logINFO;
80  if (level == "WARNING")
81  return logWARNING;
82  if (level == "ERROR")
83  return logERROR;
84  Log<T>().Get(logWARNING) << "Unknown logging level '" << level << "'. Using INFO level as default.";
85  return logINFO;
86 }
87 
89 {
90 public:
91  static FILE*& Stream();
92  static void Output(const std::string& msg);
93 };
94 
95 inline FILE*& Output2FILE::Stream()
96 {
97  static FILE* pStream = stderr;
98  return pStream;
99 }
100 
101 inline void Output2FILE::Output(const std::string& msg)
102 {
103  FILE* pStream = Stream();
104  if (!pStream)
105  return;
106  fprintf(pStream, "%s", msg.c_str());
107  fflush(pStream);
108 }
109 
110 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
111 # if defined (BUILDING_FILELOG_DLL)
112 # define FILELOG_DECLSPEC __declspec (dllexport)
113 # elif defined (USING_FILELOG_DLL)
114 # define FILELOG_DECLSPEC __declspec (dllimport)
115 # else
116 # define FILELOG_DECLSPEC
117 # endif // BUILDING_DBSIMPLE_DLL
118 #else
119 # define FILELOG_DECLSPEC
120 #endif // _WIN32
121 
122 class FILELOG_DECLSPEC FILELog : public Log<Output2FILE> {};
123 //typedef Log<Output2FILE> FILELog;
124 
125 #ifndef FILELOG_MAX_LEVEL
126 #define FILELOG_MAX_LEVEL logDEBUG4
127 #endif
128 
129 #define FILE_LOG(level) \
130  if (level > FILELOG_MAX_LEVEL) ;\
131  else if (level > FILELog::ReportingLevel() || !Output2FILE::Stream()) ; \
132  else FILELog().Get(level)
133 
134 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
135 
136 #include <windows.h>
137 
138 inline std::string NowTime()
139 {
140  const int MAX_LEN = 200;
141  char buffer[MAX_LEN];
142  if (GetTimeFormatA(LOCALE_USER_DEFAULT, 0, 0,
143  "HH':'mm':'ss", buffer, MAX_LEN) == 0)
144  return "Error in NowTime()";
145 
146  char result[100] = {0};
147  static DWORD first = GetTickCount();
148  std::sprintf(result, "%s.%03ld", buffer, (long)(GetTickCount() - first) % 1000);
149  return result;
150 }
151 
152 #else
153 
154 #include <sys/time.h>
155 
156 inline std::string NowTime()
157 {
158  char buffer[11];
159  time_t t;
160  time(&t);
161  tm r = tm();
162  strftime(buffer, sizeof(buffer), "%X", localtime_r(&t, &r));
163  struct timeval tv;
164  gettimeofday(&tv, 0);
165  char result[100] = {0};
166  sprintf(result, "%s.%03ld", buffer, (long)tv.tv_usec / 1000);
167  return result;
168 }
169 
170 #endif //WIN32
171 
172 #endif //__LOG_H__
static FILE *& Stream()
Definition: log.h:95
Definition: log.h:10
Definition: log.h:10
static void Output(const std::string &msg)
Definition: log.h:101
std::string NowTime()
Definition: log.h:156
Log()
Definition: log.h:31
std::ostringstream & Get(TLogLevel level=logINFO)
Definition: log.h:36
Definition: log.h:10
Definition: log.h:10
virtual ~Log()
Definition: log.h:45
static TLogLevel FromString(const std::string &level)
Definition: log.h:66
Definition: log.h:88
Definition: log.h:10
#define FILELOG_DECLSPEC
Definition: log.h:119
static TLogLevel & ReportingLevel()
Definition: log.h:52
static std::string ToString(TLogLevel level)
Definition: log.h:59
std::ostringstream os
Definition: log.h:24
Log & operator=(const Log &)
TLogLevel
Definition: log.h:10
Definition: log.h:122
Definition: log.h:13
Definition: log.h:10
Definition: log.h:10
Definition: log.h:10