From 7d4c0917b33b12b4051ba04949e4b91ffd34dbb2 Mon Sep 17 00:00:00 2001 From: Agustin K-ballo Berge Date: Sun, 5 Jan 2020 09:06:41 -0300 Subject: [PATCH] Lift stringstream into log message --- libs/logging/CMakeLists.txt | 1 - libs/logging/include/hpx/logging.hpp | 12 +-- .../include/hpx/logging/detail/logger.hpp | 24 ++---- .../hpx/logging/format/named_write.hpp | 10 +-- libs/logging/include/hpx/logging/message.hpp | 61 +++++--------- .../logging/src/format/formatter/defaults.cpp | 6 +- .../format/formatter/high_precision_time.cpp | 11 ++- .../src/format/formatter/thread_id.cpp | 5 +- libs/logging/src/format/named_write.cpp | 2 - libs/logging/src/message.cpp | 80 ------------------- src/util/init_logging.cpp | 41 ++++------ 11 files changed, 63 insertions(+), 190 deletions(-) delete mode 100644 libs/logging/src/message.cpp diff --git a/libs/logging/CMakeLists.txt b/libs/logging/CMakeLists.txt index 0e62f6d5c5e3..4616f0787512 100644 --- a/libs/logging/CMakeLists.txt +++ b/libs/logging/CMakeLists.txt @@ -46,7 +46,6 @@ set(logging_sources level.cpp logging.cpp manipulator.cpp - message.cpp format/named_write.cpp format/destination/defaults.cpp format/destination/file.cpp diff --git a/libs/logging/include/hpx/logging.hpp b/libs/logging/include/hpx/logging.hpp index e6d884f6cfa5..9ccaed8e09b0 100644 --- a/libs/logging/include/hpx/logging.hpp +++ b/libs/logging/include/hpx/logging.hpp @@ -179,6 +179,12 @@ namespace hpx { namespace util { { constexpr dummy_log_impl() noexcept {} + template + dummy_log_impl const& operator<<(T&&) const noexcept + { + return *this; + } + template dummy_log_impl const& format(char const*, Args const&...) const noexcept @@ -187,12 +193,6 @@ namespace hpx { namespace util { } }; constexpr dummy_log_impl dummy_log; - - template - dummy_log_impl const& operator<<(dummy_log_impl const& l, T&&) - { - return l; - } } // namespace detail // clang-format off diff --git a/libs/logging/include/hpx/logging/detail/logger.hpp b/libs/logging/include/hpx/logging/detail/logger.hpp index 0ff400a0ce37..6e6b81441d21 100644 --- a/libs/logging/include/hpx/logging/detail/logger.hpp +++ b/libs/logging/include/hpx/logging/detail/logger.hpp @@ -22,8 +22,6 @@ #include #include -#include -#include #include #include @@ -84,31 +82,23 @@ namespace hpx { namespace util { namespace logging { { HPX_NON_COPYABLE(logger); - struct gather_holder : std::ostringstream + struct gather_holder : message { //-V690 HPX_NON_COPYABLE(gather_holder); - gather_holder(logger const& p_this) + gather_holder(logger& p_this) : m_this(p_this) { } ~gather_holder() { - std::string msg = std::move(*this).str(); - if (!msg.empty()) - m_this.do_write(std::move(msg)); - } - - template - gather_holder& format(char const* format_str, Args const&... args) - { - util::format_to(*this, format_str, args...); - return *this; + if (!empty()) + m_this.write(std::move(*this)); } private: - logger const& m_this; + logger& m_this; }; public: @@ -131,7 +121,7 @@ namespace hpx { namespace util { namespace logging { /** reads all data about a log message (gathers all the data about it) */ - gather_holder gather() const + gather_holder gather() { return {*this}; } @@ -180,7 +170,7 @@ namespace hpx { namespace util { namespace logging { void HPX_EXPORT turn_cache_off(); // called after all data has been gathered - void do_write(message msg) const + void write(message msg) { if (m_is_caching_off) m_writer(msg); diff --git a/libs/logging/include/hpx/logging/format/named_write.hpp b/libs/logging/include/hpx/logging/format/named_write.hpp index c09132c48ab5..fa23a4f409c5 100644 --- a/libs/logging/include/hpx/logging/format/named_write.hpp +++ b/libs/logging/include/hpx/logging/format/named_write.hpp @@ -120,9 +120,9 @@ You could have an output like this: { for (auto const& step : write_steps) { + msg << step.prefix; if (step.fmt) (*step.fmt)(msg); - msg.prepend_string(step.prefix); } } @@ -394,11 +394,11 @@ This will just configure "file" twice, ending up with writing only to "two.txt" void operator()(message const& msg) const { - message formatted_msg("\n"); - formatted_msg.prepend_string(msg.full_string()); - m_format(formatted_msg); + message formatted_message; + m_format(formatted_message); + formatted_message << msg.full_string() << '\n'; - m_destination(formatted_msg); + m_destination(formatted_message); } /** @brief Replaces a formatter from the named formatter. diff --git a/libs/logging/include/hpx/logging/message.hpp b/libs/logging/include/hpx/logging/message.hpp index 9d73564af494..d4b075171f29 100644 --- a/libs/logging/include/hpx/logging/message.hpp +++ b/libs/logging/include/hpx/logging/message.hpp @@ -18,10 +18,12 @@ #define HPX_LOGGING_MESSAGE_HPP #include +#include #include #include +#include #include #include @@ -43,56 +45,40 @@ namespace hpx { namespace util { namespace logging { class message { public: - /** - @param reserve - how many chars to have space to prepend by default - */ - message(std::size_t reserve_) - : m_reserve(reserve_) - , m_full_msg_computed(false) - { - } - /** @param msg - the message that is originally cached - @param reserve - how many chars to have space to prepend by default */ - message(std::string const& msg, std::size_t reserve_ = 10) - : m_reserve(reserve_) + message(std::string const& msg = "") + : m_str(msg) , m_full_msg_computed(false) { - set_string(msg); } - message(message&& other) - : m_reserve(other.m_reserve) - , m_str(std::move(other.m_str)) + message(message&& other) noexcept + : m_str(std::move(other.m_str)) , m_full_msg_computed(other.m_full_msg_computed) , m_full_msg(std::move(other.m_full_msg)) { - other.m_reserve = 10; other.m_full_msg_computed = false; } - message() - : m_reserve(10) - , m_full_msg_computed(false) - { - } - - void HPX_EXPORT set_string(std::string const& str); - - std::size_t reserve() const + template + message& operator<<(T&& v) { - return m_reserve; + m_str << std::forward(v); + m_full_msg_computed = false; + return *this; } - void reserve(std::size_t new_size) + template + message& format( + boost::string_ref format_str, Args const&... args) noexcept { - resize_string(new_size); + util::format_to(m_str, format_str, args...); + m_full_msg_computed = false; + return *this; } - void HPX_EXPORT prepend_string(boost::string_ref str); - /** returns the full string */ @@ -101,23 +87,18 @@ namespace hpx { namespace util { namespace logging { if (!m_full_msg_computed) { m_full_msg_computed = true; - m_full_msg = m_str.substr(m_reserve, m_str.size() - m_reserve); + m_full_msg = m_str.str(); } return m_full_msg; } - private: - void HPX_EXPORT resize_string(std::size_t reserve_); - - // if true, string was already set - bool is_string_set() const + bool empty() const { - return !m_str.empty(); + return full_string().empty(); } private: - std::size_t m_reserve; - std::string m_str; + std::ostringstream m_str; // caching mutable bool m_full_msg_computed; diff --git a/libs/logging/src/format/formatter/defaults.cpp b/libs/logging/src/format/formatter/defaults.cpp index df4a006ddba8..eb6c3b015b62 100644 --- a/libs/logging/src/format/formatter/defaults.cpp +++ b/libs/logging/src/format/formatter/defaults.cpp @@ -17,7 +17,6 @@ #include #include -#include #include #include @@ -35,10 +34,9 @@ namespace hpx { namespace util { namespace logging { namespace formatter { { } - void operator()(message& str) override + void operator()(message& msg) override { - std::string idx = hpx::util::format("{:016x}", ++value); - str.prepend_string(idx); + msg.format("{:016x}", ++value); } private: diff --git a/libs/logging/src/format/formatter/high_precision_time.cpp b/libs/logging/src/format/formatter/high_precision_time.cpp index 113df3381a33..6939f4ed93f4 100644 --- a/libs/logging/src/format/formatter/high_precision_time.cpp +++ b/libs/logging/src/format/formatter/high_precision_time.cpp @@ -80,12 +80,11 @@ namespace hpx { namespace util { namespace logging { namespace formatter { std::chrono::duration_cast( val.time_since_epoch()); - std::string time_str = hpx::util::format(m_format, local_tm.tm_mday, - local_tm.tm_mon + 1, local_tm.tm_year + 1900, - local_tm.tm_year % 100, local_tm.tm_hour, local_tm.tm_min, - local_tm.tm_sec, millisecs.count() % 1000, - microsecs.count() % 1000, nanosecs.count() % 1000); - msg.prepend_string(time_str); + msg.format(m_format, local_tm.tm_mday, local_tm.tm_mon + 1, + local_tm.tm_year + 1900, local_tm.tm_year % 100, + local_tm.tm_hour, local_tm.tm_min, local_tm.tm_sec, + millisecs.count() % 1000, microsecs.count() % 1000, + nanosecs.count() % 1000); } /** @brief configure through script diff --git a/libs/logging/src/format/formatter/thread_id.cpp b/libs/logging/src/format/formatter/thread_id.cpp index 41e2b618b0b1..f3b8a1d72fa8 100644 --- a/libs/logging/src/format/formatter/thread_id.cpp +++ b/libs/logging/src/format/formatter/thread_id.cpp @@ -17,7 +17,6 @@ #include #include -#include #include #include @@ -37,15 +36,13 @@ namespace hpx { namespace util { namespace logging { namespace formatter { { void operator()(message& msg) override { - std::string out = hpx::util::format("{}", + msg.format("{}", #if defined(HPX_WINDOWS) ::GetCurrentThreadId() #else pthread_self() #endif ); - - msg.prepend_string(out); } }; diff --git a/libs/logging/src/format/named_write.cpp b/libs/logging/src/format/named_write.cpp index d7d22802bfb8..668bed105710 100644 --- a/libs/logging/src/format/named_write.cpp +++ b/libs/logging/src/format/named_write.cpp @@ -21,7 +21,6 @@ #include #include -#include #include #include #include @@ -92,7 +91,6 @@ namespace hpx { namespace util { namespace logging { namespace detail { remaining.clear(); } } - std::reverse(write_steps.begin(), write_steps.end()); } void named_destinations::compute_write_steps() diff --git a/libs/logging/src/message.cpp b/libs/logging/src/message.cpp deleted file mode 100644 index b29106812c62..000000000000 --- a/libs/logging/src/message.cpp +++ /dev/null @@ -1,80 +0,0 @@ -// message.cpp - -// Boost Logging library -// -// Author: John Torjo, www.torjo.com -// -// Copyright (C) 2007 John Torjo (see www.torjo.com for email) -// -// SPDX-License-Identifier: BSL-1.0 -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -// -// See http://www.boost.org for updates, documentation, and revision history. -// See http://www.torjo.com/log2/ for more details - -#include - -#include -#include - -#include -#include -#include -#include - -namespace hpx { namespace util { namespace logging { - - void message::set_string(std::string const& str) - { - m_str.resize(str.size() + m_reserve); - std::copy(str.begin(), str.end(), - m_str.begin() + static_cast(m_reserve)); - m_full_msg_computed = false; - } - - void message::prepend_string(boost::string_ref str) - { - std::size_t len = str.length(); - if (m_reserve < len) - { - std::size_t new_reserve = len + 10; - resize_string(new_reserve); - } - - HPX_ASSERT(m_reserve >= len); - - std::ptrdiff_t start_idx = static_cast(m_reserve - len); - m_reserve -= len; - - std::copy(str.data(), str.data() + len, m_str.begin() + start_idx); - m_full_msg_computed = false; - } - - void message::resize_string(std::size_t reserve_) - { - if (is_string_set()) - { - std::size_t to_add = reserve_ - m_reserve; - std::size_t new_size = m_str.size() + to_add; - - // I'm creating a new string instead of resizing the existing one - // this is because the new string could be of lower size - std::string new_str(reserve_, 0); - std::size_t used_size = m_str.size() - m_reserve; - new_str.insert(new_str.end(), - m_str.begin() + static_cast(m_reserve), - m_str.begin() + - static_cast(m_reserve + used_size)); - - HPX_ASSERT(new_size == reserve_ + used_size); - - new_str.resize(new_size, 0); - std::swap(new_str, m_str); - } - - m_reserve = reserve_; - } - -}}} // namespace hpx::util::logging diff --git a/src/util/init_logging.cpp b/src/util/init_logging.cpp index 1e0c34a11a5d..f1390a3129f0 100644 --- a/src/util/init_logging.cpp +++ b/src/util/init_logging.cpp @@ -60,12 +60,11 @@ namespace hpx { namespace util { if (std::size_t(-1) != thread_num) { - std::string out = format("{:016x}", thread_num); - msg.prepend_string(out); + msg.format("{:016x}", thread_num); } else { - msg.prepend_string(std::string(16, '-')); + msg << std::string(16, '-'); } } }; @@ -82,13 +81,12 @@ namespace hpx { namespace util { if (naming::invalid_locality_id != locality_id) { - std::string out = format("{:08x}", locality_id); - msg.prepend_string(out); + msg.format("{:08x}", locality_id); } else { // called from outside a HPX thread - msg.prepend_string(std::string(8, '-')); + msg << std::string(8, '-'); } } }; @@ -108,14 +106,13 @@ namespace hpx { namespace util { { std::ptrdiff_t value = reinterpret_cast(id.get()); - std::string out = format("{:016x}", value); - msg.prepend_string(out); + msg.format("{:016x}", value); return; } } // called from outside a HPX thread or invalid thread id - msg.prepend_string(std::string(16, '-')); + msg << std::string(16, '-'); } }; @@ -132,15 +129,13 @@ namespace hpx { namespace util { std::size_t phase = self->get_thread_phase(); if (0 != phase) { - std::string out = - format("{:04x}", self->get_thread_phase()); - msg.prepend_string(out); + msg.format("{:04x}", self->get_thread_phase()); return; } } // called from outside a HPX thread or no phase given - msg.prepend_string(std::string(4, '-')); + msg << std::string(4, '-'); } }; @@ -155,13 +150,12 @@ namespace hpx { namespace util { if (naming::invalid_locality_id != parent_locality_id) { // called from inside a HPX thread - std::string out = format("{:08x}", parent_locality_id); - msg.prepend_string(out); + msg.format("{:08x}", parent_locality_id); } else { // called from outside a HPX thread - msg.prepend_string(std::string(8, '-')); + msg << std::string(8, '-'); } } }; @@ -178,13 +172,12 @@ namespace hpx { namespace util { // called from inside a HPX thread std::ptrdiff_t value = reinterpret_cast(parent_id.get()); - std::string out = format("{:016x}", value); - msg.prepend_string(out); + msg.format("{:016x}", value); } else { // called from outside a HPX thread - msg.prepend_string(std::string(16, '-')); + msg << std::string(16, '-'); } } }; @@ -199,13 +192,12 @@ namespace hpx { namespace util { if (0 != parent_phase) { // called from inside a HPX thread - std::string out = format("{:04x}", parent_phase); - msg.prepend_string(out); + msg.format("{:04x}", parent_phase); } else { // called from outside a HPX thread - msg.prepend_string(std::string(4, '-')); + msg << std::string(4, '-'); } } }; @@ -220,13 +212,12 @@ namespace hpx { namespace util { if (0 != component_id) { // called from inside a HPX thread - std::string out = format("{:016x}", component_id); - msg.prepend_string(out); + msg.format("{:016x}", component_id); } else { // called from outside a HPX thread - msg.prepend_string(std::string(16, '-')); + msg << std::string(16, '-'); } } };