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
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
6namespace {
7
8//
9// In-Module Globals
10//
11thread_local size_t g_EntryLevel = 0;
12
13} // namespace
14
15namespace bux {
16
17//
18// Globals
19//
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
47std::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//
66C_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
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
92void C_EntryLog::deeper()
93{
94 ++g_EntryLevel;
95}
96
97int C_EntryLog::getId()
98{
99 static std::atomic<int> id = ATOMIC_VAR_INIT(1);
100 return id++;
101}
102
103} // namespace bux
#define RUNTIME_ERROR(fmtStr,...)
Wrap FILE(DATE)#__LINE__ FUNCTION: msg into std::runtime_error.
Definition XException.h:32
C_EntryLog(std::string_view scopeName)
Definition Logger.cpp:66
auto stream() const
Definition SyncLog.h:218
auto timezone() const
Definition SyncLog.h:219
</// Thread safety is expected
Definition SyncLog.h:15
I_SyncLog & logger()
THE common namespace of bux library.
Definition AtomiX.cpp:3
E_LogLevel
Definition LogLevel.h:9
@ LL_WARNING
Situation that should be warned but should not have sabotaged anything already.
Definition LogLevel.h:12
@ LL_ERROR
Error not serious enough that the program can continue to run.
Definition LogLevel.h:11
@ LL_INFO
Information worth mentioning about the current status, be it normal or abnormal.
Definition LogLevel.h:13
@ LL_VERBOSE
More detailed or advanced information probably considered too much by some.
Definition LogLevel.h:14
@ LL_FATAL
Error serious enough that the program should shut down immediately after reporting this.
Definition LogLevel.h:10
I_SyncLog & logger()
Definition Logger.cpp:20
std::ostream & logTrace(std::ostream &out, const std::chrono::time_zone *tz)
Definition LogStream.cpp:62
std::ostream & stamp(const C_UseLog &u, E_LogLevel level)
Definition Logger.cpp:47