oflog: log4cplus ベースのロギングライブラリ
このモジュールには、ロギングのために使うクラスが含まれています。このライブラリは log4cplus をベースにしています。
主なクラスは次のとおりです(アルファベット順)。
- OFLog
- OFLogger
- dcmtk::log4cplus::ConsoleAppender
- dcmtk::log4cplus::FileAppender
- dcmtk::log4cplus::PatternLayout
ファイル
以下のファイルに、設定例と追加のドキュメントがあります。
使用例
次の例では、コンソールアプリケーションで oflog を使う方法を示します。
実際のロギング
まず、必要なヘッダーと定義を用意します。
// Naturally, we need the header for oflog
#include "dcmtk/oflog/oflog.h"
// Then we create our logger object. The argument is the name of the logger
// which can be used to configure it from the config file
OFLogger my_log = OFLog::getLogger("dcmtk.apps.sample");
ログ文を作るのに必要なのはこれだけです。あとは次のいずれかのマクロを使ってログエントリを生成できます。
OFLOG_FATAL(my_log, "This is a sample message of log level 'fatal'");
OFLOG_ERROR(my_log, "There are six log levels and each provides a OFLOG_level() macro");
OFLOG_WARN(my_log, "These macros are quite flexible");
OFLOG_INFO(my_log, "To output numbers like " << 5 << " you can use any iostream operations");
OFLOG_DEBUG(my_log, "Since iostreams are quite flexible themselves, a lot of stuff is possible");
OFLOG_TRACE(my_log, "hex numbers? 0x" << STD_NAMESPACE hex << 0x1234 << " and decimal numbers "
<< STD_NAMESPACE dec << 0x1234 << " are no problem at all");
ロギングの設定
上のコードはそれだけでも動作しますが、出力は見栄えがよくなく、柔軟さにも欠けます。OFCommandLine を使って oflog を設定するには、次のようにします。
// This is just an example
cmd.addGroup("general options:", LONGCOL, SHORTCOL);
// You own options here, e.g. --version
cmd.addOption("--version", "print version information and exit", OFCommandLine::AF_Exclusive);
// This call adds all of oflog's options, e.g. --debug and --quiet
OFLog::addOptions(cmd);
OFConsoleApplication::parseCommandLine() を呼び出したあとは、たった1回の呼び出しで oflog を設定できます。
OFLog::configureFromCommandLine(cmd, app);
ロガーの設定はこれだけで済み、–verbose や –log-config といったオプションが使えるようになります。
あるいは OFLog::configure() を使うこともできますが、この方法では –log-level や –log-config のような柔軟さは得られません。
ログメッセージの既定のパターンは "%P: %m%n" です。これは、ログレベルの先頭1文字(例: デバッグなら "D"、エラーなら "E")、コロン、メッセージ、改行の順、という意味です。