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
include
bux
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
9
namespace
bux
{
10
11
//
12
// Types
13
//
14
class
C_EntryLog
17
{
18
public
:
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);
23
~C_EntryLog
();
24
25
private
:
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
//
38
I_SyncLog
&
logger
();
39
std::ostream &
stamp
(
const
C_UseLog
&u,
E_LogLevel
level);
40
41
//
42
// Implement Class Member Templates
43
//
44
template
<
class
T_Fmt,
class
... T_Args>
45
C_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
#ifdef _WIN32
52
stamp
(u,LL_VERBOSE) << std::vformat(fmtfmt, std::make_format_args(args...));
53
#else
54
stamp
(u,LL_VERBOSE) << std::vformat(fmtfmt, std::make_format_args(std::forward<T_Args>(args)...));
55
#endif
56
}
57
deeper();
58
}
59
60
namespace
user {
61
I_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
#define SCOPELOG_(line,scope) bux::C_EntryLog _gluePair_(_log_,line)(scope)
71
#define SCOPELOGX_(line,scope,fmtStr, ...) bux::C_EntryLog _gluePair_(_log_,line)(scope,fmtStr, ##__VA_ARGS__)
72
#define DEF_LOGGER_HEAD_ namespace bux { namespace user { I_SyncLog &logger() {
73
#define DEF_LOGGER_TAIL_(x) return x; }}}
74
75
//
76
// End-User Macros
77
//
78
#define LOG(ll,fmtStr, ...) do if (bux::C_UseLog u{bux::logger(),ll}) stamp(u,ll) <<std::format(fmtStr, ##__VA_ARGS__) <<'\n'; while(false)
79
#define LOG_RAW(fmtStr, ...) do if (bux::C_UseLog u{bux::logger()}) *u <<std::format(fmtStr, ##__VA_ARGS__) <<'\n'; while(false)
80
#define SCOPELOG(scope) SCOPELOG_(__LINE__,scope)
81
#define SCOPELOGX(scope,fmtStr, ...) SCOPELOGX_(__LINE__,scope,fmtStr, ##__VA_ARGS__)
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
142
#define FUNLOG SCOPELOG(CUR_FUNC_)
143
#define FUNLOGX(fmtStr, ...) SCOPELOGX(CUR_FUNC_,fmtStr, ##__VA_ARGS__)
SyncLog.h
XPlatform.h
bux::C_EntryLog
Log on both declaration point and end of block scope with an unique id.
Definition
Logger.h:17
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_UseLog
Definition
SyncLog.h:205
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::logger
I_SyncLog & logger()
Definition
Logger.cpp:20
bux::stamp
std::ostream & stamp(const C_UseLog &u, E_LogLevel level)
Definition
Logger.cpp:47
Generated by
1.12.0