|
| 1 | +#define ZEROERR_IMPLEMENTATION |
| 2 | +#include "zeroerr.hpp" |
| 3 | + |
| 4 | +using namespace zeroerr; |
| 5 | + |
| 6 | +TEST_CASE("1. basic log test") { |
| 7 | + LOG("Basic log"); |
| 8 | + WARN("Warning log"); |
| 9 | + ERR("Error log"); |
| 10 | + FATAL("Fatal log"); |
| 11 | + |
| 12 | + LOG("log with basic thype {} {} {} {}", 1, true, 1.0, "string"); |
| 13 | + |
| 14 | + std::vector<std::tuple<int, float, std::string>> data = { |
| 15 | + {1, 1.0, "string"}, {2, 2.0, "string"} |
| 16 | + }; |
| 17 | + LOG("log with complex type: {data}", data); |
| 18 | + |
| 19 | + LOG_IF(1==1, "log if condition is true"); |
| 20 | + LOG_FIRST(1==1, "log only at the first time condition is true"); |
| 21 | + WARN_EVERY_(2, "log every 2 times"); |
| 22 | + WARN_IF_EVERY_(2, 1==1, "log if condition is true every 2 times"); |
| 23 | + |
| 24 | + DLOG(WARN_IF, 1==1, "debug log for WARN_IF"); |
| 25 | +} |
| 26 | + |
| 27 | + |
| 28 | +struct Expr { |
| 29 | + virtual Expr* Clone() { return new Expr(); }; |
| 30 | + virtual ~Expr() {} |
| 31 | +}; |
| 32 | + |
| 33 | +Expr* parse_the_input(std::string input) { |
| 34 | + return new Expr(); |
| 35 | +} |
| 36 | + |
| 37 | + |
| 38 | +Expr* parseExpr(std::string input) |
| 39 | +{ |
| 40 | + static std::map<std::string, Expr*> cache; |
| 41 | + if (cache.count(input) == 0) { |
| 42 | + Expr* expr = parse_the_input(input); |
| 43 | + cache[input] = expr; |
| 44 | + return expr; |
| 45 | + } else { |
| 46 | + LOG("CacheHit: input = {input}", input); |
| 47 | + return cache[input]->Clone(); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +TEST_CASE("parsing test") { |
| 52 | + zeroerr::suspendLog(); |
| 53 | + std::string log; |
| 54 | + Expr* e1 = parseExpr("1 + 2"); |
| 55 | + log = LOG_GET(parseExpr, "CacheHit", input, std::string); |
| 56 | + CHECK(log == std::string{}); |
| 57 | + Expr* e2 = parseExpr("1 + 2"); |
| 58 | + log = zeroerr::LogStream::getDefault() |
| 59 | + .getLog<std::string>("parseExpr", "CacheHit", "input"); |
| 60 | + CHECK(log == "1 + 2"); |
| 61 | + zeroerr::resumeLog(); |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | +TEST_CASE("iterate log stream") { |
| 66 | + zeroerr::suspendLog(); |
| 67 | + |
| 68 | + auto& stream = zeroerr::LogStream::getDefault(); |
| 69 | + |
| 70 | + for (auto p = stream.begin("function log {sum}, {i}"); p != stream.end(); ++p) { |
| 71 | + std::cerr << "p.get<int>(\"sum\") = " << p.get<int>("sum") << std::endl; |
| 72 | + std::cerr << "p.get<int>(\"i\") = " << p.get<int>("i") << std::endl; |
| 73 | + CHECK(p.get<int>("sum") == 10); |
| 74 | + CHECK(p.get<int>("i") == 1); |
| 75 | + } |
| 76 | + |
| 77 | + zeroerr::resumeLog(); |
| 78 | +} |
| 79 | + |
0 commit comments