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
4#include <algorithm>
5#include <functional>
6#include <sstream>
7#include <string>
8#include <vector>
9
10#define BENCHMARK_INFO_PREFIX "##BENCHMARK_INFO_PREFIX##"
11#define BENCHMARK_INFO_SEPARATOR "#"
12#define BENCHMARK_INFO_SUFFIX "##BENCHMARK_INFO_SUFFIX##"
13
14#define BENCH_GATE_COUNT_START(builder, op_name) uint64_t __bench_before = builder.get_estimated_num_finalized_gates();
15
16#define BENCH_GATE_COUNT_END(builder, op_name) \
17 uint64_t __bench_after = builder.get_estimated_num_finalized_gates(); \
18 std::cerr << "num gates with " << op_name << " = " << __bench_after - __bench_before << std::endl; \
19 benchmark_info(Builder::NAME_STRING, "Bigfield", op_name, "Gate Count", __bench_after - __bench_before);
20
21template <typename... Args> std::string format(Args... args)
22{
23 std::ostringstream os;
24 ((os << args), ...);
25 return os.str();
26}
27
28template <typename T> void benchmark_format_chain(std::ostream& os, T const& first)
29{
30 // We will be saving these values to a CSV file, so we can't tolerate commas
31 std::stringstream current_argument;
32 current_argument << first;
33 std::string current_argument_string = current_argument.str();
34 std::replace(current_argument_string.begin(), current_argument_string.end(), ',', ';');
35 os << current_argument_string << BENCHMARK_INFO_SUFFIX;
36}
37
38template <typename T, typename... Args>
39void benchmark_format_chain(std::ostream& os, T const& first, Args const&... args)
40{
41 // We will be saving these values to a CSV file, so we can't tolerate commas
42 std::stringstream current_argument;
43 current_argument << first;
44 std::string current_argument_string = current_argument.str();
45 std::replace(current_argument_string.begin(), current_argument_string.end(), ',', ';');
46 os << current_argument_string << BENCHMARK_INFO_SEPARATOR;
47 benchmark_format_chain(os, args...);
48}
49
50template <typename... Args> std::string benchmark_format(Args... args)
51{
52 std::ostringstream os;
54 benchmark_format_chain(os, args...);
55 return os.str();
56}
57
58extern bool debug_logging;
59// In release mode (e.g., NDEBUG is defined), we don't compile debug logs.
60#ifndef NDEBUG
61#define debug(...) debug_([&]() { return format(__VA_ARGS__); })
62#else
63#define debug(...) (void)0
64#endif
65
66// We take a function so that evaluation is lazy.
67inline void debug_(std::function<std::string()> func)
68{
69 if (debug_logging) {
70 logstr(func().c_str());
71 }
72}
73
74template <typename... Args> inline void info(Args... args)
75{
76 logstr(format(args...).c_str());
77}
78
79#define vinfo(...) vinfo_([&]() { return format(__VA_ARGS__); })
80
81extern bool verbose_logging;
82inline void vinfo_(std::function<std::string()> func)
83{
84 if (verbose_logging) {
85 info(func());
86 }
87}
88
89template <typename... Args> inline void important(Args... args)
90{
91 logstr(format("important: ", args...).c_str());
92}
93
102#ifdef CI
103template <typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5>
104inline void benchmark_info(Arg1 composer, Arg2 class_name, Arg3 operation, Arg4 metric, Arg5 value)
105{
106 logstr(benchmark_format(composer, class_name, operation, metric, value).c_str());
107}
108#else
109template <typename... Args> inline void benchmark_info(Args... /*unused*/) {}
110#endif
111
117
118 std::vector<std::string> saved_benchmarks;
119
120 public:
126
136#ifdef CI
137 template <typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5>
138 inline void benchmark_info_deferred(Arg1 composer, Arg2 class_name, Arg3 operation, Arg4 metric, Arg5 value)
139 {
140 saved_benchmarks.push_back(benchmark_format(composer, class_name, operation, metric, value).c_str());
141 }
142#else
143 explicit BenchmarkInfoCollator(std::vector<std::string> saved_benchmarks)
145 {}
146 template <typename... Args> inline void benchmark_info_deferred(Args... /*unused*/) {}
147#endif
149 {
150 for (auto& x : saved_benchmarks) {
151 logstr(x.c_str());
152 }
153 }
154};
A class for saving benchmarks and printing them all at once in the end of the function.
Definition log.hpp:116
void benchmark_info_deferred(Args...)
Definition log.hpp:146
BenchmarkInfoCollator(BenchmarkInfoCollator &&other)=default
BenchmarkInfoCollator(const BenchmarkInfoCollator &other)=default
BenchmarkInfoCollator & operator=(const BenchmarkInfoCollator &other)=default
std::vector< std::string > saved_benchmarks
Definition log.hpp:118
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:143
#define BENCHMARK_INFO_SEPARATOR
Definition log.hpp:11
std::string format(Args... args)
Definition log.hpp:21
bool debug_logging
Definition log.cpp:12
#define BENCHMARK_INFO_PREFIX
Definition log.hpp:10
void debug_(std::function< std::string()> func)
Definition log.hpp:67
void benchmark_info(Args...)
Info used to store circuit statistics during CI/CD with concrete structure. Writes straight to log.
Definition log.hpp:109
#define BENCHMARK_INFO_SUFFIX
Definition log.hpp:12
void important(Args... args)
Definition log.hpp:89
std::string benchmark_format(Args... args)
Definition log.hpp:50
void benchmark_format_chain(std::ostream &os, T const &first)
Definition log.hpp:28
void info(Args... args)
Definition log.hpp:74
bool verbose_logging
Definition log.cpp:6
void vinfo_(std::function< std::string()> func)
Definition log.hpp:82
void logstr(char const *msg)
Definition logstr.cpp:66
STL namespace.