CppLogging  1.0.4.0
C++ Logging Library
config.cpp
Go to the documentation of this file.
1 
9 #include "logging/config.h"
10 
11 namespace CppLogging {
12 
14 {
15  // Shutdown working logger processors
16  Shutdown();
17 }
18 
19 void Config::ConfigLogger(const std::shared_ptr<Processor>& sink)
20 {
21  Config& instance = GetInstance();
22 
23  CppCommon::Locker<CppCommon::CriticalSection> locker(instance._lock);
24 
25  instance._config[""] = sink;
26 }
27 
28 void Config::ConfigLogger(const std::string& name, const std::shared_ptr<Processor>& sink)
29 {
30  Config& instance = GetInstance();
31 
32  CppCommon::Locker<CppCommon::CriticalSection> locker(instance._lock);
33 
34  instance._config[name] = sink;
35 }
36 
38 {
39  Config& instance = GetInstance();
40 
41  CppCommon::Locker<CppCommon::CriticalSection> locker(instance._lock);
42 
43  auto it = instance._working.find("");
44  if (it != instance._working.end())
45  return Logger(it->first, it->second);
46  else
47  {
48  auto sink = std::make_shared<Processor>(std::make_shared<TextLayout>());
49  sink->appenders().push_back(std::make_shared<ConsoleAppender>());
50  instance._working[""] = sink;
51  return Logger("", sink);
52  }
53 }
54 
55 Logger Config::CreateLogger(const std::string& name)
56 {
57  Config& instance = GetInstance();
58 
59  CppCommon::Locker<CppCommon::CriticalSection> locker(instance._lock);
60 
61  auto it = instance._working.find(name);
62  if (it != instance._working.end())
63  return Logger(it->first, it->second);
64  else
65  return CreateLogger();
66 }
67 
69 {
70  Config& instance = GetInstance();
71 
72  CppCommon::Locker<CppCommon::CriticalSection> locker(instance._lock);
73 
74  // Update working logger processors map
75  std::swap(instance._working, instance._config);
76 
77  // Start all working logger processors
78  for (auto& processor : instance._working)
79  if (processor.second)
80  processor.second->Start();
81 
82  // Clear config logger processors map
83  instance._config.clear();
84 }
85 
87 {
88  Config& instance = GetInstance();
89 
90  CppCommon::Locker<CppCommon::CriticalSection> locker(instance._lock);
91 
92  // Flush and stop all working logger processors
93  for (auto& processor : instance._working)
94  {
95  if (processor.second)
96  {
97  processor.second->Flush();
98  processor.second->Stop();
99  }
100  }
101 
102  // Clear working logger processors map
103  instance._working.clear();
104 }
105 
106 } // namespace CppLogging
Logger configuration static class.
Definition: config.h:25
static void Shutdown()
Shutdown the logging infrastructure.
Definition: config.cpp:86
static void ConfigLogger(const std::shared_ptr< Processor > &sink)
Configure default logger with a given logging sink processor.
Definition: config.cpp:19
static void Startup()
Startup the logging infrastructure.
Definition: config.cpp:68
static Logger CreateLogger()
Create default logger.
Definition: config.cpp:37
Logger interface.
Definition: logger.h:23
Logger configuration definition.
C++ Logging project definitions.
Definition: appender.h:15
void swap(Record &record1, Record &record2) noexcept
Definition: record.inl:472