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