PLCnext API Documentation  22.9.0.33
LogManager.hpp
1 //
3 // Copyright PHOENIX CONTACT Electronics GmbH
4 //
6 #pragma once
7 #include "Arp/System/Core/Arp.h"
8 #include "Arp/System/Core/event.hxx"
9 #include "Arp/System/Core/TypeName.hxx"
10 #include "Arp/System/Core/AppDomainSingleton.hxx"
11 #include "Arp/System/Commons/Threading/Mutex.hpp"
12 #include "Arp/System/Commons/Diagnostics/Logging/LogLevel.hpp"
13 #include "Arp/System/Commons/Diagnostics/Logging/LogInfo.hpp"
14 #include "Arp/System/Commons/Diagnostics/Logging/Internal/LogAdapter.hpp"
15 #include <new>
16 #include <atomic>
17 
18 // forwards
19 namespace log4cplus
20 {
21 class Initializer;
22 class Hierarchy;
23 class Logger;
24 }
25 
26 namespace Arp { namespace System { namespace Commons { namespace Diagnostics { namespace Logging
27 {
28 
29 // forwards
30 namespace Internal
31 {
32 class LogEventAppender;
33 }
34 class LoggerBase;
35 
36 // singleton log manager class
37 //TODO:SDK: Remove from SDK
38 class LogManager : public AppDomainSingleton<LogManager>
39 {
40  friend class LoggerBase;
41  friend class Internal::LogAdapter;
42  friend class AppDomainSingleton<LogManager>;
43 
44 private: // typedefs/usings
47  using LogAdapter = Internal::LogAdapter;
48  using LogEventAppenderPtr = std::shared_ptr<Internal::LogEventAppender>;
50  using AppendDelegate = delegate<void(const LogInfo&)>;
51 
52 private:
53  LogManager(void);
54  LogManager(const LogManager&) = delete;
55  ~LogManager(void);
56  LogManager& operator=(const LogManager&) = delete;
57 
58 public: // singleton operations
59  static void AssignAppDomain(AppDomain& otherDomain);
60  static void Create(const char* configFileName);
61  static void Create(bool enableConsoleLogging, const char* logFileName = nullptr);
62  static void Create(LogLevel logLevel, bool enableConsoleLogging, const char* logFileName = nullptr);
63  static void Create(const char* layoutPattern, bool enableConsoleLogging, const char* logFileName = nullptr);
64  static void Create(LogLevel logLevel, const char* layoutPattern, bool enableConsoleLogging, const char* logFileName = nullptr);
65  static void Dispose(void);
66 
67 public: // setter/getter
68  void SetRootLevel(LogLevel value);
69  LogLevel GetRootLevel(void);
70  Mutex& GetSyncRoot(void); // TODO:SDK: make private
71 
72 public: // operations
73  LogAdapter CreateLogAdapter(const char* loggerName); // TODO:SDK: make private
74  void AddAppendDelegate(AppendDelegate appendDelegate);
75  void RemoveAppendDelegate(AppendDelegate appendDelegate);
76 
77 private: // methods
78  void Initialize(LogLevel rootLevel, log4cplus::Logger& rootLogger);
79  log4cplus::Logger GetLoggerInstance(const char* loggerName);
80  bool HasAppendEvent(void);
81  void FireAppendEvent(const LogInfo& logInfo);
82 
83 private: // fields
84  Mutex syncRoot;
85  LogAdapter::Ptr rootLogAdapterPtr;
86  log4cplus::Hierarchy* pRootHierarchy;
87  log4cplus::Initializer* pInitializer;
88 
89 private: // static event fields
90  ARP_DEPRECATED("Do not use. Might still be referenced in obsolete inlined code.")
91  static std::atomic<size_t> appendEventCounter; // TODO:SDK: Remove this
92 
93 private: // const static data
94  static const char* const defaultLayoutPattern;
95 };
96 
98 // inline methods of class LogManager
99 
100 inline void LogManager::SetRootLevel(LogLevel value)
101 {
102  this->rootLogAdapterPtr->SetLogLevel(value);
103 }
104 
105 inline LogLevel LogManager::GetRootLevel()
106 {
107  // OR: do not call LogAdapter::GetLogLevel() - this would cause a cyclic method invocation
108  return this->rootLogAdapterPtr->logLevel;
109 }
110 
111 inline LogManager::Mutex& LogManager::GetSyncRoot()
112 {
113  return this->syncRoot;
114 }
115 
116 inline void LogManager::Create(bool enableConsoleLogging, const char* logFileName)
117 {
118  Create(LogLevel::Default, nullptr, enableConsoleLogging, logFileName);
119 }
120 
121 inline void LogManager::Create(LogLevel logLevel, bool enableConsoleLogging, const char* logFileName)
122 {
123  Create(logLevel, nullptr, enableConsoleLogging, logFileName);
124 }
125 
126 inline void LogManager::Create(const char* layoutPattern, bool enableConsoleLogging, const char* logFileName)
127 {
128  Create(LogLevel::Default, layoutPattern, enableConsoleLogging, logFileName);
129 }
130 
131 }}}}} // end of namesapce Arp::System::Commons::Diagnostics::Logging
This class implements the singleton pattern for singletons with process wide scope.
Definition: AppDomainSingleton.hxx:25
This class represents a single application domain for each process and is implemented as singleton.
Definition: AppDomain.hpp:122
Mutual exclusion object to prevent data from concurrent modifications.
Definition: Mutex.hpp:26
Use this class to register and invoke several delegates (function pointer in OOP design).
Definition: event.hxx:33
@ System
System components used by the System, Device, Plc or Io domains.
Root namespace for the PLCnext API