PLCnext API Documentation 24.0.0.71
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#include <cstring>
12
13namespace Arp { namespace System { namespace Commons { namespace Diagnostics { namespace Logging
14{
15
17// Primary template of class Loggable<Derived, bool, bool>
18template<class Derived, bool IsSingleton = false, bool IsStaticClass = false>
20{
21protected: // typedefs
23
24protected: // construction
25 Loggable(bool initialize = true);
26
27protected: // static operations
28 static void InitializeLogger(const char* loggerName = nullptr);
29
30protected: // static field used by derived class for logging
31 static Logger<Derived> log;
32};
33
35// inline methods of class Loggable<T,bool>
36template<class Derived, bool IsSingleton, bool IsStaticClass>
38{
39 if (initialize)
40 {
41 this->InitializeLogger(CommonTypeName<Derived>());
42 }
43}
44
45template<class Derived, bool IsSingleton, bool IsStaticClass>
47{
48 if (loggerName == nullptr || ::strlen(loggerName) == 0)
49 {
50 log.Initialize(CommonTypeName<Derived>());
51 }
52 else
53 {
54 log.Initialize(loggerName);
55 }
56}
57
59// Initializing of static member of class Loggable<Derived,bool>
60template <typename Derived, bool IsSingleton, bool IsStaticClass>
61Logger<Derived> Loggable<Derived, IsSingleton, IsStaticClass>::log;
62
63
65// Template specialization Loaggable<Derived, true> for singletons
66template<typename Derived>
67class Loggable<Derived, true>
68{
69protected: // typedefs
71
72protected: // construction
73 Loggable(bool initialize = true)
74 {
75 if (initialize)
76 {
77 log.Initialize(CommonTypeName<Derived>());
78 }
79 }
80
81protected: // use non static field here in specialization to avoid linking of static member into DLLs on Windows
83};
84
85
87// Template specialization Loggable<Derived, false, true> for static classes
88
91template<typename Derived>
92class Loggable<Derived, false, true>
93{
94protected: // typedefs
96
97public: // construction
98 Loggable(void) = default;
99
100public: // static operation
102 static void InitializeLogger(void)
103 {
104 log.Initialize(CommonTypeName<Derived>());
105 }
106
107protected: // static field used by derived class for logging
108 static Logger<Derived> log;
109};
110
111// Initializing of static member of class Loggable<T,false, true>
112template <typename Derived>
114
115
116}}}}} // end of namesapce Arp::System::Commons::Diagnostics::Logging
117
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:93
static void InitializeLogger(void)
This operation must be called explicitly for loggable static classes in IComponent::Initialize() or c...
Definition: Loggable.hxx:102
@ System
System components used by the System, Device, Plc or Io domains.
Root namespace for the PLCnext API