施放boost :: log :: expressions :: attr< std :: string>到std :: string

时间:2015-07-01 14:11:40

标签: c++ boost boost-log

在使用Boost.Log时,我正在尝试保留我的TimeStamp格式化程序,例如:

  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
     )
    );

据说我无法使用其他形式的格式化程序,因为将"TimeStamp"转换为自定义格式会遇到很多困难:

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

会输出类似:2015-Jul-01 16:06:31.514053的内容,而我只对"%Y-%m-%d %H:%M:%S"感兴趣。但是第一种形式非常难以使用,例如我无法将expr::attr< std::string >转换为简单的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::filesystem::path(expr::attr< std::string >("File"))
        .filename().string()
      << " " << logging::trivial::severity
      << " - " << expr::smessage
     )
    );

以上代码甚至无法编译。

是否有一种简单的方法可以使用我的自定义格式打印TimeStamp,同时使用自定义强制转换为字符串以便能够使用boost::filesystem::path::filename()

2 个答案:

答案 0 :(得分:5)

在自定义格式化程序中,您可以轻松地以"%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];
}

答案 1 :(得分:4)

有多种方法可以达到你想要的效果。要理解的关键点是Boost.Log格式化表达式(以及顺便说一下过滤器)是Boost.Phoenix lambda函数。因此,您可以使用Boost.Phoenix构造(例如boost::phoenix::bind)在其中注入您自己的函数。例如,请参阅示例here。您的代码看起来像这样:

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
  )
);

另一种方法是使用属性关键字并定义特定于File属性的operator<<。您可以找到示例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
  )
);

请注意,属性关键字可以显着简化表达式。

最后,您可以使用wrap_formatter将自己的函数注入流表达式。在格式化方面,wrap_formatter调用您的函数,为其提供格式化的日志记录和格式化流。当您的函数返回时,包装器会自动返回对格式化流的引用,以便格式化表达式的其余部分可以继续。

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
  )
);

上述内容类似于boost::phoenix::bind的第一个变体,但允许在file_basename实现中提供更大的灵活性。