lys861205
lys861205

Reputation: 13

boost log, why not print threadid and processid

I'm having trouble using the boost log library, as follows:

BOOST_LOG_ATTRIBUTE_KEYWORD(log_severity, "Severity", SeverityLevel)
BOOST_LOG_ATTRIBUTE_KEYWORD(log_timestamp, "TimeStamp", boost::posix_time::ptime)
BOOST_LOG_ATTRIBUTE_KEYWORD(log_uptime, "Uptime", attrs::timer::value_type)
BOOST_LOG_ATTRIBUTE_KEYWORD(log_scope, "Scope", attrs::named_scope::value_type)
BOOST_LOG_ATTRIBUTE_KEYWORD(line_id, "LineID", unsigned int)
BOOST_LOG_ATTRIBUTE_KEYWORD(thread_id, "ThreadID", unsigned int)
BOOST_LOG_ATTRIBUTE_KEYWORD(process_id, "ProcessID", unsigned int)

    auto file_sink=logging::add_file_log
        (
        keywords::file_name="%Y-%m-%d_%N.log",     
        keywords::rotation_size=100*1024*1024,       
        keywords::time_based_rotation=sinks::file::rotation_at_time_point(0,0,0)   
        );



    file_sink->set_filter(log_severity>= Log_Info);   

    file_sink->locked_backend()->scan_for_files();

    logging::formatter formatter = expr::stream << "[" << log_timestamp << "] [" << line_id << "] [" << log_scope << "] [" << log_severity << "] [" << thread_id << "] [" << process_id << "] " << expr::message;

    file_sink->set_formatter(formatter);

    file_sink->locked_backend()->auto_flush(true);
    logging::core::get()->add_sink(console_sink);
    logging::add_common_attributes();

My call to the mian function is as follows:

src::severity_logger<SeverityLevel>lg;
BOOST_LOG_FUNCTION();
BOOST_LOG_SEV(lg, Log_Info) << "====main";

output message:

enter image description here

Upvotes: 1

Views: 1501

Answers (1)

vasek
vasek

Reputation: 2839

You don't have to create custom attribute keywords ThreadID, ProcessID and TimeStamp. Just simply construct formatter with common attributes as follows:

logging::formatter formatter = expr::stream << 
    "[" << log_timestamp << 
    "] [" << line_id << 
    "] [" << log_scope << 
    "] [" << log_severity <<
    "] [" << logging::expressions::attr<logging::attributes::current_thread_id::value_type>("ThreadID") << 
    "] [" << logging::expressions::attr<logging::attributes::current_process_id::value_type>("ProcessID") << 
    "] " << expr::message;

However as Andrey pointed out, if you still want to create custom keywords that already exist, you have to specify their types:

BOOST_LOG_ATTRIBUTE_KEYWORD(thread_id, "ThreadID", logging::attributes::current_thread_id::value_type)
BOOST_LOG_ATTRIBUTE_KEYWORD(process_id, "ProcessID", logging::attributes::current_process_id::value_type)

Working example is here.

Upvotes: 3

Related Questions