DDS  ver. 3.5.3.8.g5fe284b
LogImp.h
Go to the documentation of this file.
1 // Copyright 2014 GSI, Inc. All rights reserved.
2 //
3 // Log engine core implementation.
4 //
5 #ifndef CLOGIMP_H
6 #define CLOGIMP_H
7 
8 // STD
9 #include <memory>
10 #include <stdexcept>
11 // MiscCommon
12 #include "Log.h"
13 
14 namespace MiscCommon
15 {
22 #define REGISTER_LOG_MODULE(name) \
23  std::string GetModuleName() const \
24  { \
25  return name; \
26  }
27 
35  {
36  typedef std::auto_ptr<CFileLog> CFileLogPtr;
37 
39  {
40  }
41  ~CLogSingleton()
42  {
43  }
44 
45  public:
46  int Init(const std::string& _LogFileName,
47  bool _CreateNew = false,
48  unsigned char _logLevel = LOG_SEVERITY_INFO | LOG_SEVERITY_WARNING | LOG_SEVERITY_FAULT |
50  {
51  if (m_log.get())
52  throw std::logic_error("Log's singleton class has been already initialized.");
53 
54  m_log = CFileLogPtr(new CFileLog(_LogFileName, _CreateNew, _logLevel));
55  push(LOG_SEVERITY_INFO, 0, "LOG singleton", "LOG singleton has been initialized.");
56  return 0;
57  }
59  {
60  static CLogSingleton log;
61  return log;
62  }
63  void push(LOG_SEVERITY _Severity,
64  unsigned long _ErrorCode,
65  const std::string& _Module,
66  const std::string& _Message)
67  {
68  if (!m_log.get())
69  {
70  std::cerr << _Message << std::endl;
71  return;
72  }
73  m_log->push(_Severity, _ErrorCode, _Module, _Message);
74  }
75  bool IsReady()
76  {
77  return (NULL != m_log.get());
78  }
79 
80  private:
81  CFileLogPtr m_log;
82  };
83 
100  template <typename _T>
101  class CLogImp
102  {
103  public:
105  {
106  if (CLogSingleton::Instance().IsReady())
108  LOG_SEVERITY_INFO, 0, g_cszMODULENAME_CORE, "Bringing >>> " + GetModuleName() + " <<< to life...");
109  }
111  {
113  LOG_SEVERITY_INFO, 0, g_cszMODULENAME_CORE, "Shutting down >>> " + GetModuleName() + " <<<");
114  }
115  void InfoLog(const std::string& _Message)
116  {
117  return CLogSingleton::Instance().push(LOG_SEVERITY_INFO, 0, GetModuleName(), _Message);
118  }
119  void InfoLog(unsigned long _ErrorCode, const std::string& _Message)
120  {
121  return CLogSingleton::Instance().push(LOG_SEVERITY_INFO, _ErrorCode, GetModuleName(), _Message);
122  }
123  void WarningLog(unsigned long _ErrorCode, const std::string& _Message)
124  {
125  return CLogSingleton::Instance().push(LOG_SEVERITY_WARNING, _ErrorCode, GetModuleName(), _Message);
126  }
127  void FaultLog(unsigned long _ErrorCode, const std::string& _Message)
128  {
129  return CLogSingleton::Instance().push(LOG_SEVERITY_FAULT, _ErrorCode, GetModuleName(), _Message);
130  }
131  void CriticalErrLog(unsigned long _ErrorCode, const std::string& _Message)
132  {
133  return CLogSingleton::Instance().push(LOG_SEVERITY_CRITICAL_ERROR, _ErrorCode, GetModuleName(), _Message);
134  }
135  void DebugLog(unsigned long _ErrorCode, const std::string& _Message)
136  {
137  return CLogSingleton::Instance().push(LOG_SEVERITY_DEBUG, _ErrorCode, GetModuleName(), _Message);
138  }
139  void msgPush(LOG_SEVERITY _Severity, const std::string& _Message, unsigned long _ErrorCode = 0)
140  {
141  return CLogSingleton::Instance().push(_Severity, _ErrorCode, GetModuleName(), _Message);
142  }
143 
144  private:
145  std::string GetModuleName()
146  {
147  _T* pT = reinterpret_cast<_T*>(this);
148  return pT->GetModuleName();
149  }
150  };
151 }; // namespace MiscCommon
152 #endif
Logging to a file.
Definition: Log.h:148
void WarningLog(unsigned long _ErrorCode, const std::string &_Message)
Definition: LogImp.h:123
void InfoLog(unsigned long _ErrorCode, const std::string &_Message)
Definition: LogImp.h:119
Definition: Log.h:35
bool IsReady()
Definition: LogImp.h:75
enum MiscCommon::ESeverity LOG_SEVERITY
Log's severity's constants.
Template class. High-end helper implementation of CLog, its ofstream specialization.
Definition: LogImp.h:101
const LPCSTR g_cszMODULENAME_CORE("CORE")
~CLogImp()
Definition: LogImp.h:110
#define _T(s)
Use TCHAR instead of char or wchar_t. It will be appropriately translated.
Definition: def.h:85
It represents logbook as a singleton.
Definition: LogImp.h:34
Definition: Log.h:33
void msgPush(LOG_SEVERITY _Severity, const std::string &_Message, unsigned long _ErrorCode=0)
Definition: LogImp.h:139
void push(LOG_SEVERITY _Severity, unsigned long _ErrorCode, const std::string &_Module, const std::string &_Message)
Definition: LogImp.h:63
void FaultLog(unsigned long _ErrorCode, const std::string &_Message)
Definition: LogImp.h:127
Definition: Log.h:31
void DebugLog(unsigned long _ErrorCode, const std::string &_Message)
Definition: LogImp.h:135
CLogImp()
Definition: LogImp.h:104
void InfoLog(const std::string &_Message)
Definition: LogImp.h:115
static CLogSingleton & Instance()
Definition: LogImp.h:58
int Init(const std::string &_LogFileName, bool _CreateNew=false, unsigned char _logLevel=LOG_SEVERITY_INFO|LOG_SEVERITY_WARNING|LOG_SEVERITY_FAULT|LOG_SEVERITY_CRITICAL_ERROR)
Definition: LogImp.h:46
Miscellaneous functions and helpers are located here.
Definition: BOOST_FILESYSTEM.h:21
void CriticalErrLog(unsigned long _ErrorCode, const std::string &_Message)
Definition: LogImp.h:131