While using Boost.Log, I am trying to keep my TimeStamp
formatter such as:
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
)
);
It is said that I cannot use the other form of formatter since I'll have much difficulties turning "TimeStamp"
into 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);
will output something like: 2015-Jul-01 16:06:31.514053
, while I am only interested in: "%Y-%m-%d %H:%M:%S"
. However the first form is extremely hard to use, for instance I am not able to cast an expr::attr< std::string >
to a simple std::string
for instance:
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 does not even compile.
Is there an easy way to both print TimeStamp
using my custom format and at the same time use a custom cast to string to be able to use boost::filesystem::path::filename()
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…