Opened 7 years ago

Closed 7 years ago

#11686 closed Bugs (invalid)

boost::posix_time::ptime custom facet doesn't work with BOOST_LOG_TRIVIAL

Reported by: anonymous Owned by: Andrey Semashev
Milestone: To Be Determined Component: log
Version: Boost 1.58.0 Severity: Problem
Keywords: Cc:

Description

Here's my program, I expect BOOST_LOG_TRIVIAL to print HH:MM:SS value.

cout.imblue does it correctly.

Not sure why

std::ostream& operator<<(std::ostream& os, const boost::posix_time::ptime& o)

is never called for

BOOST_LOG_TRIVIAL << ptime

Is this a bug in library?

#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;

class my_ptime_facet : public std::locale::facet { public:

enum option{

use_hhmmss ,....

};

static std::locale::id id; my_ptime_facet(option o=use_hhmmss): facet(0),

_option(o)

{ }; option get_option() const {return _option;};

protected:

option _option;

};

std::locale::id my_ptime_facet::id; Facet family unique id

std::ostream& operator<<(std::ostream& os, const boost::posix_time::ptime& o) {

std::locale const& l = os.getloc(); if( std::has_facet<my_ptime_facet>(l) ){

my_ptime_facet const& f = std::use_facet<my_ptime_facet>(l); switch(f.get_option()){ case my_ptime_facet::use_hhmmss:

os << o.time_of_day().hours() << ":" << o.time_of_day().minutes() << ":" << o.time_of_day().seconds() ; break;

} return os;

} return os;

}

int main() {

logging::add_common_attributes();

boost::posix_time::ptime t1(boost::posix_time::time_from_string("2014-11-23 23:59:59.117")); std::locale custom_locale ( std::locale(), new my_ptime_facet(my_ptime_facet::use_hhmmss) );

auto sink = logging::add_console_log(std::cout);

sink->imbue(custom_locale); std::cout.imbue(custom_locale);

std::cout<< t1 <<std::endl; prints 23:59:59 BOOST_LOG_TRIVIAL(info) << t1; prints 2014-Nov-23 23:59:59.117000, Why Not 23:59:59

}

Change History (1)

comment:1 by Andrey Semashev, 7 years ago

Resolution: invalid
Status: newclosed

Your code is incorrect, see here.

Note: See TracTickets for help on using tickets.