Skip to content

Commit

Permalink
Lift stringstream into log message
Browse files Browse the repository at this point in the history
  • Loading branch information
K-ballo committed Jan 14, 2020
1 parent 2f4de28 commit 7d4c091
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 190 deletions.
1 change: 0 additions & 1 deletion libs/logging/CMakeLists.txt
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions libs/logging/include/hpx/logging.hpp
Expand Up @@ -179,6 +179,12 @@ namespace hpx { namespace util {
{
constexpr dummy_log_impl() noexcept {}

template <typename T>
dummy_log_impl const& operator<<(T&&) const noexcept
{
return *this;
}

template <typename... Args>
dummy_log_impl const& format(char const*, Args const&...) const
noexcept
Expand All @@ -187,12 +193,6 @@ namespace hpx { namespace util {
}
};
constexpr dummy_log_impl dummy_log;

template <typename T>
dummy_log_impl const& operator<<(dummy_log_impl const& l, T&&)
{
return l;
}
} // namespace detail

// clang-format off
Expand Down
24 changes: 7 additions & 17 deletions libs/logging/include/hpx/logging/detail/logger.hpp
Expand Up @@ -22,8 +22,6 @@
#include <hpx/logging/format/named_write.hpp>
#include <hpx/logging/level.hpp>

#include <sstream>
#include <string>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -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 <typename... Args>
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:
Expand All @@ -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};
}
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions libs/logging/include/hpx/logging/format/named_write.hpp
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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.
Expand Down
61 changes: 21 additions & 40 deletions libs/logging/include/hpx/logging/message.hpp
Expand Up @@ -18,10 +18,12 @@
#define HPX_LOGGING_MESSAGE_HPP

#include <hpx/config.hpp>
#include <hpx/format.hpp>

#include <boost/utility/string_ref.hpp>

#include <cstddef>
#include <sstream>
#include <string>
#include <utility>

Expand All @@ -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 <typename T>
message& operator<<(T&& v)
{
return m_reserve;
m_str << std::forward<T>(v);
m_full_msg_computed = false;
return *this;
}

void reserve(std::size_t new_size)
template <typename... Args>
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
*/
Expand All @@ -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;
Expand Down
6 changes: 2 additions & 4 deletions libs/logging/src/format/formatter/defaults.cpp
Expand Up @@ -17,7 +17,6 @@
#include <hpx/logging/format/formatters.hpp>

#include <hpx/config.hpp>
#include <hpx/format.hpp>
#include <hpx/logging/message.hpp>

#include <cstdint>
Expand All @@ -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:
Expand Down
11 changes: 5 additions & 6 deletions libs/logging/src/format/formatter/high_precision_time.cpp
Expand Up @@ -80,12 +80,11 @@ namespace hpx { namespace util { namespace logging { namespace formatter {
std::chrono::duration_cast<std::chrono::milliseconds>(
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
Expand Down
5 changes: 1 addition & 4 deletions libs/logging/src/format/formatter/thread_id.cpp
Expand Up @@ -17,7 +17,6 @@
#include <hpx/logging/format/formatters.hpp>

#include <hpx/config.hpp>
#include <hpx/format.hpp>
#include <hpx/logging/message.hpp>

#include <memory>
Expand All @@ -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);
}
};

Expand Down
2 changes: 0 additions & 2 deletions libs/logging/src/format/named_write.cpp
Expand Up @@ -21,7 +21,6 @@
#include <hpx/logging/format/destinations.hpp>
#include <hpx/logging/format/formatters.hpp>

#include <algorithm>
#include <cstddef>
#include <memory>
#include <sstream>
Expand Down Expand Up @@ -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()
Expand Down
80 changes: 0 additions & 80 deletions libs/logging/src/message.cpp

This file was deleted.

0 comments on commit 7d4c091

Please sign in to comment.