コード例 #1
0
// The function registers two sinks - one for statistic information,
// and another one for other records
void init()
{
    boost::shared_ptr< logging::core > core = logging::core::get();

    // Create a backend and attach a stream to it
    boost::shared_ptr< sinks::text_ostream_backend > backend =
        boost::make_shared< sinks::text_ostream_backend >();
    backend->add_stream(
        boost::shared_ptr< std::ostream >(new std::ofstream("test.log")));

    // Create a frontend and setup filtering
    typedef sinks::synchronous_sink< sinks::text_ostream_backend > log_sink_type;
    boost::shared_ptr< log_sink_type > log_sink(new log_sink_type(backend));
    // All records that don't have a "StatisticStream" attribute attached
    // will go to the "test.log" file
    log_sink->set_filter(!expr::has_attr(stat_stream));

    core->add_sink(log_sink);

    // Create another sink that will receive all statistic data
    typedef sinks::synchronous_sink< my_stat_accumulator > stat_sink_type;
    boost::shared_ptr< stat_sink_type > stat_sink(new stat_sink_type());
    // All records with a "StatisticStream" string attribute attached
    // will go to the my_stat_accumulator sink
    stat_sink->set_filter(expr::has_attr(stat_stream));

    core->add_sink(stat_sink);
}
コード例 #2
0
ファイル: log.c プロジェクト: andysan/greenpea
void
lprintf(log_level_t level, const char *fmt, ...) {
    if (level < global_log_level)
        return;

    va_list ap1;
    va_list ap2;
    char *msg;
    int len;

    va_start(ap1, fmt);
    va_copy(ap2, ap1);

    len = vsnprintf(NULL, 0, fmt, ap1);

    msg = malloc(len + 1);
    if (msg) {
        vsnprintf(msg, len + 1, fmt, ap2);
        log_sink(level, msg, log_data);
        free(msg);
    }

    va_end(ap2);
    va_end(ap1);
}