2
0
mirror of https://github.com/boostorg/log.git synced 2026-02-13 12:32:14 +00:00

Fixes #10926. Generalized id formatting.

Also, different hex-based formatting routines now use the common
character table, which should reduce binary size a little.
This commit is contained in:
Andrey Semashev
2015-01-25 17:27:41 +03:00
parent 2afb87a3d9
commit 59f5bcdcad
7 changed files with 81 additions and 46 deletions

View File

@@ -39,6 +39,7 @@
#if !defined(BOOST_LOG_USE_COMPILER_TLS)
#include <boost/log/detail/singleton.hpp>
#endif
#include "id_formatting.hpp"
#include <boost/log/detail/header.hpp>
#if defined(BOOST_WINDOWS)
@@ -232,46 +233,19 @@ BOOST_LOG_API thread::id const& get_id()
} // namespace this_thread
BOOST_LOG_ANONYMOUS_NAMESPACE {
template< typename CharT >
inline void format_thread_id_impl(CharT* buf, std::size_t size, thread::id tid, const CharT (&char_table)[18])
{
// Input buffer is assumed to be always larger than 2 chars
*buf++ = char_table[0]; // '0'
*buf++ = char_table[16]; // '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 - 4u; i < n; ++i, shift -= 4u)
{
buf[i] = char_table[(id >> shift) & 15u];
}
buf[i] = '\0';
}
} // namespace
// Used in default_sink.cpp
void format_thread_id(char* buf, std::size_t size, thread::id tid)
{
format_thread_id_impl(buf, size, tid, "0123456789abcdefx");
format_id< tid_size >(buf, size, tid.native_id(), false);
}
template< typename CharT, typename TraitsT >
std::basic_ostream< CharT, TraitsT >&
operator<< (std::basic_ostream< CharT, TraitsT >& strm, thread::id const& tid)
std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, thread::id const& tid)
{
if (strm.good())
{
static const CharT lowercase[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'x', '\0' };
static const CharT uppercase[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'X', '\0' };
CharT buf[tid_size * 2 + 3]; // 2 chars per byte + 3 chars for the leading 0x and terminating zero
format_thread_id_impl(buf, sizeof(buf) / sizeof(*buf), tid, (strm.flags() & std::ios_base::uppercase) ? uppercase : lowercase);
format_id< tid_size >(buf, sizeof(buf) / sizeof(*buf), tid.native_id(), (strm.flags() & std::ios_base::uppercase) != 0);
strm << buf;
}