Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
log.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <functional>
5#include <sstream>
6#include <string>
7#include <vector>
8
10
11#define BENCHMARK_INFO_PREFIX "##BENCHMARK_INFO_PREFIX##"
12#define BENCHMARK_INFO_SEPARATOR "#"
13#define BENCHMARK_INFO_SUFFIX "##BENCHMARK_INFO_SUFFIX##"
14
15#define BENCH_GATE_COUNT_START(builder, op_name) \
16 uint64_t __bench_before = builder.get_num_finalized_gates_inefficient();
17
18#define BENCH_GATE_COUNT_END(builder, op_name) \
19 uint64_t __bench_after = builder.get_num_finalized_gates_inefficient(); \
20 std::cerr << "num gates with " << op_name << " = " << __bench_after - __bench_before << std::endl; \
21 benchmark_info(Builder::NAME_STRING, "Bigfield", op_name, "Gate Count", __bench_after - __bench_before);
22
23template <typename... Args> std::string format(Args... args)
24{
25 std::ostringstream os;
26 ((os << args), ...);
27 return os.str();
28}
29
30template <typename T> void benchmark_format_chain(std::ostream& os, T const& first)
31{
32 // We will be saving these values to a CSV file, so we can't tolerate commas
33 std::stringstream current_argument;
34 current_argument << first;
35 std::string current_argument_string = current_argument.str();
36 std::ranges::replace(current_argument_string, ',', ';');
37 os << current_argument_string << BENCHMARK_INFO_SUFFIX;
38}
39
40template <typename T, typename... Args>
41void benchmark_format_chain(std::ostream& os, T const& first, Args const&... args)
42{
43 // We will be saving these values to a CSV file, so we can't tolerate commas
44 std::stringstream current_argument;
45 current_argument << first;
46 std::string current_argument_string = current_argument.str();
47 std::ranges::replace(current_argument_string, ',', ';');
48 os << current_argument_string << BENCHMARK_INFO_SEPARATOR;
49 benchmark_format_chain(os, args...);
50}
51
52template <typename... Args> std::string benchmark_format(Args... args)
53{
54 std::ostringstream os;
56 benchmark_format_chain(os, args...);
57 return os.str();
58}
59
60// Log levels from TS foundation/src/log/log-levels.ts:
61// ['silent', 'fatal', 'error', 'warn', 'info', 'verbose', 'debug', 'trace']
62// Map: 0=silent, 1=fatal, 2=error, 3=warn, 4=info, 5=verbose, 6=debug, 7=trace
63enum class LogLevel : int {
64 SILENT = 0, // Works ok as 0 assuming nothing logs as SILENT.
65 FATAL = 1,
66 ERROR = 2,
67 WARN = 3,
68 INFO = 4,
69 VERBOSE = 5,
70 DEBUG = 6,
71 TRACE = 7,
72};
74
75// This allows the logging sink to be customized. Useful for Typescript use-cases.
76using LogFunction = std::function<void(LogLevel level, const std::string& msg)>;
78void set_log_function(LogFunction new_log_function);
79
80// This logs (using log_function) if the log level is enabled.
81// NOTE: Evaluation of __VA_ARGS__ is lazy since it's inside the if statement.
82#define log_(level, ...) \
83 do { \
84 if (level <= bb_log_level) { \
85 log_function(level, format(__VA_ARGS__)); \
86 } \
87 } while (0)
88
89#define log_fatal(...) log_(LogLevel::FATAL, __VA_ARGS__)
90#define log_error(...) log_(LogLevel::ERROR, __VA_ARGS__)
91#define log_warn(...) log_(LogLevel::WARN, __VA_ARGS__)
92#define important(...) log_(LogLevel::WARN, "important: ", __VA_ARGS__)
93#define info(...) log_(LogLevel::INFO, __VA_ARGS__)
94#define vinfo(...) log_(LogLevel::VERBOSE, __VA_ARGS__)
95#define log_verbose(...) log_(LogLevel::VERBOSE, __VA_ARGS__)
96
97// The following logging levels are only compiled in debug mode (i.e., NDEBUG is not defined).
98#ifndef NDEBUG
99#define debug(...) log_(LogLevel::DEBUG, __VA_ARGS__)
100#define log_trace(...) log_(LogLevel::TRACE, __VA_ARGS__)
101#else
102#define debug(...) (void)0
103#define log_trace(...) (void)0
104#endif
105
114#ifdef CI
115template <typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5>
116inline void benchmark_info(Arg1 composer, Arg2 class_name, Arg3 operation, Arg4 metric, Arg5 value)
117{
118 logstr(benchmark_format(composer, class_name, operation, metric, value).c_str());
119}
120#else
121template <typename... Args> inline void benchmark_info(Args... /*unused*/) {}
122#endif
123
129
130 std::vector<std::string> saved_benchmarks;
131
132 public:
138
148#ifdef CI
149 template <typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5>
150 inline void benchmark_info_deferred(Arg1 composer, Arg2 class_name, Arg3 operation, Arg4 metric, Arg5 value)
151 {
152 saved_benchmarks.push_back(benchmark_format(composer, class_name, operation, metric, value).c_str());
153 }
154#else
155 explicit BenchmarkInfoCollator(std::vector<std::string> saved_benchmarks)
157 {}
158 template <typename... Args> inline void benchmark_info_deferred(Args... /*unused*/) {}
159#endif
161 {
162 for (auto& x : saved_benchmarks) {
163 logstr(x.c_str());
164 }
165 }
166};
A class for saving benchmarks and printing them all at once in the end of the function.
Definition log.hpp:128
void benchmark_info_deferred(Args...)
Definition log.hpp:158
BenchmarkInfoCollator(BenchmarkInfoCollator &&other)=default
BenchmarkInfoCollator(const BenchmarkInfoCollator &other)=default
BenchmarkInfoCollator & operator=(const BenchmarkInfoCollator &other)=default
std::vector< std::string > saved_benchmarks
Definition log.hpp:130
BenchmarkInfoCollator()=default
BenchmarkInfoCollator & operator=(BenchmarkInfoCollator &&other)=default
BenchmarkInfoCollator(std::vector< std::string > saved_benchmarks)
Info used to store circuit statistics during CI/CD with concrete structure. Stores string in vector f...
Definition log.hpp:155
#define BENCHMARK_INFO_SEPARATOR
Definition log.hpp:12
std::string format(Args... args)
Definition log.hpp:23
LogFunction log_function
Definition log.cpp:19
LogLevel bb_log_level
Definition log.cpp:9
std::function< void(LogLevel level, const std::string &msg)> LogFunction
Definition log.hpp:76
#define BENCHMARK_INFO_PREFIX
Definition log.hpp:11
LogLevel
Definition log.hpp:63
void benchmark_info(Args...)
Info used to store circuit statistics during CI/CD with concrete structure. Writes straight to log.
Definition log.hpp:121
void set_log_function(LogFunction new_log_function)
Definition log.cpp:21
#define BENCHMARK_INFO_SUFFIX
Definition log.hpp:13
std::string benchmark_format(Args... args)
Definition log.hpp:52
void benchmark_format_chain(std::ostream &os, T const &first)
Definition log.hpp:30
void logstr(char const *msg)
Definition logstr.cpp:66
STL namespace.