需要为使用BOOST_LOG_TRIVIAL打印的posix_time :: ptime设置自定义面。假设将默认构面更改为“%Y - %m - %d%H:%M:%S”。 我试图用新的语言环境灌输日志接收器,但没有成功。 当应用于std :: cout时,相同的imbueing技巧非常适用。 可能是什么问题,有没有办法处理它?BOOST_LOG_TRIVIAL和boost :: posix_time :: ptime输出格式化
#include <boost/log/trivial.hpp>
#include <boost/log/utility/setup.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <locale>
namespace logging = boost::log;
namespace pt = boost::posix_time;
void main(void)
{
// console sink
auto sink = logging::add_console_log(std::cout);
// standart output
boost::posix_time::ptime t1(boost::posix_time::time_from_string("2014-11-23 23:59:59.117"));
BOOST_LOG_TRIVIAL(info) << "before " << t1;
// adding custom facet...
pt::time_facet *facet = new boost::posix_time::time_facet("%Y--%m--%d %H:%M:%S");
sink->imbue(std::locale(sink->getloc(), facet));
// ... but output doesn't change
BOOST_LOG_TRIVIAL(info) << "after " << t1;
}
定制全球语言环境的方法对我来说效果很好。谢谢你的详细解答。 –