forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoggingConfigurator.cpp
198 lines (166 loc) · 5.26 KB
/
LoggingConfigurator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//
// LoggingConfigurator.cpp
//
// Library: Util
// Package: Configuration
// Module: LoggingConfigurator
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Util/LoggingConfigurator.h"
#include "Poco/AutoPtr.h"
#include "Poco/Channel.h"
#include "Poco/FormattingChannel.h"
#include "Poco/Formatter.h"
#include "Poco/PatternFormatter.h"
#include "Poco/Logger.h"
#include "Poco/LoggingRegistry.h"
#include "Poco/LoggingFactory.h"
#include <map>
using Poco::AutoPtr;
using Poco::Formatter;
using Poco::PatternFormatter;
using Poco::Channel;
using Poco::FormattingChannel;
using Poco::Logger;
using Poco::LoggingRegistry;
using Poco::LoggingFactory;
namespace Poco {
namespace Util {
LoggingConfigurator::LoggingConfigurator()
{
}
LoggingConfigurator::~LoggingConfigurator()
{
}
void LoggingConfigurator::configure(AbstractConfiguration::Ptr pConfig)
{
poco_check_ptr (pConfig);
AbstractConfiguration::Ptr pFormattersConfig(pConfig->createView("logging.formatters"));
configureFormatters(pFormattersConfig);
AbstractConfiguration::Ptr pChannelsConfig(pConfig->createView("logging.channels"));
configureChannels(pChannelsConfig);
AbstractConfiguration::Ptr pLoggersConfig(pConfig->createView("logging.loggers"));
configureLoggers(pLoggersConfig);
}
void LoggingConfigurator::configureFormatters(AbstractConfiguration::Ptr pConfig)
{
AbstractConfiguration::Keys formatters;
pConfig->keys(formatters);
for (const auto& f: formatters)
{
AutoPtr<AbstractConfiguration> pFormatterConfig(pConfig->createView(f));
AutoPtr<Formatter> pFormatter(createFormatter(pFormatterConfig));
LoggingRegistry::defaultRegistry().registerFormatter(f, pFormatter);
}
}
void LoggingConfigurator::configureChannels(AbstractConfiguration::Ptr pConfig)
{
AbstractConfiguration::Keys channels;
pConfig->keys(channels);
for (const auto& c: channels)
{
AutoPtr<AbstractConfiguration> pChannelConfig(pConfig->createView(c));
AutoPtr<Channel> pChannel = createChannel(pChannelConfig);
LoggingRegistry::defaultRegistry().registerChannel(c, pChannel);
}
for (const auto& c: channels)
{
AutoPtr<AbstractConfiguration> pChannelConfig(pConfig->createView(c));
Channel::Ptr pChannel = LoggingRegistry::defaultRegistry().channelForName(c);
configureChannel(pChannel, pChannelConfig);
}
}
void LoggingConfigurator::configureLoggers(AbstractConfiguration::Ptr pConfig)
{
using LoggerMap = std::map<std::string, AutoPtr<AbstractConfiguration>>;
AbstractConfiguration::Keys loggers;
pConfig->keys(loggers);
// use a map to sort loggers by their name, ensuring initialization in correct order (parents before children)
LoggerMap loggerMap;
for (const auto& l: loggers)
{
AutoPtr<AbstractConfiguration> pLoggerConfig(pConfig->createView(l));
loggerMap[pLoggerConfig->getString("name", "")] = pLoggerConfig;
}
for (const auto& p: loggerMap)
{
configureLogger(p.second);
}
}
Formatter::Ptr LoggingConfigurator::createFormatter(AbstractConfiguration::Ptr pConfig)
{
Formatter::Ptr pFormatter(LoggingFactory::defaultFactory().createFormatter(pConfig->getString("class")));
AbstractConfiguration::Keys props;
pConfig->keys(props);
for (const auto& p: props)
{
if (p != "class")
pFormatter->setProperty(p, pConfig->getString(p));
}
return pFormatter;
}
Channel::Ptr LoggingConfigurator::createChannel(AbstractConfiguration::Ptr pConfig)
{
Channel::Ptr pChannel(LoggingFactory::defaultFactory().createChannel(pConfig->getString("class")));
Channel::Ptr pWrapper(pChannel);
AbstractConfiguration::Keys props;
pConfig->keys(props);
for (const auto& p: props)
{
if (p == "pattern")
{
AutoPtr<Formatter> pPatternFormatter(new PatternFormatter(pConfig->getString(p)));
pWrapper = new FormattingChannel(pPatternFormatter, pChannel);
}
else if (p == "formatter")
{
AutoPtr<FormattingChannel> pFormattingChannel(new FormattingChannel(0, pChannel));
if (pConfig->hasProperty("formatter.class"))
{
AutoPtr<AbstractConfiguration> pFormatterConfig(pConfig->createView(p));
AutoPtr<Formatter> pFormatter(createFormatter(pFormatterConfig));
pFormattingChannel->setFormatter(pFormatter);
}
else pFormattingChannel->setProperty(p, pConfig->getString(p));
pWrapper = pFormattingChannel;
}
}
return pWrapper;
}
void LoggingConfigurator::configureChannel(Channel::Ptr pChannel, AbstractConfiguration::Ptr pConfig)
{
AbstractConfiguration::Keys props;
pConfig->keys(props);
for (const auto& p: props)
{
if (p != "pattern" && p != "formatter" && p != "class")
{
pChannel->setProperty(p, pConfig->getString(p));
}
}
}
void LoggingConfigurator::configureLogger(AbstractConfiguration::Ptr pConfig)
{
Logger& logger = Logger::get(pConfig->getString("name", ""));
AbstractConfiguration::Keys props;
pConfig->keys(props);
for (const auto& p: props)
{
if (p == "channel" && pConfig->hasProperty("channel.class"))
{
AutoPtr<AbstractConfiguration> pChannelConfig(pConfig->createView(p));
AutoPtr<Channel> pChannel(createChannel(pChannelConfig));
configureChannel(pChannel, pChannelConfig);
Logger::setChannel(logger.name(), pChannel);
}
else if (p != "name")
{
Logger::setProperty(logger.name(), p, pConfig->getString(p));
}
}
}
} } // namespace Poco::Util