Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r85696 - in trunk: boost/log/detail libs/log/doc libs/log/src
From: andrey.semashev_at_[hidden]
Date: 2013-09-16 01:50:50


Author: andysem
Date: 2013-09-16 01:50:50 EDT (Mon, 16 Sep 2013)
New Revision: 85696
URL: http://svn.boost.org/trac/boost/changeset/85696

Log:
Fixed thread id formatting discrepancies between the default sink and formatters.

Text files modified:
   trunk/boost/log/detail/thread_id.hpp | 2 +-
   trunk/libs/log/doc/changelog.qbk | 1 +
   trunk/libs/log/src/default_sink.cpp | 25 +++++++++++++++++++++----
   trunk/libs/log/src/thread_id.cpp | 23 ++++++++++++++++++++++-
   4 files changed, 45 insertions(+), 6 deletions(-)

Modified: trunk/boost/log/detail/thread_id.hpp
==============================================================================
--- trunk/boost/log/detail/thread_id.hpp Mon Sep 16 00:32:09 2013 (r85695)
+++ trunk/boost/log/detail/thread_id.hpp 2013-09-16 01:50:50 EDT (Mon, 16 Sep 2013) (r85696)
@@ -48,7 +48,7 @@
 //! The function returns current thread identifier
 BOOST_LOG_API thread::id const& get_id();
 
-} // namespace this_process
+} // namespace this_thread
 
 template< typename CharT, typename TraitsT >
 BOOST_LOG_API std::basic_ostream< CharT, TraitsT >&

Modified: trunk/libs/log/doc/changelog.qbk
==============================================================================
--- trunk/libs/log/doc/changelog.qbk Mon Sep 16 00:32:09 2013 (r85695)
+++ trunk/libs/log/doc/changelog.qbk 2013-09-16 01:50:50 EDT (Mon, 16 Sep 2013) (r85696)
@@ -23,6 +23,7 @@
 
 * Fixed [link log.detailed.attributes.timer `timer`] attribute generating incorrect time readings on Windows on heavy thread contention when `QueryPerformanceCounter` API was used.
 * Fixed a bug in the filter parser that prevented using parsed filters with some attributes. For example, parsed filters didn't work with a string-typed attribute value, if the value was compared to a numeric operand.
+* Fixed thread id formatting discrepancies between the default sink and formatters.
 * Closed tickets: [ticket 8815], [ticket 8819], [ticket 8915], [ticket 8917].
 
 [heading 2.1, Boost 1.54]

Modified: trunk/libs/log/src/default_sink.cpp
==============================================================================
--- trunk/libs/log/src/default_sink.cpp Mon Sep 16 00:32:09 2013 (r85695)
+++ trunk/libs/log/src/default_sink.cpp 2013-09-16 01:50:50 EDT (Mon, 16 Sep 2013) (r85696)
@@ -31,6 +31,13 @@
 
 BOOST_LOG_OPEN_NAMESPACE
 
+namespace aux {
+
+// Defined in thread_id.cpp
+void format_thread_id(char* buf, std::size_t size, thread::id tid);
+
+} // namespace aux
+
 namespace sinks {
 
 namespace aux {
@@ -111,11 +118,16 @@
 
     result_type operator() (std::string const& msg) const
     {
+#if !defined(BOOST_LOG_NO_THREADS)
+ char thread_id_buf[64];
+ boost::log::aux::format_thread_id(thread_id_buf, sizeof(thread_id_buf), boost::log::aux::this_thread::get_id());
+#endif
+
         const decomposed_time_point now = date_time::microsec_clock< decomposed_time_point >::local_time();
 
         std::printf("[%04u-%02u-%02u %02u:%02u:%02u.%06u] "
 #if !defined(BOOST_LOG_NO_THREADS)
- "[0x%08x] "
+ "[%s] "
 #endif
                     "%s %s\n",
             static_cast< unsigned int >(now.date.year),
@@ -126,7 +138,7 @@
             static_cast< unsigned int >(now.time.seconds),
             static_cast< unsigned int >(now.time.useconds),
 #if !defined(BOOST_LOG_NO_THREADS)
- static_cast< unsigned int >(boost::log::aux::this_thread::get_id().native_id()),
+ thread_id_buf,
 #endif
             severity_level_to_string(m_level),
             msg.c_str());
@@ -138,11 +150,16 @@
 
     result_type operator() (std::wstring const& msg) const
     {
+#if !defined(BOOST_LOG_NO_THREADS)
+ char thread_id_buf[64];
+ boost::log::aux::format_thread_id(thread_id_buf, sizeof(thread_id_buf), boost::log::aux::this_thread::get_id());
+#endif
+
         const decomposed_time_point now = date_time::microsec_clock< decomposed_time_point >::local_time();
 
         std::printf("[%04u-%02u-%02u %02u:%02u:%02u.%06u] "
 #if !defined(BOOST_LOG_NO_THREADS)
- "[0x%08x] "
+ "[%s] "
 #endif
                     "%s %ls\n",
             static_cast< unsigned int >(now.date.year),
@@ -153,7 +170,7 @@
             static_cast< unsigned int >(now.time.seconds),
             static_cast< unsigned int >(now.time.useconds),
 #if !defined(BOOST_LOG_NO_THREADS)
- static_cast< unsigned int >(boost::log::aux::this_thread::get_id().native_id()),
+ thread_id_buf,
 #endif
             severity_level_to_string(m_level),
             msg.c_str());

Modified: trunk/libs/log/src/thread_id.cpp
==============================================================================
--- trunk/libs/log/src/thread_id.cpp Mon Sep 16 00:32:09 2013 (r85695)
+++ trunk/libs/log/src/thread_id.cpp 2013-09-16 01:50:50 EDT (Mon, 16 Sep 2013) (r85696)
@@ -226,6 +226,27 @@
 
 } // namespace this_thread
 
+// Used in default_sink.cpp
+void format_thread_id(char* buf, std::size_t size, thread::id tid)
+{
+ static const char char_table[] = "0123456789abcdef";
+
+ // Input buffer is assumed to be always larger than 2 chars
+ *buf++ = '0';
+ *buf++ = 'x';
+
+ size -= 3; // reserve space for the terminating 0
+ thread::id::native_type id = tid.native_id();
+ unsigned int i = 0;
+ const unsigned int n = (size > (tid_size * 2u)) ? static_cast< unsigned int >(tid_size * 2u) : static_cast< unsigned int >(size);
+ for (unsigned int shift = n * 4u; i < n; ++i, shift -= 4u)
+ {
+ buf[i] = char_table[(id >> shift) & 15u];
+ }
+
+ buf[i] = '\0';
+}
+
 template< typename CharT, typename TraitsT >
 std::basic_ostream< CharT, TraitsT >&
 operator<< (std::basic_ostream< CharT, TraitsT >& strm, thread::id const& tid)
@@ -233,8 +254,8 @@
     if (strm.good())
     {
         io::ios_flags_saver flags_saver(strm, (strm.flags() & std::ios_base::uppercase) | std::ios_base::hex | std::ios_base::internal | std::ios_base::showbase);
- io::ios_width_saver width_saver(strm, static_cast< std::streamsize >(tid_size * 2 + 2)); // 2 chars per byte + 2 chars for the leading 0x
         io::basic_ios_fill_saver< CharT, TraitsT > fill_saver(strm, static_cast< CharT >('0'));
+ strm.width(static_cast< std::streamsize >(tid_size * 2 + 2)); // 2 chars per byte + 2 chars for the leading 0x
         strm << static_cast< uint_t< tid_size * 8 >::least >(tid.native_id());
     }
 


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk