Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
debug_log.hpp
Go to the documentation of this file.
1#pragma once
2
15#ifdef BBERG_DEBUG_LOG
16
17#include <iostream>
18#include <sstream>
19#include <string>
20namespace barretenberg {
29void _debug_log_impl(const std::string& log_str);
30
31template <class T>
32concept Printable = requires(T a) { std::cout << a; };
33template <class T>
34concept ContainerLike = requires(T a) {
35 a.begin();
36 a.size();
37};
38
39template <typename T> const char* _summarize(const T* /*unused*/)
40{
41 return "(?)";
42}
43
44template <Printable T> const T& _summarize(const T* x)
45{
46 return *x;
47}
48
49// fallback for non-printable containers
50template <ContainerLike T>
51std::string _summarize(const T* x)
52 requires(!Printable<T>)
53{
54 std::ostringstream ss;
55 if (x->size() == 0) {
56 ss << "[]";
57 } else {
58 ss << "[" << _summarize(&*x->begin()) << "...]";
59 }
60 return ss.str();
61}
62
68template <typename... FuncArgs> void _debug_log(const char* func_name, const char* arg_string, const FuncArgs&... args)
69{
70 // shut off recursive DEBUG_LOG
71 static size_t debug_log_calls = 0;
72 if (debug_log_calls > 0) {
73 return;
74 }
75 debug_log_calls++;
76 std::ostringstream ss;
77 ss << func_name << " " << arg_string << " = ";
78 // Using fold expression to append args to stringstream
79 ((ss << _summarize(&args) << " "), ...);
80 std::string log_str = ss.str();
81 // Want to be able to catch offending statements in a debugger, this throws if we match an env variable pattern
82 _debug_log_impl(log_str);
83 debug_log_calls--;
84}
85} // namespace barretenberg
92#define DEBUG_LOG(...) \
93 if (!std::is_constant_evaluated()) { /*we are not in a compile-time constexpr*/ \
94 barretenberg::_debug_log(__FUNCTION__, #__VA_ARGS__, __VA_ARGS__); \
95 }
96
97#define DEBUG_LOG_ALL(container) \
98 for (auto& x : (container)) { \
99 barretenberg::_debug_log(__FUNCTION__, #container, x); \
100 }
101
102#else // If BBERG_DEBUG_LOG is not defined
103
104// Define empty macros and functions
105#define DEBUG_LOG(...)
106#define DEBUG_LOG_ALL(container)
107
108#endif // BBERG_DEBUG_LOG
FF a
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13