2

Im trying to format the boost log output such that a field of choice will always be of a specific width for output log alignment reasons.

add_file_log(
        keywords::file_name = s.str(),
        keywords::rotation_size = log_info.log_file_size,
        keywords::max_size = log_info.log_file_amount * log_info.log_file_size,
        keywords::target = log_info.log_path,
        keywords::open_mode = std::ios::out | std::ios::app,
        keywords::auto_flush = true,
        keywords::format =
        expressions::format("[%1%] [%2%] [%3%] [%4%] %5%")
        % expressions::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S.%f")
        % expressions::attr<unsigned int>("ThreadID")
        % expressions::attr<string>("Scope")
        % trivial::severity
        % expressions::smessage
    );

I've tried different formats such as

  • "[%1%] [%2%] [%3%] [%|20t|%4%] %5%"
  • "[%1%] [%2%] [%3%] [%-20s] %5%"
  • trivial::severity << std::setw(20)

Many of my attempts threw the following error:

> Caught Exception in cyacollector main. Error: Unsupported format
> placeholder

1 Answers1

2

Currently, expressions::format in Boost.Log supports only positional placeholders in the form of %N%. This format does not allow additional parameters, such as width or precision. Mostly, this is because this formatter inserts already formatted strings into the format template, so most of the parameters cannot be applied at this point.

However, you can achieve what you want by modifying the arguments to format. You can use the combination of the max_size_decor decorator and std::setw manipulator to achieve the effect of a fixed width of each of the columns in the log output.

add_file_log(
    ...,
    keywords::format =
    expressions::format("[%1%] [%2%] [%3%] [%4%] %5%")
        % expressions::max_size_decor< char >(30)[ expressions::stream << std::setw(30) << expressions::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S.%f") ]
        % expressions::max_size_decor< char >(10)[ expressions::stream << std::setw(10) << expressions::attr<unsigned int>("ThreadID") ]
        % expressions::max_size_decor< char >(20)[ expressions::stream << std::setw(20) << expressions::attr<string>("Scope") ]
        % expressions::max_size_decor< char >(5)[ expressions::stream << std::setw(5) << trivial::severity ]
        % expressions::smessage
);
Andrey Semashev
  • 10,046
  • 1
  • 17
  • 27