PLCnext API Documentation  22.9.0.33
Loggable.hxx
1 //
3 // Copyright PHOENIX CONTACT Electronics GmbH
4 //
6 #pragma once
7 #include "Arp/System/Core/Arp.h"
8 #include "Arp/System/Core/CommonTypeName.hxx"
9 #include "Arp/System/Commons/Diagnostics/Logging/Logger.hxx"
10 #include "Arp/System/Commons/Diagnostics/Logging/LogLevel.hpp"
11 
12 namespace Arp { namespace System { namespace Commons { namespace Diagnostics { namespace Logging
13 {
14 
16 // Primary template of class Loggable<Derived, bool, bool>
17 template<class Derived, bool IsSingleton = false, bool IsStaticClass = false>
18 class Loggable
19 {
20 protected: // typedefs
22 
23 protected: // construction
24  Loggable(bool initialize = true);
25 
26 protected: // static operations
27  static void InitializeLogger(const char* loggerName = nullptr);
28 
29 protected: // static field used by derived class for logging
30  static Logger<Derived> log;
31 };
32 
34 // inline methods of class Loggable<T,bool>
35 template<class Derived, bool IsSingleton, bool IsStaticClass>
37 {
38  if (initialize)
39  {
40  this->InitializeLogger(CommonTypeName<Derived>());
41  }
42 }
43 
44 template<class Derived, bool IsSingleton, bool IsStaticClass>
46 {
47  if (loggerName == nullptr || ::strlen(loggerName) == 0)
48  {
49  log.Initialize(CommonTypeName<Derived>());
50  }
51  else
52  {
53  log.Initialize(loggerName);
54  }
55 }
56 
58 // Initializing of static member of class Loggable<Derived,bool>
59 template <typename Derived, bool IsSingleton, bool IsStaticClass>
60 Logger<Derived> Loggable<Derived, IsSingleton, IsStaticClass>::log;
61 
62 
64 // Template specialization Loaggable<Derived, true> for singletons
65 template<typename Derived>
66 class Loggable<Derived, true>
67 {
68 protected: // typedefs
70 
71 protected: // construction
72  Loggable(bool initialize = true)
73  {
74  if (initialize)
75  {
76  log.Initialize(CommonTypeName<Derived>());
77  }
78  }
79 
80 protected: // use non static field here in specialization to avoid linking of static member into DLLs on Windows
81  Logger<Derived> log;
82 };
83 
84 
86 // Template specialization Loggable<Derived, false, true> for static classes
87 
90 template<typename Derived>
91 class Loggable<Derived, false, true>
92 {
93 protected: // typedefs
95 
96 public: // construction
97  Loggable(void) = default;
98 
99 public: // static operation
101  static void InitializeLogger(void)
102  {
103  log.Initialize(CommonTypeName<Derived>());
104  }
105 
106 protected: // static field used by derived class for logging
107  static Logger<Derived> log;
108 };
109 
110 // Initializing of static member of class Loggable<T,false, true>
111 template <typename Derived>
113 
114 
115 }}}}} // end of namesapce Arp::System::Commons::Diagnostics::Logging
116 
This (meta programming) class provides the typename according the CLS (common language specification)...
Definition: CommonTypeName.hxx:30
This template specialization Loggable<T, false, true> shall be used by pure static classes.
Definition: Loggable.hxx:92
static void InitializeLogger(void)
This operation must be called explicitly for loggable static classes in IComponent::Initialize() or c...
Definition: Loggable.hxx:101
@ System
System components used by the System, Device, Plc or Io domains.
Root namespace for the PLCnext API