当我使用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()
?
setfill('0')
吗?不需要。 - malat