发现一个不错的Poco博客,附带Logger Demo

传送门:POCO Fanatic: http://poco.roundsquare.net/

Demo地址:http://poco.roundsquare.net/2010/04/19/pocologger/

ScopedLogMessage.h

//
// Copied from http://poco.roundsquare.net/2010/04/19/pocologger/
//
#include <string>
#include <memory>

class ScopedLogMessage
{
public:
   ScopedLogMessage( const std::string& commonMsg,
            const std::string& startMsg,
            const std::string& endMsg);
   ~ScopedLogMessage();
   void Message(const std::string& msg) const;

private:
   ScopedLogMessage();
   ScopedLogMessage(const ScopedLogMessage&);
   ScopedLogMessage& operator = (const ScopedLogMessage&);

   class ScopedLogMessageImpl;
   std::auto_ptr<ScopedLogMessageImpl> m_pImpl;
};

ScopedLogMessage.cpp

//
// Copied from http://poco.roundsquare.net/2010/04/19/pocologger/
//
#include "ScopedLogMessage.h"

#include <Poco/Logger.h>
#include <Poco/Thread.h>
#include <Poco/Format.h>

class ScopedLogMessage::ScopedLogMessageImpl
{
public:
   ScopedLogMessageImpl(   const std::string& commonMsg,
            const std::string& startMsg,
            const std::string& endMsg) :
      m_endMsg (commonMsg + endMsg)
   {
      Poco::Logger::get(Poco::Logger::ROOT).information(
            ThreadIDString() + commonMsg + startMsg );
   }
   ~ScopedLogMessageImpl()
   {
      Poco::Logger::get(Poco::Logger::ROOT).information(
            ThreadIDString() + m_endMsg );
   }
   void Message(const std::string& msg) const
   {
      Poco::Logger::get(Poco::Logger::ROOT).information(
            ThreadIDString() + msg );
   }
private:
   ScopedLogMessageImpl();
   ScopedLogMessageImpl(const ScopedLogMessageImpl&);
   ScopedLogMessageImpl& operator = (const ScopedLogMessageImpl&);

   int GetThreadID(void) const
   {
      Poco::Thread* p_thread = Poco::Thread::current();
      return (0 == p_thread) ? 0:p_thread->id();
   }
   std::string ThreadIDString(void) const
   {
      return Poco::format("[%d] ", GetThreadID());
   }

   const std::string m_endMsg;
};
ScopedLogMessage::ScopedLogMessage( const std::string& commonMsg,
               const std::string& startMsg,
               const std::string& endMsg) :
   m_pImpl(new ScopedLogMessageImpl(commonMsg, startMsg, endMsg))
{
}

ScopedLogMessage::~ScopedLogMessage()
{
}

void ScopedLogMessage::Message(const std::string& msg) const
{
   m_pImpl->Message(msg);
}

ScopedLogMessageTest.cpp

//
// Copied from http://poco.roundsquare.net/2010/04/19/pocologger/
//
#include <Poco/Logger.h>
#include <Poco/PatternFormatter.h>
#include <Poco/FormattingChannel.h>
#include <Poco/ConsoleChannel.h>

#include "ScopedLogMessage.h"

void PrepareConsoleLogger( const std::string& name,
            int level=Poco::Message::PRIO_INFORMATION)
{
   Poco::FormattingChannel* pFCConsole =
      new Poco::FormattingChannel(new Poco::PatternFormatter("%t"));
   pFCConsole->setChannel(new Poco::ConsoleChannel);
   pFCConsole->open();

   Poco::Logger::create(name, pFCConsole, level);
}

int main(int /*argc*/, char** /*argv*/)
{
   PrepareConsoleLogger(Poco::Logger::ROOT, Poco::Message::PRIO_INFORMATION);

   ScopedLogMessage msg("ScopedLogMessage ", "start", "end");

   msg.Message("  doing something...");

   return 0;
}

 

编译:

[root@slayer pocologger]# g++ ScopedLogMessage.cpp ScopedLogMessageTest.cpp -lPocoFoundation -o slmt

结果:

[root@slayer pocologger]# ./slmt
[0] ScopedLogMessage start
[0]   doing something...
[0] ScopedLogMessage end

 

posted @ 2013-01-18 10:05  Leo Forest  阅读(1689)  评论(0编辑  收藏  举报