support absence of BaseDaemon in OwnPatternFormatter

This commit is contained in:
Pavel Kartavyy 2017-02-07 19:37:56 +03:00
parent fb5ff00c4a
commit 5107cd94b3
2 changed files with 11 additions and 8 deletions

View File

@ -7,6 +7,9 @@
#include <DB/IO/WriteHelpers.h> #include <DB/IO/WriteHelpers.h>
#include <daemon/BaseDaemon.h> #include <daemon/BaseDaemon.h>
#include <experimental/optional>
#include <functional>
/** Форматирует по своему. /** Форматирует по своему.
* Некоторые детали невозможно получить, используя только Poco::PatternFormatter. * Некоторые детали невозможно получить, используя только Poco::PatternFormatter.
@ -30,16 +33,16 @@ public:
ADD_LAYER_TAG = 1 << 0 ADD_LAYER_TAG = 1 << 0
}; };
OwnPatternFormatter(const BaseDaemon & daemon_, Options options_ = ADD_NOTHING) : Poco::PatternFormatter(""), daemon(daemon_), options(options_) {} OwnPatternFormatter(const BaseDaemon * daemon_, Options options_ = ADD_NOTHING) : Poco::PatternFormatter(""), daemon(daemon_), options(options_) {}
void format(const Poco::Message & msg, std::string & text) override void format(const Poco::Message & msg, std::string & text) override
{ {
DB::WriteBufferFromString wb(text); DB::WriteBufferFromString wb(text);
/// For syslog: tag must be before message and first whitespace. /// For syslog: tag must be before message and first whitespace.
if (options & ADD_LAYER_TAG) if (options & ADD_LAYER_TAG && daemon)
{ {
auto layer = daemon.getLayer(); auto layer = daemon->getLayer();
if (layer) if (layer)
{ {
writeCString("layer[", wb); writeCString("layer[", wb);
@ -75,6 +78,6 @@ public:
} }
private: private:
const BaseDaemon & daemon; const BaseDaemon * daemon;
Options options; Options options;
}; };

View File

@ -540,7 +540,7 @@ void BaseDaemon::buildLoggers()
Poco::AutoPtr<SplitterChannel> split = new SplitterChannel; Poco::AutoPtr<SplitterChannel> split = new SplitterChannel;
// set up two channel chains // set up two channel chains
Poco::AutoPtr<OwnPatternFormatter> pf = new OwnPatternFormatter(*this); Poco::AutoPtr<OwnPatternFormatter> pf = new OwnPatternFormatter(this);
pf->setProperty("times", "local"); pf->setProperty("times", "local");
Poco::AutoPtr<FormattingChannel> log = new FormattingChannel(pf); Poco::AutoPtr<FormattingChannel> log = new FormattingChannel(pf);
log_file = new FileChannel; log_file = new FileChannel;
@ -558,7 +558,7 @@ void BaseDaemon::buildLoggers()
std::cerr << "Should error logs to " << config().getString("logger.errorlog") << std::endl; std::cerr << "Should error logs to " << config().getString("logger.errorlog") << std::endl;
Poco::AutoPtr<Poco::LevelFilterChannel> level = new Poco::LevelFilterChannel; Poco::AutoPtr<Poco::LevelFilterChannel> level = new Poco::LevelFilterChannel;
level->setLevel(Message::PRIO_NOTICE); level->setLevel(Message::PRIO_NOTICE);
Poco::AutoPtr<OwnPatternFormatter> pf = new OwnPatternFormatter(*this); Poco::AutoPtr<OwnPatternFormatter> pf = new OwnPatternFormatter(this);
pf->setProperty("times", "local"); pf->setProperty("times", "local");
Poco::AutoPtr<FormattingChannel> errorlog = new FormattingChannel(pf); Poco::AutoPtr<FormattingChannel> errorlog = new FormattingChannel(pf);
error_log_file = new FileChannel; error_log_file = new FileChannel;
@ -575,7 +575,7 @@ void BaseDaemon::buildLoggers()
if (config().getBool("logger.use_syslog", false) || config().getBool("dynamic_layer_selection", false)) if (config().getBool("logger.use_syslog", false) || config().getBool("dynamic_layer_selection", false))
{ {
Poco::AutoPtr<OwnPatternFormatter> pf = new OwnPatternFormatter(*this, OwnPatternFormatter::ADD_LAYER_TAG); Poco::AutoPtr<OwnPatternFormatter> pf = new OwnPatternFormatter(this, OwnPatternFormatter::ADD_LAYER_TAG);
pf->setProperty("times", "local"); pf->setProperty("times", "local");
Poco::AutoPtr<FormattingChannel> log = new FormattingChannel(pf); Poco::AutoPtr<FormattingChannel> log = new FormattingChannel(pf);
syslog_channel = new Poco::SyslogChannel(commandName(), Poco::SyslogChannel::SYSLOG_CONS | Poco::SyslogChannel::SYSLOG_PID, Poco::SyslogChannel::SYSLOG_DAEMON); syslog_channel = new Poco::SyslogChannel(commandName(), Poco::SyslogChannel::SYSLOG_CONS | Poco::SyslogChannel::SYSLOG_PID, Poco::SyslogChannel::SYSLOG_DAEMON);
@ -592,7 +592,7 @@ void BaseDaemon::buildLoggers()
{ {
// Выводим на консоль // Выводим на консоль
Poco::AutoPtr<ConsoleChannel> file = new ConsoleChannel; Poco::AutoPtr<ConsoleChannel> file = new ConsoleChannel;
Poco::AutoPtr<OwnPatternFormatter> pf = new OwnPatternFormatter(*this); Poco::AutoPtr<OwnPatternFormatter> pf = new OwnPatternFormatter(this);
pf->setProperty("times", "local"); pf->setProperty("times", "local");
Poco::AutoPtr<FormattingChannel> log = new FormattingChannel(pf); Poco::AutoPtr<FormattingChannel> log = new FormattingChannel(pf);
log->setChannel(file); log->setChannel(file);