bux API Reference 1.11.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.h
Go to the documentation of this file.
1#pragma once
2
3#include "SyncLog.h" // bux::I_SyncLog, bux::C_UseLog
4#include "XPlatform.h" // CUR_FUNC_
5#include <format> // std::format(), std::vformat(), std::make_format_args()
6#include <optional> // std::optional<>
7#include <string_view> // std::string_view
8
9namespace bux {
10
11//
12// Types
13//
17{
18public:
19
20 // Nonvirtuals
21 explicit C_EntryLog(std::string_view scopeName);
22 template<class T_Fmt, class... T_Args> C_EntryLog(std::string_view scopeName, T_Fmt &&fmtStr, T_Args&&...args);
24
25private:
26
27 // Data
28 std::optional<int> m_Id;
29
30 // Nonvirtuals
31 static void deeper();
32 static int getId();
33};
34
35//
36// Externs
37//
39std::ostream &stamp(const C_UseLog &u, E_LogLevel level);
40
41//
42// Implement Class Member Templates
43//
44template<class T_Fmt, class... T_Args>
45C_EntryLog::C_EntryLog(std::string_view scopeName, T_Fmt &&fmtStr, T_Args&&...args)
46{
47 if (C_UseLog u{logger()})
48 {
49 m_Id = getId();
50 const auto fmtfmt = std::format("@{}@{}({}) {{{{\n", *m_Id, scopeName, fmtStr);
51 stamp(u,LL_VERBOSE) << std::vformat(fmtfmt, std::make_format_args(args...));
52 }
53 deeper();
54}
55
56namespace user {
57I_SyncLog &logger(); // provided by user of LOG(), FUNLOG(), SCOPELOG()
58} // namespace User
59
60} // namespace bux
61
62//
63// Internal Macros
64//
65#define _gluePair_(x,y) x##y
66
67#ifndef TURN_OFF_LOGGER_
68#define SCOPELOG_(line,scope) bux::C_EntryLog _gluePair_(_log_,line)(scope)
69#define SCOPELOGX_(line,scope,fmtStr, ...) bux::C_EntryLog _gluePair_(_log_,line)(scope,fmtStr, ##__VA_ARGS__)
70#define DEF_LOGGER_HEAD_ namespace bux { namespace user { I_SyncLog &logger() {
71#define DEF_LOGGER_TAIL_(x) return x; }}}
72
73//
74// End-User Macros
75//
76#define LOG(ll,fmtStr, ...) do if (bux::C_UseLog u{bux::logger(),ll}) stamp(u,ll) <<std::format(fmtStr, ##__VA_ARGS__) <<'\n'; while(false)
77#define LOG_RAW(fmtStr, ...) do if (bux::C_UseLog u{bux::logger()}) *u <<std::format(fmtStr, ##__VA_ARGS__) <<'\n'; while(false)
78
79#ifndef LOGGER_USE_LOCAL_TIME_
80#define LOGGER_USE_LOCAL_TIME_ true
95#endif
96
97#define DEF_LOGGER_OSTREAM(out, ...) DEF_LOGGER_HEAD_ \
98 static C_ReenterableOstream ro_{out, ##__VA_ARGS__}; \
99 static C_SyncLogger l_{ro_, LOGGER_USE_LOCAL_TIME_}; \
100 DEF_LOGGER_TAIL_(l_)
101
102// #include <iotream> before using either of these but never both
103#define DEF_LOGGER_COUT(...) DEF_LOGGER_OSTREAM(std::cout, ##__VA_ARGS__)
104#define DEF_LOGGER_CERR(...) DEF_LOGGER_OSTREAM(std::cerr, ##__VA_ARGS__)
105
106// #include <fstream> before using this
107#define DEF_LOGGER_FILE(path, ...) \
108 DEF_LOGGER_HEAD_ \
109 static std::ofstream out{path}; \
110 static C_ReenterableOstream ro_{out, ##__VA_ARGS__}; \
111 static C_SyncLogger l_{ro_, LOGGER_USE_LOCAL_TIME_}; \
112 DEF_LOGGER_TAIL_(l_)
113
114// #include <bux/FileLog.h> before using this
115#define DEF_LOGGER_FILES(pathfmt, ...) \
116 DEF_LOGGER_HEAD_ \
117 static C_PathFmtLogSnap snap_{LOGGER_USE_LOCAL_TIME_}; \
118 static C_ReenterableOstreamSnap ros_{snap_.configPath(pathfmt), ##__VA_ARGS__}; \
119 static C_SyncLogger l_{ros_, LOGGER_USE_LOCAL_TIME_}; \
120 DEF_LOGGER_TAIL_(l_)
121
122// #include <bux/FileLog.h> before using this
123#define DEF_FALLBACK_LOGGER_FILES(fsize_in_bytes, fallbackPaths) \
124 namespace bux { namespace user { \
125 C_PathFmtLogSnap g_snap{LOGGER_USE_LOCAL_TIME_}; \
126 C_ReenterableOstreamSnap g_ros{g_snap.configPath(fsize_in_bytes, fallbackPaths)}; \
127 I_SyncLog &logger() { \
128 static C_SyncLogger l_{g_ros, LOGGER_USE_LOCAL_TIME_}; \
129 DEF_LOGGER_TAIL_(l_)
130
131// #include <bux/ParaLog.h> before using this
132#define DEF_PARA_LOGGER \
133 namespace bux { namespace user { \
134 C_ParaLog g_paraLog(LOGGER_USE_LOCAL_TIME_); \
135 I_SyncLog &logger() { \
136 DEF_LOGGER_TAIL_(g_paraLog)
137#else
138#define SCOPELOG_(line,scope)
139#define SCOPELOGX_(line,scope,fmtStr, ...)
140#define LOG(ll,fmtStr, ...)
141#define LOG_RAW(fmtStr, ...)
142#define DEF_LOGGER_OSTREAM(out, ...)
143#define DEF_LOGGER_FILE(path, ...)
144#define DEF_LOGGER_FILES(pathfmt, ...)
145#define DEF_FALLBACK_LOGGER_FILES(fsize_in_bytes, fallbackPaths)
146#define DEF_PARA_LOGGER
147#endif // TURN_OFF_LOGGER_
148
149// #include <iotream> before using either of these but never both
150#define DEF_LOGGER_COUT(...) DEF_LOGGER_OSTREAM(std::cout, ##__VA_ARGS__)
151#define DEF_LOGGER_CERR(...) DEF_LOGGER_OSTREAM(std::cerr, ##__VA_ARGS__)
152
153#define SCOPELOG(scope) SCOPELOG_(__LINE__,scope)
154#define SCOPELOGX(scope,fmtStr, ...) SCOPELOGX_(__LINE__,scope,fmtStr, ##__VA_ARGS__)
155#define FUNLOG SCOPELOG(CUR_FUNC_)
156#define FUNLOGX(fmtStr, ...) SCOPELOGX(CUR_FUNC_,fmtStr, ##__VA_ARGS__)
157
158#define LOG1(ll, str) LOG(ll, "{}", str)
159
160#define SCOPELOGX1(scope, x) SCOPELOGX(scope, "{}", x)
161#define SCOPELOGX2(scope, x1, x2) SCOPELOGX(scope, "{}, {}", x1, x2)
162#define SCOPELOGX3(scope, x1, x2, x3) SCOPELOGX(scope, "{}, {}, {}", x1, x2, x3)
163#define SCOPELOGX4(scope, x1, x2, x3, x4) SCOPELOGX(scope, "{}, {}, {}, {}", x1, x2, x3, x4)
164#define SCOPELOGX5(scope, x1, x2, x3, x4, x5) SCOPELOGX(scope, "{}, {}, {}, {}, {}", x1, x2, x3, x4, x5)
165#define SCOPELOGX6(scope, x1, x2, x3, x4, x5, x6) SCOPELOGX(scope, "{}, {}, {}, {}, {}, {}", x1, x2, x3, x4, x5, x6)
166#define SCOPELOGX7(scope, x1, x2, x3, x4, x5, x6, x7) SCOPELOGX(scope, "{}, {}, {}, {}, {}, {}, {}", x1, x2, x3, x4, x5, x6, x7)
167#define SCOPELOGX8(scope, x1, x2, x3, x4, x5, x6, x7, x8) SCOPELOGX(scope, "{}, {}, {}, {}, {}, {}, {}, {}", x1, x2, x3, x4, x5, x6, x7, x8)
168#define SCOPELOGX9(scope, x1, x2, x3, x4, x5, x6, x7, x8, x9) SCOPELOGX(scope, "{}, {}, {}, {}, {}, {}, {}, {}, {}", x1, x2, x3, x4, x5, x6, x7, x8, x9)
169
170#define FUNLOGX1(x) FUNLOGX("{}", x)
171#define FUNLOGX2(x1, x2) FUNLOGX("{}, {}", x1, x2)
172#define FUNLOGX3(x1, x2, x3) FUNLOGX("{}, {}, {}", x1, x2, x3)
173#define FUNLOGX4(x1, x2, x3, x4) FUNLOGX("{}, {}, {}, {}", x1, x2, x3, x4)
174#define FUNLOGX5(x1, x2, x3, x4, x5) FUNLOGX("{}, {}, {}, {}, {}", x1, x2, x3, x4, x5)
175#define FUNLOGX6(x1, x2, x3, x4, x5, x6) FUNLOGX("{}, {}, {}, {}, {}, {}", x1, x2, x3, x4, x5, x6)
176#define FUNLOGX7(x1, x2, x3, x4, x5, x6, x7) FUNLOGX("{}, {}, {}, {}, {}, {}, {}", x1, x2, x3, x4, x5, x6, x7)
177#define FUNLOGX8(x1, x2, x3, x4, x5, x6, x7, x8) FUNLOGX("{}, {}, {}, {}, {}, {}, {}, {}", x1, x2, x3, x4, x5, x6, x7, x8)
178#define FUNLOGX9(x1, x2, x3, x4, x5, x6, x7, x8, x9) FUNLOGX("{}, {}, {}, {}, {}, {}, {}, {}, {}", x1, x2, x3, x4, x5, x6, x7, x8, x9)
C_EntryLog(std::string_view scopeName)
Definition Logger.cpp:66
</// Thread safety is expected
Definition SyncLog.h:15
Function prototypes declared in this subnamespace are required to be implemented by user so that func...
Definition Logger.h:56
I_SyncLog & logger()
THE common namespace of bux library.
Definition AtomiX.cpp:3
E_LogLevel
Definition LogLevel.h:9
@ LL_VERBOSE
More detailed or advanced information probably considered too much by some.
Definition LogLevel.h:14
I_SyncLog & logger()
Definition Logger.cpp:20
std::ostream & stamp(const C_UseLog &u, E_LogLevel level)
Definition Logger.cpp:47