DDS  ver. 3.4
Log.h
Go to the documentation of this file.
1 // Copyright 2014 GSI, Inc. All rights reserved.
2 //
3 // Log engine core.
4 //
5 #ifndef CLOG_H
6 #define CLOG_H
7 
8 // API
9 #include <sys/time.h>
10 // STD
11 #include <ctime>
12 #include <fstream>
13 #include <iostream>
14 #include <sstream>
15 // STL
16 #include <string>
17 // MiscCommon
18 #include "Res.h"
19 #include "SysHelper.h"
20 #include "def.h"
21 
22 namespace MiscCommon
23 {
29  typedef enum ESeverity
30  {
36  } LOG_SEVERITY;
37  enum
38  {
40  e_WhiteSpace = 0x20
41  };
49  template <typename _T>
50  class CLog
51  {
52  public:
53  CLog(_T* _stream, unsigned char _logLevel)
54  : m_stream(_stream)
55  , m_logLevel(_logLevel)
56  {
57  }
58 
59  void push(LOG_SEVERITY _Severity,
60  unsigned long _ErrorCode,
61  const std::string& _Module,
62  const std::string& _Message)
63  {
64  if ((_Severity & m_logLevel) != _Severity)
65  return;
66 
67  // Thread ID
68  pid_t tid = gettid();
69  std::string _DTBuff;
70  std::stringstream strMsg;
71  strMsg << GetCurTimeString(&_DTBuff) << char(e_FieldSeparator) << GetSeverityString(_Severity)
72  << char(e_FieldSeparator) << GetErrorCode(_ErrorCode) << char(e_FieldSeparator) << "[" << _Module
73  << ":thread-" << tid << "]" << char(e_FieldSeparator) << _Message;
74 
75  smart_mutex m(m_mutex);
76  if (m_stream && m_stream->good())
77  {
78  *m_stream << strMsg.str() << std::endl;
79  m_stream->flush();
80  }
81  else
82  {
83  std::cout << strMsg.str() << std::endl;
84  }
85  }
86 
87  private:
88  std::string& GetCurTimeString(std::string* _Buf)
89  {
90  if (!_Buf)
91  return *_Buf;
92 
93  // Obtain the time of day, and convert it to a tm struct.
94  timeval tv;
95  gettimeofday(&tv, NULL);
96  tm* tm_now(localtime(&tv.tv_sec));
98  strftime(&buff[0], sizeof(char) * LOG_DATETIME_BUFF_LEN, g_cszLOG_DATETIME_FRMT, tm_now);
99  const long milliseconds = tv.tv_usec / 1000;
100  *_Buf = &buff[0];
101  std::stringstream ss;
102  ss << "." << milliseconds;
103  *_Buf += ss.str();
104  return *_Buf;
105  }
106 
107  const std::string GetSeverityString(LOG_SEVERITY _Severity) const
108  {
109  switch (_Severity)
110  {
111  case LOG_SEVERITY_INFO:
112  return g_cszLOG_SEVERITY_INFO;
115  case LOG_SEVERITY_FAULT:
119  default:
121  }
122  }
123 
124  std::string GetErrorCode(unsigned long _ErrorCode) const
125  {
126  std::stringstream sErrCode;
127  sErrCode << _ErrorCode;
128  return sErrCode.str();
129  }
130 
131  private:
132  _T* m_stream;
133  CMutex m_mutex;
134  unsigned char m_logLevel;
135  };
148  class CFileLog : public CLog<std::ofstream>
149  {
150  public:
151  typedef std::ofstream stream_type;
152 
153  public:
154  CFileLog(const std::string& _LogFileName,
155  bool _CreateNew = false,
156  unsigned char _logLevel = LOG_SEVERITY_INFO | LOG_SEVERITY_WARNING | LOG_SEVERITY_FAULT |
158  : CLog<stream_type>(&m_log_file, _logLevel)
159  , m_log_file(_LogFileName.c_str(), (_CreateNew ? std::ios::trunc : std::ios::app) | std::ios::out)
160  {
161  }
162 
163  private:
164  stream_type m_log_file;
165  };
166 }; // namespace MiscCommon
167 #endif
const size_t LOG_DATETIME_BUFF_LEN(25)
Logging to a file.
Definition: Log.h:148
const LPCSTR g_cszLOG_SEVERITY_CRITICAL_ERROR("FLT")
const LPCSTR g_cszLOG_SEVERITY_INFO("INF")
A smart CMutex helper.
Definition: SysHelper.h:275
Definition: Log.h:35
const LPCSTR g_cszLOG_SEVERITY_DEBUG("DBG")
const LPCSTR g_cszLOG_DATETIME_FRMT("%Y-%m-%d %H:%M:%S")
Log Date/Time format.
enum MiscCommon::ESeverity LOG_SEVERITY
Log's severity's constants.
const LPCSTR g_cszLOG_SEVERITY_WARNING("WRN")
unsigned long gettid()
A system helper, which helps to get a Thread ID of the current thread.
Definition: SysHelper.h:197
std::vector< char > CHARVector_t
An STL vector of char(s).
Definition: def.h:121
#define _T(s)
Use TCHAR instead of char or wchar_t. It will be appropriately translated.
Definition: def.h:85
Definition: Log.h:33
CLog(_T *_stream, unsigned char _logLevel)
Definition: Log.h:53
CFileLog(const std::string &_LogFileName, bool _CreateNew=false, unsigned char _logLevel=LOG_SEVERITY_INFO|LOG_SEVERITY_WARNING|LOG_SEVERITY_FAULT|LOG_SEVERITY_CRITICAL_ERROR)
Definition: Log.h:154
Definition: Log.h:31
void push(LOG_SEVERITY _Severity, unsigned long _ErrorCode, const std::string &_Module, const std::string &_Message)
Definition: Log.h:59
const LPCSTR g_cszLOG_SEVERITY_FAULT("ERR")
CLog< std::ostream > CSTDOutLog
ostream specialization of CLog.
Definition: Log.h:141
std::ofstream stream_type
Definition: Log.h:151
Definition: Log.h:39
ESeverity
Log's severity's constants.
Definition: Log.h:29
A simple template class which represents the Log engine of library.
Definition: Log.h:50
Miscellaneous functions and helpers are located here.
Definition: BOOST_FILESYSTEM.h:21
Definition: Log.h:40