[Boost-bugs] [Boost C++ Libraries] #11686: boost::posix_time::ptime custom facet doesn't work with BOOST_LOG_TRIVIAL

Subject: [Boost-bugs] [Boost C++ Libraries] #11686: boost::posix_time::ptime custom facet doesn't work with BOOST_LOG_TRIVIAL
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2015-09-26 02:46:28


#11686: boost::posix_time::ptime custom facet doesn't work with BOOST_LOG_TRIVIAL
------------------------------+---------------------
 Reporter: anonymous | Owner: andysem
     Type: Bugs | Status: new
Milestone: To Be Determined | Component: log
  Version: Boost 1.58.0 | Severity: Problem
 Keywords: |
------------------------------+---------------------
 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
 }

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/11686>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:19 UTC