forked from AztecProtocol/aztec-packages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.hpp
129 lines (116 loc) · 4.2 KB
/
log.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#pragma once
#include "barretenberg/env/logstr.hpp"
#include "barretenberg/stdlib/primitives/circuit_builders/circuit_builders_fwd.hpp"
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#define BENCHMARK_INFO_PREFIX "##BENCHMARK_INFO_PREFIX##"
#define BENCHMARK_INFO_SEPARATOR "#"
#define BENCHMARK_INFO_SUFFIX "##BENCHMARK_INFO_SUFFIX##"
template <typename... Args> std::string format(Args... args)
{
std::ostringstream os;
((os << args), ...);
return os.str();
}
template <typename T> void benchmark_format_chain(std::ostream& os, T const& first)
{
// We will be saving these values to a CSV file, so we can't tolerate commas
std::stringstream current_argument;
current_argument << first;
std::string current_argument_string = current_argument.str();
std::replace(current_argument_string.begin(), current_argument_string.end(), ',', ';');
os << current_argument_string << BENCHMARK_INFO_SUFFIX;
}
template <typename T, typename... Args>
void benchmark_format_chain(std::ostream& os, T const& first, Args const&... args)
{
// We will be saving these values to a CSV file, so we can't tolerate commas
std::stringstream current_argument;
current_argument << first;
std::string current_argument_string = current_argument.str();
std::replace(current_argument_string.begin(), current_argument_string.end(), ',', ';');
os << current_argument_string << BENCHMARK_INFO_SEPARATOR;
benchmark_format_chain(os, args...);
}
template <typename... Args> std::string benchmark_format(Args... args)
{
std::ostringstream os;
os << BENCHMARK_INFO_PREFIX;
benchmark_format_chain(os, args...);
return os.str();
}
#if NDEBUG
template <typename... Args> inline void debug(Args... args)
{
logstr(format(args...).c_str());
}
#else
template <typename... Args> inline void debug(Args... /*unused*/) {}
#endif
template <typename... Args> inline void info(Args... args)
{
logstr(format(args...).c_str());
}
template <typename... Args> inline void important(Args... args)
{
logstr(format("important: ", args...).c_str());
}
/**
* @brief Info used to store circuit statistics during CI/CD with concrete structure. Writes straight to log
*
* @details Automatically appends the necessary prefix and suffix, as well as separators.
*
* @tparam Args
* @param args
*/
#ifdef CI
template <typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5>
inline void benchmark_info(Arg1 composer, Arg2 class_name, Arg3 operation, Arg4 metric, Arg5 value)
{
logstr(benchmark_format(composer, class_name, operation, metric, value).c_str());
}
#else
template <typename... Args> inline void benchmark_info(Args... /*unused*/) {}
#endif
/**
* @brief A class for saving benchmarks and printing them all at once in the end of the function.
*
*/
class BenchmarkInfoCollator {
std::vector<std::string> saved_benchmarks;
public:
BenchmarkInfoCollator() = default;
BenchmarkInfoCollator(const BenchmarkInfoCollator& other) = default;
BenchmarkInfoCollator(BenchmarkInfoCollator&& other) = default;
BenchmarkInfoCollator& operator=(const BenchmarkInfoCollator& other) = default;
BenchmarkInfoCollator& operator=(BenchmarkInfoCollator&& other) = default;
/**
* @brief Info used to store circuit statistics during CI/CD with concrete structure. Stores string in vector for now
* (used to flush all benchmarks at the end of test).
*
* @details Automatically appends the necessary prefix and suffix, as well as separators.
*
* @tparam Args
* @param args
*/
#ifdef CI
template <typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5>
inline void benchmark_info_deferred(Arg1 composer, Arg2 class_name, Arg3 operation, Arg4 metric, Arg5 value)
{
saved_benchmarks.push_back(benchmark_format(composer, class_name, operation, metric, value).c_str());
}
#else
explicit BenchmarkInfoCollator(std::vector<std::string> saved_benchmarks)
: saved_benchmarks(std::move(saved_benchmarks))
{}
template <typename... Args> inline void benchmark_info_deferred(Args... /*unused*/) {}
#endif
~BenchmarkInfoCollator()
{
for (auto& x : saved_benchmarks) {
logstr(x.c_str());
}
}
};