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()
?
In custom formatter you can easily build the timestamp in "%Y-%m-%d %H:%M:%S"
format:
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];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With