DDS  ver. 1.6
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 {
35  } LOG_SEVERITY;
36  enum
37  {
39  e_WhiteSpace = 0x20
40  };
48  template <typename _T>
49  class CLog
50  {
51  public:
52  CLog(_T* _stream, unsigned char _logLevel)
53  : m_stream(_stream)
54  , m_logLevel(_logLevel)
55  {
56  }
57 
58  void push(LOG_SEVERITY _Severity,
59  unsigned long _ErrorCode,
60  const std::string& _Module,
61  const std::string& _Message)
62  {
63  if ((_Severity & m_logLevel) != _Severity)
64  return;
65 
66  // Thread ID
67  pid_t tid = gettid();
68  std::string _DTBuff;
69  std::stringstream strMsg;
70  strMsg << GetCurTimeString(&_DTBuff) << char(e_FieldSeparator) << GetSeverityString(_Severity)
71  << char(e_FieldSeparator) << GetErrorCode(_ErrorCode) << char(e_FieldSeparator) << "[" << _Module
72  << ":thread-" << tid << "]" << char(e_FieldSeparator) << _Message;
73 
74  smart_mutex m(m_mutex);
75  if (m_stream && m_stream->good())
76  {
77  *m_stream << strMsg.str() << std::endl;
78  m_stream->flush();
79  }
80  else
81  {
82  std::cout << strMsg.str() << std::endl;
83  }
84  }
85 
86  private:
87  std::string& GetCurTimeString(std::string* _Buf)
88  {
89  if (!_Buf)
90  return *_Buf;
91 
92  // Obtain the time of day, and convert it to a tm struct.
93  timeval tv;
94  gettimeofday(&tv, NULL);
95  tm* tm_now(localtime(&tv.tv_sec));
97  strftime(&buff[0], sizeof(char) * LOG_DATETIME_BUFF_LEN, g_cszLOG_DATETIME_FRMT, tm_now);
98  const long milliseconds = tv.tv_usec / 1000;
99  *_Buf = &buff[0];
100  std::stringstream ss;
101  ss << "." << milliseconds;
102  *_Buf += ss.str();
103  return *_Buf;
104  }
105 
106  const std::string GetSeverityString(LOG_SEVERITY _Severity) const
107  {
108  switch (_Severity)
109  {
110  case LOG_SEVERITY_INFO:
111  return g_cszLOG_SEVERITY_INFO;
114  case LOG_SEVERITY_FAULT:
118  default:
120  }
121  }
122 
123  std::string GetErrorCode(unsigned long _ErrorCode) const
124  {
125  std::stringstream sErrCode;
126  sErrCode << _ErrorCode;
127  return sErrCode.str();
128  }
129 
130  private:
131  _T* m_stream;
132  CMutex m_mutex;
133  unsigned char m_logLevel;
134  };
147  class CFileLog : public CLog<std::ofstream>
148  {
149  public:
150  typedef std::ofstream stream_type;
151 
152  public:
153  CFileLog(const std::string& _LogFileName,
154  bool _CreateNew = false,
155  unsigned char _logLevel = LOG_SEVERITY_INFO | LOG_SEVERITY_WARNING | LOG_SEVERITY_FAULT |
157  : CLog<stream_type>(&m_log_file, _logLevel)
158  , m_log_file(_LogFileName.c_str(), (_CreateNew ? std::ios::trunc : std::ios::app) | std::ios::out)
159  {
160  }
161 
162  private:
163  stream_type m_log_file;
164  };
165 };
166 #endif
const size_t LOG_DATETIME_BUFF_LEN(25)
Logging to a file.
Definition: Log.h:147
const LPCSTR g_cszLOG_SEVERITY_CRITICAL_ERROR("FLT")
const LPCSTR g_cszLOG_SEVERITY_INFO("INF")
A smart CMutex helper.
Definition: SysHelper.h:281
Definition: Log.h:34
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&#39;s severity&#39;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:204
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:32
CLog(_T *_stream, unsigned char _logLevel)
Definition: Log.h:52
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:153
Definition: Log.h:30
void push(LOG_SEVERITY _Severity, unsigned long _ErrorCode, const std::string &_Module, const std::string &_Message)
Definition: Log.h:58
A Mutex wrapper. Based on pthread calls.
Definition: SysHelper.h:256
const LPCSTR g_cszLOG_SEVERITY_FAULT("ERR")
CLog< std::ostream > CSTDOutLog
ostream specialization of CLog.
Definition: Log.h:140
std::ofstream stream_type
Definition: Log.h:150
Definition: Log.h:38
ESeverity
Log&#39;s severity&#39;s constants.
Definition: Log.h:29
A simple template class which represents the Log engine of library.
Definition: Log.h:49
Miscellaneous functions and helpers are located here.
Definition: BOOST_FILESYSTEM.h:21
Definition: Log.h:39