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 <sstream>
6#include <string>
7#include <vector>
8
9#define BENCHMARK_INFO_PREFIX "##BENCHMARK_INFO_PREFIX##"
10#define BENCHMARK_INFO_SEPARATOR "#"
11#define BENCHMARK_INFO_SUFFIX "##BENCHMARK_INFO_SUFFIX##"
12
13#define BENCH_GATE_COUNT_START(builder, op_name) uint64_t __bench_before = builder.get_estimated_num_finalized_gates();
14
15#define BENCH_GATE_COUNT_END(builder, op_name) \
16 uint64_t __bench_after = builder.get_estimated_num_finalized_gates(); \
17 std::cerr << "num gates with " << op_name << " = " << __bench_after - __bench_before << std::endl; \
18 benchmark_info(Builder::NAME_STRING, "Bigfield", op_name, "Gate Count", __bench_after - __bench_before);
19
20template <typename... Args> std::string format(Args... args)
21{
22 std::ostringstream os;
23 ((os << args), ...);
24 return os.str();
25}
26
27template <typename T> void benchmark_format_chain(std::ostream& os, T const& first)
28{
29 // We will be saving these values to a CSV file, so we can't tolerate commas
30 std::stringstream current_argument;
31 current_argument << first;
32 std::string current_argument_string = current_argument.str();
33 std::replace(current_argument_string.begin(), current_argument_string.end(), ',', ';');
34 os << current_argument_string << BENCHMARK_INFO_SUFFIX;
35}
36
37template <typename T, typename... Args>
38void benchmark_format_chain(std::ostream& os, T const& first, Args const&... args)
39{
40 // We will be saving these values to a CSV file, so we can't tolerate commas
41 std::stringstream current_argument;
42 current_argument << first;
43 std::string current_argument_string = current_argument.str();
44 std::replace(current_argument_string.begin(), current_argument_string.end(), ',', ';');
45 os << current_argument_string << BENCHMARK_INFO_SEPARATOR;
46 benchmark_format_chain(os, args...);
47}
48
49template <typename... Args> std::string benchmark_format(Args... args)
50{
51 std::ostringstream os;
53 benchmark_format_chain(os, args...);
54 return os.str();
55}
56
57extern bool debug_logging;
58#ifndef NDEBUG
59template <typename... Args> inline void debug(Args... args)
60{
61 // NDEBUG is used to turn off asserts, so we want this flag to prevent debug log spamming.
62 if (debug_logging) {
63 logstr(format(args...).c_str());
64 }
65}
66#else
67template <typename... Args> inline void debug(Args... /*unused*/) {}
68#endif
69
70template <typename... Args> inline void info(Args... args)
71{
72 logstr(format(args...).c_str());
73}
74
75extern bool verbose_logging;
76template <typename... Args> inline void vinfo(Args... args)
77{
78 if (verbose_logging) {
79 info(args...);
80 }
81}
82
83template <typename... Args> inline void important(Args... args)
84{
85 logstr(format("important: ", args...).c_str());
86}
87
96#ifdef CI
97template <typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5>
98inline void benchmark_info(Arg1 composer, Arg2 class_name, Arg3 operation, Arg4 metric, Arg5 value)
99{
100 logstr(benchmark_format(composer, class_name, operation, metric, value).c_str());
101}
102#else
103template <typename... Args> inline void benchmark_info(Args... /*unused*/) {}
104#endif
105
111
112 std::vector<std::string> saved_benchmarks;
113
114 public:
120
130#ifdef CI
131 template <typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5>
132 inline void benchmark_info_deferred(Arg1 composer, Arg2 class_name, Arg3 operation, Arg4 metric, Arg5 value)
133 {
134 saved_benchmarks.push_back(benchmark_format(composer, class_name, operation, metric, value).c_str());
135 }
136#else
137 explicit BenchmarkInfoCollator(std::vector<std::string> saved_benchmarks)
139 {}
140 template <typename... Args> inline void benchmark_info_deferred(Args... /*unused*/) {}
141#endif
143 {
144 for (auto& x : saved_benchmarks) {
145 logstr(x.c_str());
146 }
147 }
148};
A class for saving benchmarks and printing them all at once in the end of the function.
Definition log.hpp:110
void benchmark_info_deferred(Args...)
Definition log.hpp:140
BenchmarkInfoCollator(BenchmarkInfoCollator &&other)=default
BenchmarkInfoCollator(const BenchmarkInfoCollator &other)=default
BenchmarkInfoCollator & operator=(const BenchmarkInfoCollator &other)=default
std::vector< std::string > saved_benchmarks
Definition log.hpp:112
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:137
#define BENCHMARK_INFO_SEPARATOR
Definition log.hpp:10
std::string format(Args... args)
Definition log.hpp:20
bool debug_logging
Definition log.cpp:12
#define BENCHMARK_INFO_PREFIX
Definition log.hpp:9
void debug(Args... args)
Definition log.hpp:59
void benchmark_info(Args...)
Info used to store circuit statistics during CI/CD with concrete structure. Writes straight to log.
Definition log.hpp:103
void vinfo(Args... args)
Definition log.hpp:76
#define BENCHMARK_INFO_SUFFIX
Definition log.hpp:11
void important(Args... args)
Definition log.hpp:83
std::string benchmark_format(Args... args)
Definition log.hpp:49
void benchmark_format_chain(std::ostream &os, T const &first)
Definition log.hpp:27
void info(Args... args)
Definition log.hpp:70
bool verbose_logging
Definition log.cpp:6
void logstr(char const *msg)
Definition logstr.cpp:63
STL namespace.