Skip to content

Commit

Permalink
Removing global glz::trace (#1544)
Browse files Browse the repository at this point in the history
* Removing global glz::trace

* Remove unnecessary write_file_trace

* Remove disabled option

* Revert "Remove disabled option"

This reverts commit 9ec37aa.

* disabled handling in scopers
  • Loading branch information
stephenberry authored Jan 3, 2025
1 parent 921f45f commit b14b9e7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 90 deletions.
126 changes: 45 additions & 81 deletions include/glaze/trace/trace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,51 @@ namespace glz
std::ignore = glz::write_json(std::forward<Args>(args)..., event->args.str);
}
}

// Automatically adds the end event when it leave scope
struct duration_scoper final
{
trace& tr;

duration_scoper(trace& tr, const std::string_view name) noexcept : tr(tr), name(name) {
if (not tr.disabled) {
tr.begin(name);
}
}
~duration_scoper() noexcept {
if (not tr.disabled) {
tr.end(name);
}
}

const std::string_view name{};
};

struct async_scoper final
{
trace& tr;

async_scoper(trace& tr, const std::string_view name) noexcept : tr(tr), name(name) {
if (not tr.disabled) {
tr.begin(name);
}
}
~async_scoper() noexcept {
if (not tr.disabled) {
tr.end(name);
}
}

const std::string_view name{};
};

duration_scoper scope(const std::string_view name) {
return {*this, name};
}

async_scoper async_scope(const std::string_view name) {
return {*this, name};
}
};

template <>
Expand All @@ -165,85 +210,4 @@ namespace glz
using T = trace;
static constexpr auto value = object(&T::traceEvents, &T::displayTimeUnit);
};

// Global approach to user a trace
// instead of calling: my_trace.begin("my event");
// you can call: glz::trace_begin("my event");
template <size_t I>
inline trace& global_trace() noexcept
{
static trace trc{};
return trc;
}

template <size_t I>
inline void enable_trace() noexcept
{
global_trace<0>().disabled = false;
}

template <size_t I>
inline void disable_trace() noexcept
{
global_trace<0>().disabled = true;
}

template <opts Opts = opts{}>
[[nodiscard]] error_ctx write_file_trace(const std::string& file_name, auto&& buffer) noexcept
{
const auto ec = write<set_json<Opts>()>(global_trace<0>(), buffer);
if (bool(ec)) [[unlikely]] {
return ec;
}
return {buffer_to_file(buffer, file_name)};
}

template <class... Args>
requires(sizeof...(Args) <= 1)
constexpr void trace_begin(const std::string_view name, Args&&... args) noexcept
{
auto& trc = global_trace<0>();
trc.begin(name, std::forward<Args>(args)...);
}

template <class... Args>
requires(sizeof...(Args) <= 1)
constexpr void trace_end(const std::string_view name, Args&&... args) noexcept
{
auto& trc = global_trace<0>();
trc.end(name, std::forward<Args>(args)...);
}

template <class... Args>
requires(sizeof...(Args) <= 1)
constexpr void trace_async_begin(const std::string_view name, Args&&... args) noexcept
{
auto& trc = global_trace<0>();
trc.async_begin(name, std::forward<Args>(args)...);
}

template <class... Args>
requires(sizeof...(Args) <= 1)
constexpr void trace_async_end(const std::string_view name, Args&&... args) noexcept
{
auto& trc = global_trace<0>();
trc.async_end(name, std::forward<Args>(args)...);
}

// Automatically adds the end event when it leave scope
struct duration_trace final
{
duration_trace(const std::string_view name) noexcept : name(name) { trace_begin(name); }
~duration_trace() noexcept { trace_end(name); }

const std::string_view name{};
};

struct async_trace final
{
async_trace(const std::string_view name) noexcept : name(name) { trace_async_begin(name); }
~async_trace() noexcept { trace_async_end(name); }

const std::string_view name{};
};
}
20 changes: 11 additions & 9 deletions tests/beve_test/beve_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include "glaze/trace/trace.hpp"
#include "ut/ut.hpp"

inline glz::trace trace{};

struct my_struct
{
int i = 287;
Expand Down Expand Up @@ -413,7 +415,7 @@ void bench()
{
using namespace ut;
"bench"_test = [] {
glz::trace_begin("bench");
trace.begin("bench");
std::cout << "\nPerformance regresion test: \n";
#ifdef NDEBUG
size_t repeat = 100000;
Expand Down Expand Up @@ -446,7 +448,7 @@ void bench()
mbytes_per_sec = repeat * buffer.size() / (duration * 1048576);
std::cout << "from_beve: " << duration << " s, " << mbytes_per_sec << " MB/s"
<< "\n";
glz::trace_end("bench");
trace.end("bench");
};
}

Expand Down Expand Up @@ -1166,7 +1168,7 @@ suite signal_tests = [] {

suite vector_tests = [] {
"std::vector<uint8_t>"_test = [] {
glz::duration_trace trace{"test std::vector<uint8_t>"};
auto scoped = trace.scope("test std::vector<uint8_t>");
std::string s;
static constexpr auto n = 10000;
std::vector<uint8_t> v(n);
Expand All @@ -1189,7 +1191,7 @@ suite vector_tests = [] {
};

"std::vector<uint16_t>"_test = [] {
glz::duration_trace trace{"test std::vector<uint16_t>"};
auto scoped = trace.scope("test std::vector<uint16_t>");
std::string s;
static constexpr auto n = 10000;
std::vector<uint16_t> v(n);
Expand All @@ -1213,7 +1215,7 @@ suite vector_tests = [] {
};

"std::vector<float>"_test = [] {
glz::async_trace trace{"test std::vector<float>"};
auto scoped = trace.async_scope("test std::vector<float>");
std::string s;
static constexpr auto n = 10000;
std::vector<float> v(n);
Expand All @@ -1237,7 +1239,7 @@ suite vector_tests = [] {
};

"std::vector<double>"_test = [] {
glz::async_trace trace{"test std::vector<double>"};
auto scoped = trace.async_scope("test std::vector<double>");
std::string s;
static constexpr auto n = 10000;
std::vector<double> v(n);
Expand Down Expand Up @@ -2402,15 +2404,15 @@ suite pair_ranges_tests = [] {

int main()
{
glz::trace_begin("binary_test");
trace.begin("binary_test");
write_tests();
bench();
test_partial();
file_include_test();
container_types();

glz::trace_end("binary_test");
const auto ec = glz::write_file_trace("binary_test.trace.json", std::string{});
trace.end("binary_test");
const auto ec = glz::write_file_json(trace, "binary_test.trace.json", std::string{});
if (ec) {
std::cerr << "trace output failed\n";
}
Expand Down

0 comments on commit b14b9e7

Please sign in to comment.