Boost C++ Libraries: Ticket #11686: boost::posix_time::ptime custom facet doesn't work with BOOST_LOG_TRIVIAL https://svn.boost.org/trac10/ticket/11686 <p> Here's my program, I expect BOOST_LOG_TRIVIAL to print HH:MM:SS value. </p> <p> cout.imblue does it correctly. </p> <p> Not sure why </p> <p> std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const boost::posix_time::ptime&amp; o) </p> <p> is never called for </p> <p> BOOST_LOG_TRIVIAL &lt;&lt; ptime </p> <p> Is this a bug in library? </p> <p> #include &lt;boost/log/trivial.hpp&gt; #include &lt;boost/log/utility/setup.hpp&gt; #include &lt;boost/log/support/date_time.hpp&gt; #include &lt;boost/date_time/posix_time/posix_time.hpp&gt; #include &lt;locale&gt; </p> <p> namespace logging = boost::log; namespace pt = boost::posix_time; </p> <p> class my_ptime_facet : public std::locale::facet { public: </p> <blockquote> <p> enum option{ </p> <blockquote> <p> use_hhmmss <em>,.... </em></p> </blockquote> <p> }; </p> </blockquote> <blockquote> <p> static std::locale::id id; my_ptime_facet(option o=use_hhmmss): facet(0), </p> <blockquote> <p> _option(o) </p> </blockquote> <p> { }; option get_option() const {return _option;}; </p> </blockquote> <p> protected: </p> <blockquote> <p> option _option; </p> </blockquote> <p> }; </p> <p> std::locale::id my_ptime_facet::id; <em>Facet family unique id </em></p> <p> std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const boost::posix_time::ptime&amp; o) { </p> <blockquote> <p> std::locale const&amp; l = os.getloc(); if( std::has_facet&lt;my_ptime_facet&gt;(l) ){ </p> <blockquote> <p> my_ptime_facet const&amp; f = std::use_facet&lt;my_ptime_facet&gt;(l); switch(f.get_option()){ case my_ptime_facet::use_hhmmss: </p> <blockquote> <p> os &lt;&lt; o.time_of_day().hours() &lt;&lt; ":" &lt;&lt; o.time_of_day().minutes() &lt;&lt; ":" &lt;&lt; o.time_of_day().seconds() ; break; </p> </blockquote> <p> } return os; </p> </blockquote> <p> } return os; </p> </blockquote> <p> } </p> <p> int main() { </p> <blockquote> <p> logging::add_common_attributes(); </p> </blockquote> <blockquote> <p> 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) ); </p> </blockquote> <blockquote> <p> auto sink = logging::add_console_log(std::cout); </p> </blockquote> <blockquote> <p> sink-&gt;imbue(custom_locale); std::cout.imbue(custom_locale); </p> </blockquote> <blockquote> <p> std::cout&lt;&lt; t1 &lt;&lt;std::endl; <em>prints 23:59:59 BOOST_LOG_TRIVIAL(info) &lt;&lt; t1; </em>prints 2014-Nov-23 23:59:59.117000, Why Not 23:59:59 </p> </blockquote> <p> } </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/11686 Trac 1.4.3 Andrey Semashev Sat, 26 Sep 2015 09:41:04 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/11686#comment:1 https://svn.boost.org/trac10/ticket/11686#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> </ul> <p> Your code is incorrect, see <a class="ext-link" href="http://stackoverflow.com/questions/32524347/boost-log-stdexception-formatter-unable-to-find-operator-overload-in-own-nam/32538936#32538936"><span class="icon">​</span>here</a>. </p> Ticket