From 4376d61e89b932d722378d636b9354f7386ede2f Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Tue, 11 Jan 2011 18:24:15 +0000 Subject: [PATCH] Logging: logging thread number properly. --- libs/libcommon/src/daemon.cpp | 12 +++++----- .../Ext/PatternFormatterWithOwnThreadNumber.h | 23 +++++++++++++++++++ .../include/Poco/Ext/ThreadNumber.h | 17 ++++++++++++++ .../PatternFormatterWithOwnThreadNumber.cpp | 14 +++++++++++ .../src/ThreadNumber.cpp | 4 ++-- 5 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 libs/libpocoext/include/Poco/Ext/PatternFormatterWithOwnThreadNumber.h create mode 100644 libs/libpocoext/include/Poco/Ext/ThreadNumber.h create mode 100644 libs/libpocoext/src/PatternFormatterWithOwnThreadNumber.cpp rename libs/{libcommon => libpocoext}/src/ThreadNumber.cpp (84%) diff --git a/libs/libcommon/src/daemon.cpp b/libs/libcommon/src/daemon.cpp index 802a2927203..86bd8e3f6ec 100644 --- a/libs/libcommon/src/daemon.cpp +++ b/libs/libcommon/src/daemon.cpp @@ -16,7 +16,7 @@ #include #include #include -#include +#include #include #include #include @@ -36,7 +36,7 @@ using Poco::Logger; using Poco::AutoPtr; using Poco::Observer; -using Poco::PatternFormatter; +using Poco::PatternFormatterWithOwnThreadNumber; using Poco::FormattingChannel; using Poco::SplitterChannel; using Poco::ConsoleChannel; @@ -122,7 +122,7 @@ void Daemon::buildLoggers() SplitterChannel *split = new SplitterChannel(); // set up two channel chains - PatternFormatter *pf = new PatternFormatter(format); + PatternFormatterWithOwnThreadNumber *pf = new PatternFormatterWithOwnThreadNumber(format); pf->setProperty("times", "local"); FormattingChannel *log = new FormattingChannel(pf); FileChannel *file = new FileChannel(); @@ -139,7 +139,7 @@ void Daemon::buildLoggers() std::cerr << "Should error logs to " << config().getString("logger.errorlog") << std::endl; Poco::LevelFilterChannel *level = new Poco::LevelFilterChannel(); level->setLevel(Message::PRIO_NOTICE); - PatternFormatter *pf = new PatternFormatter(format); + PatternFormatterWithOwnThreadNumber *pf = new PatternFormatterWithOwnThreadNumber(format); pf->setProperty("times", "local"); FormattingChannel *errorlog = new FormattingChannel(pf); FileChannel *errorfile = new FileChannel(); @@ -161,7 +161,7 @@ void Daemon::buildLoggers() { // Выводим на консоль ConsoleChannel * file = new ConsoleChannel(); - PatternFormatter * pf = new PatternFormatter(format); + PatternFormatterWithOwnThreadNumber * pf = new PatternFormatterWithOwnThreadNumber(format); pf->setProperty("times", "local"); FormattingChannel * log = new FormattingChannel(pf); log->setChannel(file); @@ -175,7 +175,7 @@ void Daemon::buildLoggers() { // Выводим на консоль ConsoleChannel * file = new ConsoleChannel(); - PatternFormatter * pf = new PatternFormatter(format); + PatternFormatterWithOwnThreadNumber * pf = new PatternFormatterWithOwnThreadNumber(format); pf->setProperty("times", "local"); FormattingChannel * log = new FormattingChannel(pf); log->setChannel(file); diff --git a/libs/libpocoext/include/Poco/Ext/PatternFormatterWithOwnThreadNumber.h b/libs/libpocoext/include/Poco/Ext/PatternFormatterWithOwnThreadNumber.h new file mode 100644 index 00000000000..a4d22fd1347 --- /dev/null +++ b/libs/libpocoext/include/Poco/Ext/PatternFormatterWithOwnThreadNumber.h @@ -0,0 +1,23 @@ +#ifndef Ext_Foundation_PatternFormatter_INCLUDED +#define Ext_Foundation_PatternFormatter_INCLUDED + +#include + + +namespace Poco { + +/** Отличается от PatternFormatter тем, что использует номер потока не среди + * потоков Poco::Thread, а среди всех потоков, для которых был получен номер (см. ThreadNumber.h) + */ +class Foundation_API PatternFormatterWithOwnThreadNumber : public PatternFormatter +{ +public: + PatternFormatterWithOwnThreadNumber() {} + PatternFormatterWithOwnThreadNumber(const std::string & format) : PatternFormatter(format) {} + + void format(const Message & msg, std::string & text); +}; + +} + +#endif diff --git a/libs/libpocoext/include/Poco/Ext/ThreadNumber.h b/libs/libpocoext/include/Poco/Ext/ThreadNumber.h new file mode 100644 index 00000000000..432c2c313e7 --- /dev/null +++ b/libs/libpocoext/include/Poco/Ext/ThreadNumber.h @@ -0,0 +1,17 @@ +#ifndef THREAD_NUMBER_H +#define THREAD_NUMBER_H + +/** Последовательный номер потока, начиная с 1, среди тех потоков, для которых был получен этот номер. + * Используется при логгировании. + */ +namespace Poco +{ + +namespace ThreadNumber +{ + unsigned get(); +} + +} + +#endif diff --git a/libs/libpocoext/src/PatternFormatterWithOwnThreadNumber.cpp b/libs/libpocoext/src/PatternFormatterWithOwnThreadNumber.cpp new file mode 100644 index 00000000000..157a0eaba81 --- /dev/null +++ b/libs/libpocoext/src/PatternFormatterWithOwnThreadNumber.cpp @@ -0,0 +1,14 @@ +#include +#include + + +namespace Poco { + +void PatternFormatterWithOwnThreadNumber::format(const Message & msg, std::string & text) +{ + Poco::Message tmp_message(msg); + tmp_message.setTid(ThreadNumber::get()); + PatternFormatter::format(tmp_message, text); +} + +} diff --git a/libs/libcommon/src/ThreadNumber.cpp b/libs/libpocoext/src/ThreadNumber.cpp similarity index 84% rename from libs/libcommon/src/ThreadNumber.cpp rename to libs/libpocoext/src/ThreadNumber.cpp index abaec8c3d8a..95f296e2f6f 100644 --- a/libs/libcommon/src/ThreadNumber.cpp +++ b/libs/libpocoext/src/ThreadNumber.cpp @@ -1,13 +1,13 @@ #include #include -#include +#include static __thread unsigned thread_number = 0; static unsigned threads = 0; -unsigned ThreadNumber::get() +unsigned Poco::ThreadNumber::get() { static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; if (unlikely(!thread_number))