-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidiotlog.hpp
120 lines (106 loc) · 3.17 KB
/
idiotlog.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
/* idiotlog - C++ header-only logging for idiots like me
*
* Copyright (C) 2022 SaltfishAmi <ami@saltfish.moe>
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://www.wtfpl.net/ for more details.
*
*/
#ifndef IDIOTLOG_HPP_
#define IDIOTLOG_HPP_
#pragma once
#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900)
#define IDIOTLOG_CPP11 1
#endif
// Turn this on, and yay! You disabled verbose outputting.
#ifndef IDIOTLOG_KEEP_IT_SIMPLE
#include <ctime>
#endif
#include <iostream>
namespace idiotlog {
class idiot_logger {
private:
bool enable_;
public:
idiot_logger(bool enable = true) : enable_(enable) {}
void enable() { enable_ = true; }
void disable() { enable_ = false; }
bool enabled() const { return enable_; }
public:
template <class Coutable>
idiot_logger const & operator<<(Coutable const & msg) const {
if (enable_) {
std::cout << msg;
}
return *this;
}
};
class too_complex_logger {
private:
bool enable_;
int level_;
#ifndef IDIOTLOG_KEEP_IT_SIMPLE
bool verbose_;
#endif // IDIOTLOG_KEEP_IT_SIMPLE
idiot_logger const idiot_logger_;
idiot_logger const dummy_logger_;
public:
too_complex_logger(int level = 0)
: enable_(true), level_(level), idiot_logger_(true), dummy_logger_(false) {}
too_complex_logger(bool enable, int level = 0)
: enable_(enable), level_(level), idiot_logger_(true), dummy_logger_(false) {}
void enable() { enable_ = true; }
void disable() { enable_ = false; }
bool enabled() const { return enable_; }
void set_level(int level = 0) { level_ = level; }
int get_level() const { return level_; }
#ifndef IDIOTLOG_KEEP_IT_SIMPLE
bool verbose() const { return verbose_; }
void verbose(bool verbose = true) { verbose_ = verbose; }
#endif // IDIOTLOG_KEEP_IT_SIMPLE
#ifndef IDIOTLOG_KEEP_IT_SIMPLE
private:
std::string const timestamp() const {
std::time_t const now = std::time(0);
char buf[25] = {0};
#ifdef IDIOTLOG_CPP11
std::strftime(buf, 25, "%Y-%m-%dT%H:%M:%S%z", std::localtime(&now)); // C++11
#else // IDIOTLOG_CPP11
std::strftime(buf, 20, "%Y-%m-%dT%H:%M:%S", std::localtime(&now));
#endif // IDIOTLOG_CPP11
buf[25] = 0;
return std::string(buf);
}
#endif // IDIOTLOG_KEEP_IT_SIMPLE
public:
template <class Coutable>
idiot_logger const & operator<<(Coutable const & msg) const {
if (!enable_) {
return dummy_logger_;
}
#ifndef IDIOTLOG_KEEP_IT_SIMPLE
if (verbose_) {
idiot_logger_ << timestamp() << " ";
}
#endif // IDIOTLOG_KEEP_IT_SIMPLE
return (idiot_logger_ << msg);
}
idiot_logger const & level(int level) const {
if (!enable_ || level < level_) {
return dummy_logger_;
}
#ifndef IDIOTLOG_KEEP_IT_SIMPLE
if (verbose_) {
idiot_logger_ << timestamp() << " [" << level << "] ";
}
#endif // IDIOTLOG_KEEP_IT_SIMPLE
return idiot_logger_;
}
idiot_logger const & operator()(int level) const {
return this->level(level);
}
};
} // namespace idiotlog
#endif // IDIOTLOG_HPP_