forked from hanzelpeter/dispmanx_vnc
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Logger.hpp
66 lines (56 loc) · 1.51 KB
/
Logger.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
#pragma once
#include <iostream>
#include <sstream>
#include <mutex>
#include <string>
#include <chrono>
#include <ctime>
#include <iomanip>
class Logger
{
public:
static Logger Get(const std::string& module = "")
{
return {module};
}
static void SetDefaultModule(const std::string& module)
{
m_defaultModule = module;
}
~Logger()
{
std::lock_guard<std::mutex> lock{m_mutex};
auto now = std::chrono::system_clock::now();
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
auto tmb = std::chrono::system_clock::to_time_t(now);
auto bt = *std::localtime(&tmb);
#if defined(__GNUC__) && __GNUC__ < 5
{
char timeBuff[100];
std::strftime(timeBuff, sizeof(timeBuff), "%F %X", &bt);
std::cerr << timeBuff << "." << std::setfill('0') << std::setw(3) << ms.count() << std::setw(0)
<< " [" << (m_module.empty() ? m_defaultModule : m_module) << "] "
<< m_stream.str() << std::endl;
}
#else
std::cerr << std::put_time(&bt, "%F %X") << "." << std::setfill('0') << std::setw(3) << ms.count() << std::setw(0)
<< " [" << (m_module.empty() ? m_defaultModule : m_module) << "] "
<< m_stream.str() << std::endl;
#endif
}
template <typename T>
const Logger& operator <<(T const& value) const
{
m_stream << value;
return *this;
}
private:
Logger() = default;
Logger(const std::string& module)
: m_module{module}
{}
std::string m_module;
mutable std::stringstream m_stream;
static std::string m_defaultModule;
static std::mutex m_mutex;
};