【发布时间】:2015-09-18 18:08:57
【问题描述】:
在使用 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()?
【问题讨论】: