PLCnext API Documentation 23.0.2.9
Loggable.hxx
1
2//
3// Copyright PHOENIX CONTACT Electronics GmbH
4//
6#pragma once
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
12namespace Arp { namespace System { namespace Commons { namespace Diagnostics { namespace Logging
13{
14
16// Primary template of class Loggable<Derived, bool, bool>
17template<class Derived, bool IsSingleton = false, bool IsStaticClass = false>
19{
20protected: // typedefs
22
23protected: // construction
24 Loggable(bool initialize = true);
25
26protected: // static operations
27 static void InitializeLogger(const char* loggerName = nullptr);
28
29protected: // static field used by derived class for logging
30 static Logger<Derived> log;
31};
32
34// inline methods of class Loggable<T,bool>
35template<class Derived, bool IsSingleton, bool IsStaticClass>
37{
38 if (initialize)
39 {
40 this->InitializeLogger(CommonTypeName<Derived>());
41 }
42}
43
44template<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>
59template <typename Derived, bool IsSingleton, bool IsStaticClass>
60Logger<Derived> Loggable<Derived, IsSingleton, IsStaticClass>::log;
61
62
64// Template specialization Loaggable<Derived, true> for singletons
65template<typename Derived>
66class Loggable<Derived, true>
67{
68protected: // typedefs
70
71protected: // construction
72 Loggable(bool initialize = true)
73 {
74 if (initialize)
75 {
76 log.Initialize(CommonTypeName<Derived>());
77 }
78 }
79
80protected: // use non static field here in specialization to avoid linking of static member into DLLs on Windows
82};
83
84
86// Template specialization Loggable<Derived, false, true> for static classes
87
90template<typename Derived>
91class Loggable<Derived, false, true>
92{
93protected: // typedefs
95
96public: // construction
97 Loggable(void) = default;
98
99public: // static operation
101 static void InitializeLogger(void)
102 {
103 log.Initialize(CommonTypeName<Derived>());
104 }
105
106protected: // 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>
111template <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