Skip to content

Commit

Permalink
OBJECT STREAMED ARGS REWORK
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Jan 24, 2024
1 parent 355b2e3 commit 59ce1b4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 33 deletions.
3 changes: 2 additions & 1 deletion nano/lib/logging.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <nano/lib/logging_enums.hpp>
#include <nano/lib/object_stream.hpp>
#include <nano/lib/object_stream_ostream.hpp>
#include <nano/lib/tomlconfig.hpp>

#include <initializer_list>
Expand Down Expand Up @@ -159,7 +160,7 @@ class logger final
if constexpr (is_tracing_enabled ())
{
auto logger = get_logger (type, detail);
logger.trace ("<{}> {}", to_string (detail), nano::object_stream_formatter{ global_tracing_config, std::forward<Args> (args)... });
logger.trace ("{}", nano::object_streamed_args (global_tracing_config, nano::log::arg{ "event", detail }, std::forward<Args> (args)...));
}
}

Expand Down
32 changes: 0 additions & 32 deletions nano/lib/object_stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,38 +363,6 @@ inline void nano::root_object_stream::write_range (Container const & container)
});
}

/**
* Wraps {name,value} args and provides a `<< (std::ostream &, ...)` operator that writes the arguments to the stream in a lazy manner.
*/
template <class... Args>
struct object_stream_formatter
{
object_stream_config const & config;
std::tuple<Args...> args;

explicit object_stream_formatter (object_stream_config const & config, Args &&... args) :
config{ config },
args{ std::forward<Args> (args)... }
{
}

friend std::ostream & operator<< (std::ostream & os, object_stream_formatter<Args...> const & self)
{
nano::object_stream obs{ os, self.config };
std::apply ([&obs] (auto &&... args) {
((obs.write (args.name, args.value)), ...);
},
self.args);
return os;
}

// Needed for fmt formatting, uses the ostream operator under the hood
friend auto format_as (object_stream_formatter<Args...> const & val)
{
return fmt::streamed (val);
}
};

/*
* Writers
*/
Expand Down
38 changes: 38 additions & 0 deletions nano/lib/object_stream_ostream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,42 @@ auto format_as (Value const & value)
{
return fmt::streamed (value);
}

/**
* Wraps {name,value} args and provides `<<(std::ostream &, ...)` and fmt format operator that writes the arguments to the stream in a lazy manner.
*/
template <class... Args>
struct object_stream_args_formatter
{
nano::object_stream_config const & config;
std::tuple<Args...> args;

explicit object_stream_args_formatter (nano::object_stream_config const & config, Args &&... args) :
config{ config },
args{ std::forward<Args> (args)... }
{
}

friend std::ostream & operator<< (std::ostream & os, object_stream_args_formatter<Args...> const & self)
{
nano::object_stream obs{ os, self.config };
std::apply ([&obs] (auto &&... args) {
((obs.write (args.name, args.value)), ...);
},
self.args);
return os;
}

// Needed for fmt formatting, uses the ostream operator under the hood
friend auto format_as (object_stream_args_formatter<Args...> const & val)
{
return fmt::streamed (val);
}
};

template <class... Args>
auto object_streamed_args (nano::object_stream_config const & config, Args &&... args)
{
return object_stream_args_formatter<Args...>{ config, std::forward<Args> (args)... };
}
}

0 comments on commit 59ce1b4

Please sign in to comment.