後日スレッド関係の例を載せるつもりなので、それに備えて、まずはスレッドセーフなコンソールログ出力のクラスを紹介します。
class ScopedLogMessage
ScopedLogMessage.h
・PImpl イディオム(pointer to implementation idiom)を使用し、ファイルの依存関係を削減。
#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
・RAII イディオムを使い、コンストラクタで開始ログを、デストラクタで終了ログを出す。
・後に紹介する Poco::Util::ServerApplication 内で生成される Poco::Logger インスタンスの名称と同じ、
Poco::Logger::ROOT を使う。
・マルチスレッドの時用に、スレッドIDも表示。
#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
・Poco::Logger::ROOT という名称で Poco::ConsoleChannel に Poco::Message::PRIO_INFORMATION 以上
のログを出す Poco::Logger を生成し、ScopedLogMessage を呼ぶ。
#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; }
Results of execution
・全てメインスレッドでログ出力。
[0] ScopedLogMessage start [0] doing something... [0] ScopedLogMessage end
Downloads
・ここをクリックすると、makefile や VC++ プロジェクトなど一式がダウンロードできます。
(2010.06.05 11:53 updated)
・2010年4月20日からのダウンロード数:290
Subversion
・フリーの Subversion ホスティングサービス Assemblaで、ソースコードを管理しています。
Reference
・5分で使えるLoggingフレームワーク – POCO::Foundation -
・2. ロギングフレームワークの使い方
・http://pocoproject.org にある Logging のプレセンテーション。(PDF)
| Copyright © 2010 Round Square Inc. All rights reserved. |
|---|