-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path2_log.cpp
More file actions
79 lines (61 loc) · 2.04 KB
/
2_log.cpp
File metadata and controls
79 lines (61 loc) · 2.04 KB
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
#define ZEROERR_IMPLEMENTATION
#include "zeroerr.hpp"
using namespace zeroerr;
TEST_CASE("1. basic log test") {
LOG("Basic log");
WARN("Warning log");
ERR("Error log");
FATAL("Fatal log");
LOG("log with basic thype {} {} {} {}", 1, true, 1.0, "string");
std::vector<std::tuple<int, float, std::string>> data = {
{1, 1.0, "string"}, {2, 2.0, "string"}
};
LOG("log with complex type: {data}", data);
LOG_IF(1==1, "log if condition is true");
LOG_FIRST(1==1, "log only at the first time condition is true");
WARN_EVERY_(2, "log every 2 times");
WARN_IF_EVERY_(2, 1==1, "log if condition is true every 2 times");
DLOG(WARN_IF, 1==1, "debug log for WARN_IF");
}
struct Expr {
virtual Expr* Clone() { return new Expr(); };
virtual ~Expr() {}
};
Expr* parse_the_input(std::string input) {
return new Expr();
}
Expr* parseExpr(std::string input)
{
static std::map<std::string, Expr*> cache;
if (cache.count(input) == 0) {
Expr* expr = parse_the_input(input);
cache[input] = expr;
return expr;
} else {
LOG("CacheHit: input = {input}", input);
return cache[input]->Clone();
}
}
TEST_CASE("parsing test") {
zeroerr::suspendLog();
std::string log;
Expr* e1 = parseExpr("1 + 2");
log = LOG_GET(parseExpr, "CacheHit", input, std::string);
CHECK(log == std::string{});
Expr* e2 = parseExpr("1 + 2");
log = zeroerr::LogStream::getDefault()
.getLog<std::string>("parseExpr", "CacheHit", "input");
CHECK(log == "1 + 2");
zeroerr::resumeLog();
}
TEST_CASE("iterate log stream") {
zeroerr::suspendLog();
auto& stream = zeroerr::LogStream::getDefault();
for (auto p = stream.begin("function log {sum}, {i}"); p != stream.end(); ++p) {
std::cerr << "p.get<int>(\"sum\") = " << p.get<int>("sum") << std::endl;
std::cerr << "p.get<int>(\"i\") = " << p.get<int>("i") << std::endl;
CHECK(p.get<int>("sum") == 10);
CHECK(p.get<int>("i") == 1);
}
zeroerr::resumeLog();
}