barretenberg
Loading...
Searching...
No Matches
log.hpp
1#pragma once
2#include "barretenberg/env/logstr.hpp"
3#include "barretenberg/stdlib/primitives/circuit_builders/circuit_builders_fwd.hpp"
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
13template <typename... Args> std::string format(Args... args)
14{
15 std::ostringstream os;
16 ((os << args), ...);
17 return os.str();
18}
19
20template <typename T> void benchmark_format_chain(std::ostream& os, T const& first)
21{
22 // We will be saving these values to a CSV file, so we can't tolerate commas
23 std::stringstream current_argument;
24 current_argument << first;
25 std::string current_argument_string = current_argument.str();
26 std::replace(current_argument_string.begin(), current_argument_string.end(), ',', ';');
27 os << current_argument_string << BENCHMARK_INFO_SUFFIX;
28}
29
30template <typename T, typename... Args>
31void benchmark_format_chain(std::ostream& os, T const& first, Args const&... args)
32{
33 // We will be saving these values to a CSV file, so we can't tolerate commas
34 std::stringstream current_argument;
35 current_argument << first;
36 std::string current_argument_string = current_argument.str();
37 std::replace(current_argument_string.begin(), current_argument_string.end(), ',', ';');
38 os << current_argument_string << BENCHMARK_INFO_SEPARATOR;
39 benchmark_format_chain(os, args...);
40}
41
42template <typename... Args> std::string benchmark_format(Args... args)
43{
44 std::ostringstream os;
45 os << BENCHMARK_INFO_PREFIX;
46 benchmark_format_chain(os, args...);
47 return os.str();
48}
49
50#if NDEBUG
51template <typename... Args> inline void debug(Args... args)
52{
53 logstr(format(args...).c_str());
54}
55#else
56template <typename... Args> inline void debug(Args... /*unused*/) {}
57#endif
58
59template <typename... Args> inline void info(Args... args)
60{
61 logstr(format(args...).c_str());
62}
63
64template <typename... Args> inline void important(Args... args)
65{
66 logstr(format("important: ", args...).c_str());
67}
68
77#ifdef CI
78template <typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5>
79inline void benchmark_info(Arg1 composer, Arg2 class_name, Arg3 operation, Arg4 metric, Arg5 value)
80{
81 logstr(benchmark_format(composer, class_name, operation, metric, value).c_str());
82}
83#else
84template <typename... Args> inline void benchmark_info(Args... /*unused*/) {}
85#endif
86
92
93 std::vector<std::string> saved_benchmarks;
94
95 public:
96 BenchmarkInfoCollator() = default;
97 BenchmarkInfoCollator(const BenchmarkInfoCollator& other) = default;
99 BenchmarkInfoCollator& operator=(const BenchmarkInfoCollator& other) = default;
100 BenchmarkInfoCollator& operator=(BenchmarkInfoCollator&& other) = default;
101
111#ifdef CI
112 template <typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5>
113 inline void benchmark_info_deferred(Arg1 composer, Arg2 class_name, Arg3 operation, Arg4 metric, Arg5 value)
114 {
115 saved_benchmarks.push_back(benchmark_format(composer, class_name, operation, metric, value).c_str());
116 }
117#else
118 explicit BenchmarkInfoCollator(std::vector<std::string> saved_benchmarks)
119 : saved_benchmarks(std::move(saved_benchmarks))
120 {}
121 template <typename... Args> inline void benchmark_info_deferred(Args... /*unused*/) {}
122#endif
124 {
125 for (auto& x : saved_benchmarks) {
126 logstr(x.c_str());
127 }
128 }
129};
A class for saving benchmarks and printing them all at once in the end of the function.
Definition: log.hpp:91
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:118