OpcUaCanOpen
CANopen OPC-UA server
LogIt.cpp
Go to the documentation of this file.
1 /* © Copyright CERN, 2015. All rights not expressly granted are reserved.
2  * LogIt.cpp
3  *
4  * Created on: Aug 18, 2015
5  * Author: Benjamin Farnham <benjamin.farnham@cern.ch>
6  *
7  * This file is part of Quasar.
8  *
9  * Quasar is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public Licence as published by
11  * the Free Software Foundation, either version 3 of the Licence.
12  *
13  * Quasar is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public Licence for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with Quasar. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include "LogIt.h"
23 #include <iostream>
24 #include <map>
25 
26 #include "ComponentAttributes.h"
27 #include "LogSinks.h"
28 #include "LogLevels.h"
29 #include "LogItInstance.h"
30 
31 using std::string;
32 
33 #ifdef LOGIT_BACKEND_STDOUTLOG
34 #include <StdOutLog.h>
35 #endif
36 
37 #ifdef LOGIT_BACKEND_BOOSTLOG
38 #include "BoostRotatingFileLog.h"
39 #endif
40 
41 #ifdef LOGIT_BACKEND_UATRACE
42 #include <UaTraceSink.h>
43 #endif
44 
45 #ifdef LOGIT_BACKEND_WINDOWS_DEBUGGER
46 #ifdef _WIN32 // back-end only relevant to windows
47 #include <WindowsDebuggerSink.h>
48 #endif
49 #endif
50 
51 // internal - not exposed via API
53 {
54  if(!LogItInstance::instanceExists()) return;
55 
56  #ifdef LOGIT_BACKEND_STDOUTLOG
57  StdOutLog * stdOutLog = new StdOutLog();
58  stdOutLog->initialize();
60  #endif
61 
62  #ifdef LOGIT_BACKEND_BOOSTLOG
63  BoostRotatingFileLog* fileLogger = new BoostRotatingFileLog();
64  fileLogger->initialize();
66  #endif
67 
68  #ifdef LOGIT_BACKEND_UATRACE
69  UaTraceSink * uaTraceSink = new UaTraceSink ();
70  uaTraceSink->initialize();
72  #endif
73 
74  #ifdef LOGIT_BACKEND_WINDOWS_DEBUGGER
75  #ifdef _WIN32 // back-end only relevant to windows
76  WindowsDebuggerSink * windowsDebuggerSink = new WindowsDebuggerSink ();
77  windowsDebuggerSink->initialize();
79  #endif
80  #endif
81 }
82 
83 
84 bool Log::initializeLogging(const Log::LOG_LEVEL& nonComponentLogLevel)
85 {
86  if(LogItInstance::instanceExists()) return false;
89  setNonComponentLogLevel(nonComponentLogLevel);
90  return true;
91 }
92 
94 {
95  return LogItInstance::setInstance(remoteInstance);
96 }
97 
98 Log::LogComponentHandle Log::registerLoggingComponent(const string& componentName, const Log::LOG_LEVEL& nonComponentLogLevel /*=Log::INF*/)
99 {
101 
102  try
103  {
104  const ComponentAttributes& componentAttributes = LogItInstance::getInstance()->registerLoggingComponent(componentName, nonComponentLogLevel);
105  return componentAttributes.getHandle();
106  }
107  catch(const std::runtime_error& e)
108  {
109  LOG(Log::ERR) << __FUNCTION__ << " Failed to register logging component ["<<componentName<<"], message: "<<e.what();
110  }
111 
112  return Log::INVALID_HANDLE;
113 }
114 
115 
116 bool Log::isLoggable(const Log::LOG_LEVEL& level)
117 {
118  if(!LogItInstance::instanceExists()) return false;
120 }
121 
122 bool Log::isLoggable(const Log::LOG_LEVEL& level, const LogComponentHandle& componentHandle)
123 {
124  if(!LogItInstance::instanceExists()) return false;
125 
126  LOG_LEVEL componentLogLevel;
127  if(!getComponentLogLevel(componentHandle, componentLogLevel)) return false; // unknown component handle.
128 
129  return level >= componentLogLevel;
130 }
131 
132 bool Log::isLoggable(const Log::LOG_LEVEL& level, const std::string& componentName)
133 {
134  if(!LogItInstance::instanceExists()) return false;
135 
136  const LogComponentHandle componentHandle = getComponentHandle(componentName);
137  if(componentHandle == Log::INVALID_HANDLE) return false;
138 
139  return isLoggable(level, componentHandle);
140 }
141 
143 {
144  if(!LogItInstance::instanceExists()) return;
145  if(LogItInstance::getInstance()->m_nonComponentLogLevel != level)
146  {
148  }
149 }
150 
152 {
154 }
155 
156 const std::map<Log::LogComponentHandle, string> Log::getComponentLogsList()
157 {
158  std::map<LogComponentHandle, string> result;
159  if(!LogItInstance::instanceExists()) return result; // empty
160 
162 }
163 
164 bool Log::setComponentLogLevel(const LogComponentHandle& componentHandle, const LOG_LEVEL& level)
165 {
166  if(!LogItInstance::instanceExists()) return false;
167 
168  ComponentAttributes* componentAttributes = nullptr;
169  if(!LogItInstance::getInstance()->getComponentAttributes(componentHandle, componentAttributes)) return false;
170 
171  componentAttributes->setLevel(level);
172  return true;
173 }
174 
175 bool Log::getComponentLogLevel(const LogComponentHandle& componentHandle, LOG_LEVEL& level)
176 {
177  if(!LogItInstance::instanceExists()) return false;
178 
179  ComponentAttributes* componentAttributes = nullptr;
180  if(!LogItInstance::getInstance()->getComponentAttributes(componentHandle, componentAttributes)) return false;
181 
182  level = componentAttributes->getLevel();
183  return true;
184 }
185 
186 string Log::getComponentName(const LogComponentHandle& componentHandle)
187 {
188  if(!LogItInstance::instanceExists()) return "UNKNOWN";
189 
190  ComponentAttributes* componentAttributes = nullptr;
191  if(!LogItInstance::getInstance()->getComponentAttributes(componentHandle, componentAttributes)) return "UNKNOWN";
192 
193  return componentAttributes->getName();
194 }
195 
197 {
199  LogComponentHandle componentHandle = Log::INVALID_HANDLE;
200  if(!LogItInstance::getInstance()->getComponentHandle(componentName, componentHandle)) return Log::INVALID_HANDLE;
201  return componentHandle;
202 }