Boost.Logが採択されました

Boost.Log formal review result


ロギングライブラリ、Boost.Logが採択されました。
レビュー時に見つかった問題点を修正した後、正式にBoost入りすることになります。


ドキュメント

ダウンロード(BoostLog.zip)


Boost.Logは以下のような特徴があります:

  • シンプルなロギング処理
  • ログレベルの設定
  • フォーマットの設定(自作フォーマット、CSVXML等)
  • 非同期出力
  • イベントログ


サンプル:

#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>

#include <boost/log/common.hpp>
#include <boost/log/formatters.hpp>
#include <boost/log/filters.hpp>

#include <boost/log/utility/init/to_file.hpp>
#include <boost/log/utility/init/to_console.hpp>
#include <boost/log/utility/init/common_attributes.hpp>

#include <boost/log/attributes/timer.hpp>

namespace logging = boost::log;
namespace fmt = boost::log::formatters;
namespace flt = boost::log::filters;
namespace sinks = boost::log::sinks;
namespace attrs = boost::log::attributes;
namespace src = boost::log::sources;
namespace keywords = boost::log::keywords;

using boost::shared_ptr;

// Here we define our application severity levels.
enum severity_level
{
    normal,
    notification,
    warning,
    error,
    critical
};

// The formatting logic for the severity level
template< typename CharT, typename TraitsT >
inline std::basic_ostream< CharT, TraitsT >& operator<< (
    std::basic_ostream< CharT, TraitsT >& strm, severity_level lvl)
{
    static const char* const str[] =
    {
        "normal",
        "notification",
        "warning",
        "error",
        "critical"
    };
    if (static_cast< std::size_t >(lvl) < (sizeof(str) / sizeof(*str)))
        strm << str[lvl];
    else
        strm << static_cast< int >(lvl);
    return strm;
}

int main(int argc, char* argv[])
{
    // This is a simple tutorial/example of Boost.Log usage

    // The first thing we have to do to get using the library is
    // to set up the logging sinks - i.e. where the logs will be written to.
    logging::init_log_to_console(std::clog, keywords::format = "%TimeStamp%: %_%");

    // One can also use lambda expressions to setup filters and formatters
    logging::init_log_to_file
    (
        "sample.log",
        keywords::filter = flt::attr< severity_level >("Severity", std::nothrow) >= warning,
        keywords::format = fmt::format("%1% [%2%] <%3%> %4%")
            % fmt::date_time("TimeStamp", std::nothrow)
            % fmt::time_duration("Uptime", std::nothrow)
            % fmt::attr< severity_level >("Severity", std::nothrow)
            % fmt::message()
    );

    // Also let's add some commonly used attributes, like timestamp and record counter.
    logging::add_common_attributes();

    // Now our logs will be written both to the console and to the file.
    // Let's do a quick test and output something. We have to create a logger for this.
    src::logger lg;

    // And output...
    BOOST_LOG(lg) << "Hello, World!";

    // Now, let's try logging with severity
    src::severity_logger< severity_level > slg;

    // Let's pretend we also want to profile our code, so add a special timer attribute.
    slg.add_attribute("Uptime", boost::make_shared< attrs::timer >());

    BOOST_LOG_SEV(slg, normal) << "A normal severity message, will not pass to the file";
    BOOST_LOG_SEV(slg, warning) << "A warning severity message, will pass to the file";
    BOOST_LOG_SEV(slg, error) << "An error severity message, will pass to the file";
}