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