-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog.h
48 lines (40 loc) · 1.06 KB
/
log.h
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
#ifndef LOG_H
#define LOG_H
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
inline void unused() {}
template <typename T, typename... Args>
inline void unused(const T& t, Args... args) { (void)t; unused(args...); }
inline void print(std::ostream& os, const char* fmt) {
assert(!strchr(fmt, '%') && "Not enough arguments to print");
os << fmt << std::endl;
}
template <typename T, typename... Args>
void print(std::ostream& os, const char* fmt, const T& t, Args... args) {
auto ptr = strchr(fmt, '%');
while (ptr && ptr[1] == '%') ptr = strchr(ptr + 2, '%');
assert(ptr && "Too many arguments to print");
os.write(fmt, ptr - fmt);
os << t;
print(os, ptr + 1, args...);
}
template <typename... Args>
[[noreturn]] void error(Args... args) {
print(std::cerr, args...);
std::abort();
}
template <typename... Args>
void info(Args... args) {
print(std::cout, args...);
}
template <typename... Args>
void debug(Args... args) {
#ifndef NDEBUG
print(std::cout, args...);
#else
unused(args...);
#endif
}
#endif