-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.h
51 lines (44 loc) · 1.56 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
49
50
51
#ifndef LOG_H_
#define LOG_H_
#include <algorithm>
#include <chrono>
#include <ctime>
#include <iomanip>
#include <string_view>
#define LOG_INFO(STREAM) \
ScopedLogger("INFO ", __FILE__, __LINE__).stream() << STREAM;
#define LOG_ERROR(STREAM) \
ScopedLogger("ERROR", __FILE__, __LINE__).stream() << STREAM;
#ifdef DEBUG
#define LOG_DEBUG(STREAM) \
ScopedLogger("DEBUG", __FILE__, __LINE__).stream() << STREAM;
#else
#define LOG_DEBUG(STREAM)
#endif // DEBUG
#include <cstring>
#include <iostream>
#include <sstream>
#include <thread>
struct ScopedLogger {
ScopedLogger(std::string_view level, std::string_view file, unsigned line)
: level_(level) {
ss_ << level << " - " << file << ":" << std::setw(4) << std::setfill('0')
<< line << " [" << std::this_thread::get_id() << "] ";
}
std::stringstream &stream() { return ss_; }
~ScopedLogger() {
auto now = std::chrono::system_clock::now();
auto us = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch())
.count() %
1000000;
auto time = std::chrono::system_clock::to_time_t(now);
auto gmt_time = gmtime(&time);
(level_ != "ERROR" ? std::cout : std::cerr)
<< std::put_time(gmt_time, "%Y-%m-%dT%H:%M:%S.") << std::setw(6)
<< std::setfill('0') << us << " - " << ss_.str();
}
private:
std::stringstream ss_;
const std::string_view level_;
};
#endif // LOG_H_