目前最新的穩定版是1.1:http://sourceforge.net/projects/boost-log/files/
要用之前要引入需要的檔案,
注意:引入filesystem.hpp是因為它不會自動連結filesystem的lib。

#include <boost/filesystem.hpp>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/filters.hpp>
#include <boost/log/sinks.hpp>
#include <boost/log/common.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/utility/string_literal.hpp>
#include <boost/log/utility/type_info_wrapper.hpp>
#include <boost/log/utility/init/to_console.hpp>
#include <boost/log/utility/init/to_file.hpp>
#include <boost/log/utility/init/common_attributes.hpp>
#include <boost/log/utility/init/filter_parser.hpp>
#include <boost/log/utility/init/formatter_parser.hpp>
#include <boost/log/formatters/stream.hpp>
#include <boost/log/formatters/format.hpp>
#include <boost/log/formatters/attr.hpp>
#include <boost/log/formatters/date_time.hpp>
#include <boost/log/formatters/named_scope.hpp>
#include <boost/log/formatters/if.hpp>
#include <boost/log/formatters/message.hpp>
#include <boost/log/formatters/xml_decorator.hpp>

可以照說明定義出下列短名稱空間

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

接下來就可以開始Trival了,
難度從上到下,分別由低至高,

BOOST_LOG_TRIVIAL(trace) << "A trace severity message";
BOOST_LOG_TRIVIAL(debug) << "A debug severity message";
BOOST_LOG_TRIVIAL(info) << "An informational severity message";
BOOST_LOG_TRIVIAL(warning) << "A warning severity message";
BOOST_LOG_TRIVIAL(error) << "An error severity message";
BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message";

比如說要限制程度大於info的log才算log,
可以這樣設定

logging::core::get()->set_filter
        (
        flt::attr< logging::trivial::severity_level >("Severity") >= logging::trivial::info
        );


logging::init_log_to_file("sample.log");

可以設定將log存在"sample.log"

log檔:

A trace severity message
A debug severity message
An informational severity message
A warning severity message
An error severity message
A fatal severity message

也可以設定的詳細一點

logging::init_log_to_file
(
        keywords::file_name = "sample_%N.log",                  // file name pattern
        keywords::rotation_size = 10 * 1024 * 1024,             // rotate files every 10 MiB...
        // ...or at midnight
        keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
        keywords::format = "[%TimeStamp%]: %_%"                 // log record format
);

sample_%N.log其中%N會被代換成數字,表示第幾個log檔。
log檔:

[2012-Feb-02 18:39:32.368584]: A trace severity message
[2012-Feb-02 18:39:32.379584]: A debug severity message
[2012-Feb-02 18:39:32.380584]: An informational severity message
[2012-Feb-02 18:39:32.381584]: A warning severity message
[2012-Feb-02 18:39:32.381584]: An error severity message
[2012-Feb-02 18:39:32.382584]: A fatal severity message

另外一種
怕程式當掉就沒有記錄的話記得加這行參數把auto_flush打開
keywords::auto_flush = true

keywords::format =
        (
        fmt::stream
        << fmt::attr< unsigned int >("LineID")
        << ": <" << fmt::attr< logging::trivial::severity_level >("Severity")
        << "> " << fmt::message()
        )

log檔:

1: <trace> A trace severity message
2: <debug> A debug severity message
3: <info> An informational severity message
4: <warning> A warning severity message
5: <error> An error severity message
6: <fatal> A fatal severity message

再加一個log檔,一次產生兩個log檔

typedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink;
    boost::shared_ptr< text_sink > pSink = boost::make_shared< text_sink >();

    pSink->locked_backend()->add_stream(
        boost::make_shared< std::ofstream >("sample.log"));

    pSink->locked_backend()->set_formatter
        (
        fmt::stream
        // line id will be written in hex, 8-digits, zero-filled
        << fmt::attr< unsigned int >("LineID", keywords::format = "%08x")
        << ": <" << fmt::attr< logging::trivial::severity_level >("Severity")
        << "> " << fmt::message()
        );

    logging::core::get()->add_sink(pSink);

%08x是8位數數字,不足前面補0
%8x 沒有0的話就用補空白
怕程式當掉就沒有記錄的話記得加這行把auto_flush打開
pSink->locked_backend()->auto_flush(true);
新產生的log檔:

00000001: <trace> A trace severity message
00000002: <debug> A debug severity message
00000003: <info> An informational severity message
00000004: <warning> A warning severity message
00000005: <error> An error severity message
00000006: <fatal> A fatal severity message

覺得不夠用嗎?
可以試試下面這個範例

#include <boost/lambda/lambda.hpp>

void my_formatter(std::ostream& strm, logging::record const& rec)
{
    namespace lambda = boost::lambda;

    unsigned int line_id;
    if (logging::extract< unsigned int >(
        "LineID", rec.attribute_values(), lambda::var(line_id) = lambda::_1))
    {
        strm << line_id << ": ";
    }

    logging::trivial::severity_level severity;
    if (logging::extract< logging::trivial::severity_level >(
        "Severity", rec.attribute_values(), lambda::var(severity) = lambda::_1))
    {
        strm << "<" << severity << "> ";
    }

    strm << rec.message();
}

typedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink;
    boost::shared_ptr< text_sink > pSink = boost::make_shared< text_sink >();
    pSink->locked_backend()->add_stream(
        boost::make_shared< std::ofstream >("sample.log"));
    pSink->locked_backend()->set_formatter(&my_formatter);
    logging::core::get()->add_sink(pSink);

裡面只有一個重點,
logging::extract< unsigned int >(
        "LineID", rec.attribute_values(), lambda::var(line_id) = lambda::_1)
logging::extract< logging::trivial::severity_level >(
        "Severity", rec.attribute_values(), lambda::var(severity) = lambda::_1)
就是這樣把LineID跟Severity拿出來而已。

接下來介紹如何用使用logger
首先用他的巨集宣告logger
BOOST_LOG_DECLARE_GLOBAL_LOGGER(my_logger, src::logger_mt)

BOOST_LOG_DECLARE_GLOBAL_LOGGER_INIT(my_logger, src::logger_mt)
{
    // do something on logger initialization and return logger instance
    return src::logger_mt();
}
這個巨集會產生get_my_logger函數
之後就可以照範例上一樣,加入文字到log檔

src::logger_mt& lg = get_my_logger();
//src::logger_mt& lg = my_logger::get();
logging::record rec = lg.open_record();
if (rec)
{
        rec.message() = "Hello world!";
        lg.push_record(rec);
}

跟之前的合起來的結果

Hello world!
1: <trace> A trace severity message
2: <debug> A debug severity message
3: <info> An informational severity message
4: <warning> A warning severity message
5: <error> An error severity message
6: <fatal> A fatal severity message

也可以用
BOOST_LOG(lg) << "Hello, World!";
在 src::logger_mt& lg = get_my_logger();
宣告後就直接使用。

arrow
arrow
    全站熱搜

    讓地獄深紅的天亮 發表在 痞客邦 留言(0) 人氣()