bux API Reference
1.9.0
Static library of whatever are seen required in general purpose but not directly supported from Modern C++. Or whatever reusable originated from my side projects.
Loading...
Searching...
No Matches
src
Logger.cpp
Go to the documentation of this file.
1
#include "
Logger.h
"
2
#include "
AtomiX.h
"
// bux::C_SpinLock
3
#include "
LogStream.h
"
// bux::logTrace()
4
#include "
XException.h
"
// RUNTIME_ERROR()
5
6
namespace
{
7
8
//
9
// In-Module Globals
10
//
11
thread_local
size_t
g_EntryLevel = 0;
12
13
}
// namespace
14
15
namespace
bux
{
16
17
//
18
// Globals
19
//
20
I_SyncLog
&
logger
()
21
{
22
auto
&ret =
user::logger
();
23
static
bool
first =
true
;
24
if
(first)
25
{
26
static
constinit
std::atomic_flag lock = ATOMIC_FLAG_INIT;
27
C_SpinLock
_(lock);
28
if
(first)
29
{
30
if
(
C_UseLog
u{ret})
31
{
32
first =
false
;
33
stamp
(u, LL_VERBOSE) <<std::boolalpha <<
34
#ifndef _WIN32
35
"********** LOGS BEGUN **********\n"
;
36
#elif defined(_DEBUG)
37
"********** LOGS BEGUN ********** (Debug)\n"
;
38
#else
39
"********** LOGS BEGUN ********** (Release)\n"
;
40
#endif
41
}
42
}
43
}
44
return
ret;
45
}
46
47
std::ostream &
stamp
(
const
C_UseLog
&u,
E_LogLevel
level)
48
{
49
constexpr
static
const
char
FEWIV[] =
"FEWIV"
;
50
static_assert
(FEWIV[
LL_FATAL
] ==
'F'
);
51
static_assert
(FEWIV[
LL_ERROR
] ==
'E'
);
52
static_assert
(FEWIV[
LL_WARNING
] ==
'W'
);
53
static_assert
(FEWIV[
LL_INFO
] ==
'I'
);
54
static_assert
(FEWIV[
LL_VERBOSE
] ==
'V'
);
55
if
(
auto
pout = u.
stream
())
56
{
57
logTrace
(*pout, u.
timezone
()) <<std::format(
"{}:{}"
, FEWIV[level], std::string(g_EntryLevel,
'|'
));
58
return
*pout;
59
}
60
RUNTIME_ERROR
(
"Null stream from C_UseLog"
);
61
}
62
63
//
64
// Implement Classes
65
//
66
C_EntryLog::C_EntryLog
(std::string_view scopeName)
67
{
68
if
(
C_UseLog
u{
logger
()})
69
{
70
m_Id = getId();
71
stamp
(u,LL_VERBOSE) <<std::format(
"@{}@{} {{\n"
, *m_Id, scopeName);
72
}
73
deeper();
74
}
75
76
C_EntryLog::~C_EntryLog
()
77
{
78
--g_EntryLevel;
79
if
(m_Id)
80
{
81
if
(
C_UseLog
u{
logger
()})
82
stamp
(u,LL_VERBOSE) <<std::format(
"@{}{}"
, *m_Id,
83
[]{
84
if
(
auto
n = std::uncaught_exceptions())
85
return
std::format(
"@}} due to {} uncaught exception{}\n"
, n, (n>1?
"s"
:
""
));
86
87
return
std::string{
"@}\n"
};
88
}());
89
}
90
}
91
92
void
C_EntryLog::deeper()
93
{
94
++g_EntryLevel;
95
}
96
97
int
C_EntryLog::getId()
98
{
99
static
std::atomic<int>
id
= ATOMIC_VAR_INIT(1);
100
return
id
++;
101
}
102
103
}
// namespace bux
AtomiX.h
LogStream.h
Logger.h
XException.h
RUNTIME_ERROR
#define RUNTIME_ERROR(fmtStr,...)
Wrap FILE(DATE)#__LINE__ FUNCTION: msg into std::runtime_error.
Definition
XException.h:32
bux::C_EntryLog::C_EntryLog
C_EntryLog(std::string_view scopeName)
Definition
Logger.cpp:66
bux::C_EntryLog::~C_EntryLog
~C_EntryLog()
Definition
Logger.cpp:76
bux::C_SpinLock
Definition
AtomiX.h:14
bux::C_UseLog
Definition
SyncLog.h:205
bux::C_UseLog::stream
auto stream() const
Definition
SyncLog.h:218
bux::C_UseLog::timezone
auto timezone() const
Definition
SyncLog.h:219
bux::I_SyncLog
</// Thread safety is expected
Definition
SyncLog.h:15
bux::user::logger
I_SyncLog & logger()
bux
THE common namespace of bux library.
Definition
AtomiX.cpp:3
bux::E_LogLevel
E_LogLevel
Definition
LogLevel.h:9
bux::LL_WARNING
@ LL_WARNING
Situation that should be warned but should not have sabotaged anything already.
Definition
LogLevel.h:12
bux::LL_ERROR
@ LL_ERROR
Error not serious enough that the program can continue to run.
Definition
LogLevel.h:11
bux::LL_INFO
@ LL_INFO
Information worth mentioning about the current status, be it normal or abnormal.
Definition
LogLevel.h:13
bux::LL_VERBOSE
@ LL_VERBOSE
More detailed or advanced information probably considered too much by some.
Definition
LogLevel.h:14
bux::LL_FATAL
@ LL_FATAL
Error serious enough that the program should shut down immediately after reporting this.
Definition
LogLevel.h:10
bux::logger
I_SyncLog & logger()
Definition
Logger.cpp:20
bux::logTrace
std::ostream & logTrace(std::ostream &out, const std::chrono::time_zone *tz)
Definition
LogStream.cpp:62
bux::stamp
std::ostream & stamp(const C_UseLog &u, E_LogLevel level)
Definition
Logger.cpp:47
Generated by
1.12.0