VisualStudio(VC9) でlog4cxxをつかう その3 VisualStudioのデバッグ出力へ
VisualStudio(VC9) でlog4cxxをつかう - braintag
VisualStudio(VC9) でlog4cxxをつかう その2 - braintag
これらの続きです。
今回は、log4cxxの出力を、VisualStudioのデバッグ出力にも出したいと思います。
"log4j.properties"でどうにかする方法は見つからなかったため、非常にめんどくさいです。
前回のソースから、以下のように修正します。
// logtest.cpp : コンソール アプリケーションのエントリ ポイントを定義します。 // #include "stdafx.h" #include <log4cxx/logger.h> //<--ここから追加 #include <log4cxx/propertyconfigurator.h> #include <log4cxx/patternlayout.h> #include <log4cxx/helpers/transcoder.h> #include <log4cxx/nt/outputdebugstringappender.h> //-->ここまで using namespace log4cxx; int _tmain(int argc, _TCHAR* argv[]) { LoggerPtr logger(Logger::getLogger("logtest")); //<--ここから追加 //ファイル指定読み込みしないと、既存のlogger動作が無視されたので追加 log4cxx::PropertyConfigurator::configure("./log4j.properties"); //パターンレイアウトの作成 log4cxx::LogString logstr; log4cxx::helpers::Transcoder::decode("%d %5p %l %c{1} - %m%n",logstr);//パターンレイアウト log4cxx::PatternLayoutPtr layoutPtr = new log4cxx::PatternLayout(logstr); //OutputDebugStringのappenderを作成 log4cxx::nt::OutputDebugStringAppender* pODSA = new log4cxx::nt::OutputDebugStringAppender(); //OutputDebugStringのappenderにパターンレイアウトのセット pODSA->setLayout(layoutPtr); logger->addAppender(pODSA);//OutputDebugStringのappenderを追加 //-->ここまで LOG4CXX_INFO(logger,"info") LOG4CXX_FATAL(logger,"fatal"); LOG4CXX_ERROR(logger,"error"); LOG4CXX_WARN(logger,"warn"); LOG4CXX_DEBUG(logger,"debug"); return 0; }
ビルド後、実行すると、VisualStudioのデバッグ出力に、以下のように出力されるようになりました。
ただし、"OutputDebugString"を使用しているので、リリースビルドでも出力されちゃいます。
リリースビルドで出力させたくない場合は、_DEBUGマクロでなんとかしてください。