Insert boost :: log :: expressions :: attr <std :: string> into std :: string

When using Boost.Log I am trying to keep the format TimeStamp

like:

  logging::add_file_log
    (
     keywords::file_name = "my.log",
     keywords::format =
     (
      expr::stream
      << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S")
      << "," << expr::attr< int >("Line")
      << " " << expr::attr< std::string >("File")
      << " " << logging::trivial::severity
      << " - " << expr::smessage
     )
    );

      

They say that I cannot use a different form of formatting as I am having difficulty converting "TimeStamp"

to my custom format:

static void my_formatter(logging::record_view const& rec, logging::formatting_ostream& strm)
{
  strm << logging::extract< boost::posix_time::ptime >("TimeStamp", rec);

      

displays something like: 2015-Jul-01 16:06:31.514053

while I was only interested in: "%Y-%m-%d %H:%M:%S"

. However, the first form is extremely difficult to use, for example, I cannot distinguish expr::attr< std::string >

from a simple one std::string

, for example:

  logging::add_file_log
    (
     keywords::file_name = "my.log",
     keywords::format =
     (
      expr::stream
      << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S")
      << "," << expr::attr< int >("Line")
      << " " << boost::filesystem::path(expr::attr< std::string >("File"))
        .filename().string()
      << " " << logging::trivial::severity
      << " - " << expr::smessage
     )
    );

      

The above code doesn't even compile.

Is there an easy way to both print TimeStamp

using my custom format and at the same time use a custom listing in the string to be able to use boost::filesystem::path::filename()

?

+3


source to share


2 answers


There are several ways to achieve what you want. The key point to understand is that the Boost.Log format expressions (as well as filters, by the way) are Boost.Phoenix lambda functions. This way, you can inject your own functions into them using Boost.Phoenix constructs like boost::phoenix::bind

. For example, an example is here . Your code will look something like this:

std::string file_basename(logging::value_ref< std::string > const& filename)
{
  // Check to see if the attribute value has been found
  if (filename)
    return boost::filesystem::path(filename.get()).filename().string();
  else
    return std::string();
}

// ...

logging::add_file_log
(
  keywords::file_name = "my.log",
  keywords::format =
  (
    expr::stream
      << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S")
      << "," << expr::attr< int >("Line")
      << " " << boost::phoenix::bind(&file_basename, expr::attr< std::string >("File"))
      << " " << logging::trivial::severity
      << " - " << expr::smessage
  )
);

      

Another way to do this is to use attribute keywords and define operator<<

for the File attribute. You can find it here .

BOOST_LOG_ATTRIBUTE_KEYWORD(a_timestamp, "TimeStamp", boost::posix_time::ptime)
BOOST_LOG_ATTRIBUTE_KEYWORD(a_line, "Line", int)
BOOST_LOG_ATTRIBUTE_KEYWORD(a_file, "File", std::string)

namespace std {

logging::formatting_ostream& operator<<
(
  logging::formatting_ostream& strm,
  logging::to_log_manip< std::string, tag::a_file > const& manip
)
{
  strm << boost::filesystem::path(manip.get()).filename().string();
  return strm;
}

} // namespace std

// ...

logging::add_file_log
(
  keywords::file_name = "my.log",
  keywords::format =
  (
    expr::stream
      << expr::format_date_time(a_timestamp, "%Y-%m-%d %H:%M:%S")
      << "," << a_line
      << " " << a_file
      << " " << logging::trivial::severity
      << " - " << expr::smessage
  )
);

      



Note that the attribute keywords make expressions much simpler.

Finally, you can use wrap_formatter

to inject your own function into the stream expression. When it comes to formatting, wrap_formatter

calls your function, providing it with a formatted log entry and formatting stream. When your function returns a wrapper, it automatically returns a reference to the formatting stream so that the rest of the formatting expression can continue.

BOOST_LOG_ATTRIBUTE_KEYWORD(a_timestamp, "TimeStamp", boost::posix_time::ptime)
BOOST_LOG_ATTRIBUTE_KEYWORD(a_line, "Line", int)
BOOST_LOG_ATTRIBUTE_KEYWORD(a_file, "File", std::string)

void file_basename(logging::record_view const& record, logging::formatting_ostream& strm)
{
  // Check to see if the attribute value has been found
  logging::value_ref< std::string, tag::a_file > filename = record[a_file];
  if (filename)
    strm << boost::filesystem::path(filename.get()).filename().string();
}

// ...

logging::add_file_log
(
  keywords::file_name = "my.log",
  keywords::format =
  (
    expr::stream
      << expr::format_date_time(a_timestamp, "%Y-%m-%d %H:%M:%S")
      << "," << a_line
      << " " << expr::wrap_formatter(&file_basename)
      << " " << logging::trivial::severity
      << " - " << expr::smessage
  )
);

      

The above option is similar to the first option c boost::phoenix::bind

, but provides more flexibility in implementation file_basename

.

+3


source


In custom formatting, you can easily create a timestamp in the format "%Y-%m-%d %H:%M:%S"

:



void my_formatter(logging::record_view const& rec, logging::formatting_ostream& strm)
{
    const boost::posix_time::ptime &pt = *logging::extract< boost::posix_time::ptime >("TimeStamp", rec);
    strm << pt.date() << " " << pt.time_of_day().hours() << ":" << pt.time_of_day().minutes() << ":" << pt.time_of_day().seconds()
    ...
    << rec[expr::smessage];
}

      

+4


source







All Articles