Logging: logging thread number properly.

This commit is contained in:
Alexey Milovidov 2011-01-11 18:24:15 +00:00
parent 71c927c30e
commit 4376d61e89
5 changed files with 62 additions and 8 deletions

View File

@ -16,7 +16,7 @@
#include <Poco/RWLock.h>
#include <Poco/Logger.h>
#include <Poco/AutoPtr.h>
#include <Poco/PatternFormatter.h>
#include <Poco/Ext/PatternFormatterWithOwnThreadNumber.h>
#include <Poco/SplitterChannel.h>
#include <Poco/Ext/LevelFilterChannel.h>
#include <Poco/FormattingChannel.h>
@ -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);

View File

@ -0,0 +1,23 @@
#ifndef Ext_Foundation_PatternFormatter_INCLUDED
#define Ext_Foundation_PatternFormatter_INCLUDED
#include <Poco/PatternFormatter.h>
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

View File

@ -0,0 +1,17 @@
#ifndef THREAD_NUMBER_H
#define THREAD_NUMBER_H
/** Последовательный номер потока, начиная с 1, среди тех потоков, для которых был получен этот номер.
* Используется при логгировании.
*/
namespace Poco
{
namespace ThreadNumber
{
unsigned get();
}
}
#endif

View File

@ -0,0 +1,14 @@
#include <Poco/Ext/ThreadNumber.h>
#include <Poco/Ext/PatternFormatterWithOwnThreadNumber.h>
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);
}
}

View File

@ -1,13 +1,13 @@
#include <pthread.h>
#include <Yandex/optimization.h>
#include <Yandex/ThreadNumber.h>
#include <Poco/Ext/ThreadNumber.h>
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))