2016-10-04 09:58:38 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <Poco/PatternFormatter.h>
|
|
|
|
|
#include <Poco/Ext/ThreadNumber.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
|
#include <IO/WriteBufferFromString.h>
|
|
|
|
|
#include <IO/WriteHelpers.h>
|
2016-10-04 09:58:38 +00:00
|
|
|
|
#include <daemon/BaseDaemon.h>
|
|
|
|
|
|
2017-02-07 16:37:56 +00:00
|
|
|
|
#include <experimental/optional>
|
|
|
|
|
#include <functional>
|
|
|
|
|
|
2016-10-04 09:58:38 +00:00
|
|
|
|
|
|
|
|
|
/** Форматирует по своему.
|
|
|
|
|
* Некоторые детали невозможно получить, используя только Poco::PatternFormatter.
|
|
|
|
|
*
|
|
|
|
|
* Во-первых, используется номер потока не среди потоков Poco::Thread,
|
|
|
|
|
* а среди всех потоков, для которых был получен номер (см. ThreadNumber.h)
|
|
|
|
|
*
|
|
|
|
|
* Во-вторых, корректно выводится локальная дата и время.
|
|
|
|
|
* Poco::PatternFormatter плохо работает с локальным временем,
|
|
|
|
|
* в ситуациях, когда в ближайшем будущем намечается отмена или введение daylight saving time.
|
|
|
|
|
* - см. исходники Poco и http://thread.gmane.org/gmane.comp.time.tz/8883
|
|
|
|
|
*
|
|
|
|
|
* Также сделан чуть более эффективным (что имеет мало значения).
|
|
|
|
|
*/
|
|
|
|
|
class OwnPatternFormatter : public Poco::PatternFormatter
|
|
|
|
|
{
|
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
|
enum Options
|
|
|
|
|
{
|
|
|
|
|
ADD_NOTHING = 0,
|
|
|
|
|
ADD_LAYER_TAG = 1 << 0
|
|
|
|
|
};
|
2016-10-04 09:58:38 +00:00
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
OwnPatternFormatter(const BaseDaemon * daemon_, Options options_ = ADD_NOTHING) : Poco::PatternFormatter(""), daemon(daemon_), options(options_) {}
|
2017-02-06 14:57:47 +00:00
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
void format(const Poco::Message & msg, std::string & text) override
|
|
|
|
|
{
|
|
|
|
|
DB::WriteBufferFromString wb(text);
|
2016-10-04 09:58:38 +00:00
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
/// For syslog: tag must be before message and first whitespace.
|
|
|
|
|
if (options & ADD_LAYER_TAG && daemon)
|
|
|
|
|
{
|
|
|
|
|
auto layer = daemon->getLayer();
|
|
|
|
|
if (layer)
|
|
|
|
|
{
|
|
|
|
|
writeCString("layer[", wb);
|
|
|
|
|
DB::writeIntText(*layer, wb);
|
|
|
|
|
writeCString("]: ", wb);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-04 09:58:38 +00:00
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
/// Output time with microsecond resolution.
|
|
|
|
|
timeval tv;
|
|
|
|
|
if (0 != gettimeofday(&tv, nullptr))
|
|
|
|
|
DB::throwFromErrno("Cannot gettimeofday");
|
2016-10-04 09:58:38 +00:00
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
/// Change delimiters in date for compatibility with old logs.
|
|
|
|
|
DB::writeDateTimeText<'.', ':'>(tv.tv_sec, wb);
|
2016-10-04 09:58:38 +00:00
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
DB::writeChar('.', wb);
|
|
|
|
|
DB::writeChar('0' + ((tv.tv_usec / 100000) % 10), wb);
|
|
|
|
|
DB::writeChar('0' + ((tv.tv_usec / 10000) % 10), wb);
|
|
|
|
|
DB::writeChar('0' + ((tv.tv_usec / 1000) % 10), wb);
|
|
|
|
|
DB::writeChar('0' + ((tv.tv_usec / 100) % 10), wb);
|
|
|
|
|
DB::writeChar('0' + ((tv.tv_usec / 10) % 10), wb);
|
|
|
|
|
DB::writeChar('0' + ((tv.tv_usec / 1) % 10), wb);
|
2016-10-04 09:58:38 +00:00
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
|
writeCString(" [ ", wb);
|
|
|
|
|
DB::writeIntText(Poco::ThreadNumber::get(), wb);
|
|
|
|
|
writeCString(" ] <", wb);
|
|
|
|
|
DB::writeString(getPriorityName(static_cast<int>(msg.getPriority())), wb);
|
|
|
|
|
writeCString("> ", wb);
|
|
|
|
|
DB::writeString(msg.getSource(), wb);
|
|
|
|
|
writeCString(": ", wb);
|
|
|
|
|
DB::writeString(msg.getText(), wb);
|
|
|
|
|
}
|
2016-10-04 09:58:38 +00:00
|
|
|
|
|
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
|
const BaseDaemon * daemon;
|
|
|
|
|
Options options;
|
2016-10-04 09:58:38 +00:00
|
|
|
|
};
|