PLCnext API Documentation  22.9.0.33
Log.hpp
1 //
3 // Copyright PHOENIX CONTACT Electronics GmbH
4 //
6 #pragma once
7 #include "Arp/System/Core/Arp.h"
8 #include "Arp/System/Commons/Text/Formatter.hxx"
9 #include "Arp/System/Commons/Diagnostics/Logging/LogStream.hpp"
10 #include "Arp/System/Commons/Diagnostics/Logging/Internal/LogAdapter.hpp"
11 
12 namespace Arp { namespace System { namespace Commons { namespace Diagnostics { namespace Logging
13 {
14 
15 using namespace Arp::System::Commons::Text;
16 
17 // forwards
18 class LogManager;
19 
20 // static class Log to be used for 'root' logging
21 class Log
22 {
23  friend class LogManager;
24 
25 public: // static log operations for function/printf call style: Log::Info(format, arg1, arg2, arg3, ...)
26  template<typename... Args> static void Trace(const char* format, const Args& ... args);
27  template<typename... Args> static void Debug(const char* format, const Args& ... args);
28  template<typename... Args> static void Info(const char* format, const Args& ... args);
29  template<typename... Args> static void Warning(const char* format, const Args& ... args);
30  template<typename... Args> static void Critical(const char* format, const Args& ... args);
31  template<typename... Args> static void Error(const char* format, const Args& ... args);
32  template<typename... Args> static void Fatal(const char* format, const Args& ... args);
33 
34  template<typename... Args> static void PrintTrace(const char* format, const Args& ... args);
35  template<typename... Args> static void PrintDebug(const char* format, const Args& ... args);
36  template<typename... Args> static void PrintInfo(const char* format, const Args& ... args);
37  template<typename... Args> static void PrintWarning(const char* format, const Args& ... args);
38  template<typename... Args> static void PrintCritical(const char* format, const Args& ... args);
39  template<typename... Args> static void PrintError(const char* format, const Args& ... args);
40  template<typename... Args> static void PrintFatal(const char* format, const Args& ... args);
41 
42 public: // static log operations for stream call style: Log::Info()<<".."<<arg1<<".."<<arg2<<".."<<arg3;
43  static LogStream Trace(void);
44  static LogStream Debug(void);
45  static LogStream Info(void);
46  static LogStream Warning(void);
47  static LogStream Critical(void);
48  static LogStream Error(void);
49  static LogStream Fatal(void);
50 
51 private: // methods
52  static void Initialize(LogAdapter::Ptr rootLogAdapter);
53  static void Uninitialize(void);
54 
55 private: // fields
56  static LogAdapter::Ptr rootLogAdapterPtr;
57 };
58 
60 // inline methods of class Log
61 inline void Log::Initialize(LogAdapter::Ptr rootLogAdapter)
62 {
63  rootLogAdapterPtr = rootLogAdapter;
64 }
65 
66 inline void Log::Uninitialize()
67 {
68  rootLogAdapterPtr.reset();
69 }
70 
71 template<typename... Args>
72 inline void Log::Trace(const char* format, const Args& ... args)
73 {
74  if (rootLogAdapterPtr != nullptr)
75  {
76  if (rootLogAdapterPtr->GetLogLevel() < LogLevel::Trace)
77  {
78  return;
79  }
80  // else
81  rootLogAdapterPtr->Log(LogLevel::Trace, Formatter::FormatCommon(format, args...));
82  }
83 }
84 
85 template<typename... Args>
86 inline void Log::Debug(const char* format, const Args& ... args)
87 {
88  if (rootLogAdapterPtr != nullptr)
89  {
90  if (rootLogAdapterPtr->GetLogLevel() < LogLevel::Debug)
91  {
92  return;
93  }
94  // else
95  rootLogAdapterPtr->Log(LogLevel::Debug, Formatter::FormatCommon(format, args...));
96  }
97 }
98 
99 template<typename... Args>
100 inline void Log::Info(const char* format, const Args& ... args)
101 {
102  if (rootLogAdapterPtr != nullptr)
103  {
104  if (rootLogAdapterPtr->GetLogLevel() < LogLevel::Info)
105  {
106  return;
107  }
108  // else
109  rootLogAdapterPtr->Log(LogLevel::Info, Formatter::FormatCommon(format, args...));
110  }
111 }
112 
113 template<typename... Args>
114 inline void Log::Warning(const char* format, const Args& ... args)
115 {
116  if (rootLogAdapterPtr != nullptr)
117  {
118  if (rootLogAdapterPtr->GetLogLevel() < LogLevel::Warning)
119  {
120  return;
121  }
122  // else
123  rootLogAdapterPtr->Log(LogLevel::Warning, Formatter::FormatCommon(format, args...));
124  }
125 }
126 
127 template<typename... Args>
128 inline void Log::Critical(const char* format, const Args& ... args)
129 {
130  if (rootLogAdapterPtr != nullptr)
131  {
132  if (rootLogAdapterPtr->GetLogLevel() < LogLevel::Critical)
133  {
134  return;
135  }
136  // else
137  rootLogAdapterPtr->Log(LogLevel::Critical, Formatter::FormatCommon(format, args...));
138  }
139 }
140 
141 template<typename... Args>
142 inline void Log::Error(const char* format, const Args& ... args)
143 {
144  if (rootLogAdapterPtr != nullptr)
145  {
146  if (rootLogAdapterPtr->GetLogLevel() < LogLevel::Error)
147  {
148  return;
149  }
150  // else
151  rootLogAdapterPtr->Log(LogLevel::Error, Formatter::FormatCommon(format, args...));
152  }
153 }
154 
155 template<typename... Args>
156 inline void Log::Fatal(const char* format, const Args& ... args)
157 {
158  if (rootLogAdapterPtr != nullptr)
159  {
160  if (rootLogAdapterPtr->GetLogLevel() < LogLevel::Fatal)
161  {
162  return;
163  }
164  // else
165  rootLogAdapterPtr->Log(LogLevel::Fatal, Formatter::FormatCommon(format, args...));
166  }
167 }
168 
169 template<typename... Args>
170 inline void Log::PrintTrace(const char* format, const Args& ... args)
171 {
172  if (rootLogAdapterPtr != nullptr)
173  {
174  if (rootLogAdapterPtr->GetLogLevel() < LogLevel::Trace)
175  {
176  return;
177  }
178  // else
179  rootLogAdapterPtr->Log(LogLevel::Trace, Formatter::FormatPrintf(format, args...));
180  }
181 }
182 
183 template<typename... Args>
184 inline void Log::PrintDebug(const char* format, const Args& ... args)
185 {
186  if (rootLogAdapterPtr != nullptr)
187  {
188  if (rootLogAdapterPtr->GetLogLevel() < LogLevel::Debug)
189  {
190  return;
191  }
192  // else
193  rootLogAdapterPtr->Log(LogLevel::Debug, Formatter::FormatPrintf(format, args...));
194  }
195 }
196 
197 template<typename... Args>
198 inline void Log::PrintInfo(const char* format, const Args& ... args)
199 {
200  if (rootLogAdapterPtr != nullptr)
201  {
202  if (rootLogAdapterPtr->GetLogLevel() < LogLevel::Info)
203  {
204  return;
205  }
206  // else
207  rootLogAdapterPtr->Log(LogLevel::Info, Formatter::FormatPrintf(format, args...));
208  }
209 }
210 
211 template<typename... Args>
212 inline void Log::PrintWarning(const char* format, const Args& ... args)
213 {
214  if (rootLogAdapterPtr != nullptr)
215  {
216  if (rootLogAdapterPtr->GetLogLevel() < LogLevel::Warning)
217  {
218  return;
219  }
220  // else
221  rootLogAdapterPtr->Log(LogLevel::Warning, Formatter::FormatPrintf(format, args...));
222  }
223 }
224 
225 template<typename... Args>
226 inline void Log::PrintCritical(const char* format, const Args& ... args)
227 {
228  if (rootLogAdapterPtr != nullptr)
229  {
230  if (rootLogAdapterPtr->GetLogLevel() < LogLevel::Critical)
231  {
232  return;
233  }
234  // else
235  rootLogAdapterPtr->Log(LogLevel::Critical, Formatter::FormatPrintf(format, args...));
236  }
237 }
238 
239 template<typename... Args>
240 inline void Log::PrintError(const char* format, const Args& ... args)
241 {
242  if (rootLogAdapterPtr != nullptr)
243  {
244  if (rootLogAdapterPtr->GetLogLevel() < LogLevel::Error)
245  {
246  return;
247  }
248  // else
249  rootLogAdapterPtr->Log(LogLevel::Error, Formatter::FormatPrintf(format, args...));
250  }
251 }
252 
253 template<typename... Args>
254 inline void Log::PrintFatal(const char* format, const Args& ... args)
255 {
256  if (rootLogAdapterPtr != nullptr)
257  {
258  if (rootLogAdapterPtr->GetLogLevel() < LogLevel::Fatal)
259  {
260  return;
261  }
262  // else
263  rootLogAdapterPtr->Log(LogLevel::Fatal, Formatter::FormatPrintf(format, args...));
264  }
265 }
266 
267 inline LogStream Log::Trace()
268 {
269  if (rootLogAdapterPtr != nullptr)
270  {
271  return (rootLogAdapterPtr->GetLogLevel() < LogLevel::Trace) ? LogStream::Null : rootLogAdapterPtr->GetLogStream(LogLevel::Trace);
272  }
273  else
274  {
275  return LogStream::Null;
276  }
277 }
278 
279 inline LogStream Log::Debug()
280 {
281  if (rootLogAdapterPtr != nullptr)
282  {
283  return (rootLogAdapterPtr->GetLogLevel() < LogLevel::Debug) ? LogStream::Null : rootLogAdapterPtr->GetLogStream(LogLevel::Debug);
284  }
285  else
286  {
287  return LogStream::Null;
288  }
289 }
290 
291 inline LogStream Log::Info()
292 {
293  if (rootLogAdapterPtr != nullptr)
294  {
295  return (rootLogAdapterPtr->GetLogLevel() < LogLevel::Info) ? LogStream::Null : rootLogAdapterPtr->GetLogStream(LogLevel::Info);
296  }
297  else
298  {
299  return LogStream::Null;
300  }
301 }
302 
303 inline LogStream Log::Warning()
304 {
305  if (rootLogAdapterPtr != nullptr)
306  {
307  return (rootLogAdapterPtr->GetLogLevel() < LogLevel::Warning) ? LogStream::Null : rootLogAdapterPtr->GetLogStream(LogLevel::Warning);
308  }
309  else
310  {
311  return LogStream::Null;
312  }
313 }
314 
315 inline LogStream Log::Critical()
316 {
317  if (rootLogAdapterPtr != nullptr)
318  {
319  return (rootLogAdapterPtr->GetLogLevel() < LogLevel::Critical) ? LogStream::Null : rootLogAdapterPtr->GetLogStream(LogLevel::Critical);
320  }
321  else
322  {
323  return LogStream::Null;
324  }
325 }
326 
327 inline LogStream Log::Error()
328 {
329  if (rootLogAdapterPtr != nullptr)
330  {
331  return (rootLogAdapterPtr->GetLogLevel() < LogLevel::Error) ? LogStream::Null : rootLogAdapterPtr->GetLogStream(LogLevel::Error);
332  }
333  else
334  {
335  return LogStream::Null;
336  }
337 }
338 
339 inline LogStream Log::Fatal()
340 {
341  if (rootLogAdapterPtr != nullptr)
342  {
343  return (rootLogAdapterPtr->GetLogLevel() < LogLevel::Fatal) ? LogStream::Null : rootLogAdapterPtr->GetLogStream(LogLevel::Fatal);
344  }
345  else
346  {
347  return LogStream::Null;
348  }
349 }
350 
351 }}}}} // end of namesapce Arp::System::Commons::Diagnostics::Logging
352 
static String FormatPrintf(const String &format, const Args &... args)
Uses Ansi-C printf syntax for the placeholders in the format string like 's'.
Definition: Formatter.hxx:53
static String FormatCommon(const String &format, const Args &... args)
Uses common CLR syntax (.Net) for the placeholders in the format string like '{0}'.
Definition: Formatter.hxx:39
@ System
System components used by the System, Device, Plc or Io domains.
Namespace for classes handling text
Definition: Formatter.hxx:11
Root namespace for the PLCnext API