forked from expresscpp/expresscpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole.cpp
More file actions
99 lines (77 loc) · 2.95 KB
/
console.cpp
File metadata and controls
99 lines (77 loc) · 2.95 KB
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
#include "expresscpp/console.hpp"
#include <iostream>
#ifdef EXPRESSCPP_USE_STACKTRACE
#include "boost/stacktrace.hpp"
#endif
#include "expresscpp/date.hpp"
#include "expresscpp/impl/utils.hpp"
namespace expresscpp {
std::mutex Console::mutex_;
LogLevel Console::log_level_ = LogLevel::kError;
#if defined(__cpp_lib_source_location) || defined(__cpp_lib_experimental_source_location)
void Console::PrintMessage(const std::string_view prefix, const std::string_view color, const std::string_view message,
const std::experimental::source_location &location) {
std::cout << color << "[" << Date::getTime() << "]" << prefix << getFileName(location.file_name()) << ":"
<< location.line() << "-" << location.function_name() << "()"
<< " -- " << message << kReset << std::endl;
}
void Console::Log(const std::string_view message, const std::experimental::source_location &location) {
std::scoped_lock<std::mutex> lock(mutex_);
PrintMessage(" -L- ", kReset, message, location);
}
void Console::Trace(const std::string_view message, const std::experimental::source_location &location) {
std::scoped_lock<std::mutex> lock(mutex_);
PrintMessage(" -T- ", kBoldcyan, message, location);
#ifdef EXPRESSCPP_USE_STACKTRACE
std::cerr << boost::stacktrace::stacktrace();
#endif
}
void Console::Error(const std::string_view message, const std::experimental::source_location &location) {
if (log_level_ > LogLevel::kError) {
return;
}
std::scoped_lock<std::mutex> lock(mutex_);
PrintMessage(" -E- ", kBoldred, message, location);
}
void Console::Debug(const std::string_view message, const std::experimental::source_location &location) {
if (log_level_ > LogLevel::kDebug) {
return;
}
std::scoped_lock<std::mutex> lock(mutex_);
PrintMessage(" -D- ", kBlue, message, location);
}
#else
void Console::PrintMessage(const std::string_view prefix, const std::string_view color,
const std::string_view message) {
std::cout << color << "[" << Date::getTime() << "]" << prefix << " -- " << message << kReset << std::endl;
}
void Console::Log(const std::string_view message) {
std::scoped_lock<std::mutex> lock(mutex_);
PrintMessage(" -L- ", kReset, message);
}
void Console::Trace(const std::string_view message) {
std::scoped_lock<std::mutex> lock(mutex_);
PrintMessage(" -T- ", kBoldcyan, message);
#ifdef EXPRESSCPP_USE_STACKTRACE
std::cerr << boost::stacktrace::stacktrace();
#endif
}
void Console::Error(const std::string_view message) {
if (log_level_ > LogLevel::kError) {
return;
}
std::scoped_lock<std::mutex> lock(mutex_);
PrintMessage(" -E- ", kBoldred, message);
}
void Console::Debug(const std::string_view message) {
if (log_level_ > LogLevel::kDebug) {
return;
}
std::scoped_lock<std::mutex> lock(mutex_);
PrintMessage(" -D- ", kBlue, message);
}
#endif
void Console::setLogLevel(const LogLevel &log_level) {
log_level_ = log_level;
}
} // namespace expresscpp