-
Notifications
You must be signed in to change notification settings - Fork 7
/
log.hpp
259 lines (204 loc) · 7.45 KB
/
log.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// solid/system/log.hpp
//
// Copyright (c) 2018 Valentin Palade (vipalade @ gmail . com)
//
// This file is part of SolidFrame framework.
//
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.
//
#pragma once
#include "solid/system/chunkedstream.hpp"
#include "solid/system/common.hpp"
#include "solid/system/error.hpp"
#include <atomic>
#include <cstring>
#include <memory>
#include <ostream>
#include <string>
namespace solid {
using LogAtomicFlagsBackT = unsigned long;
using LogAtomicFlagsT = std::atomic<LogAtomicFlagsBackT>;
enum struct LogFlags : LogAtomicFlagsBackT {
Verbose,
Info,
Warning,
Error,
Statistic,
Raw,
Exception,
LastFlag,
};
struct LogCategoryBase {
virtual ~LogCategoryBase();
virtual void parse(LogAtomicFlagsBackT& _ror_flags, LogAtomicFlagsBackT& _rand_flags, const std::string& _txt) const = 0;
};
struct LogCategory : public LogCategoryBase {
static const LogCategory& the();
const char* flagName(const LogFlags _flag) const
{
switch (_flag) {
case LogFlags::Verbose:
return "V";
case LogFlags::Info:
return "I";
case LogFlags::Warning:
return "W";
case LogFlags::Error:
return "E";
case LogFlags::Statistic:
return "S";
case LogFlags::Raw:
return "R";
case LogFlags::Exception:
return "X";
case LogFlags::LastFlag:
break;
}
return "?";
}
private:
void parse(LogAtomicFlagsBackT& _ror_flags, LogAtomicFlagsBackT& _rand_flags, const std::string& _txt) const override;
};
struct LogLineBase {
virtual ~LogLineBase();
virtual std::ostream& writeTo(std::ostream&) const = 0;
virtual size_t size() const = 0;
};
namespace impl {
template <size_t Size>
class LogLineStream : public OChunkedStream<Size, Size>, public LogLineBase {
using BaseStream = OChunkedStream<Size, Size>;
public:
std::ostream& writeTo(std::ostream& _ros) const override
{
return BaseStream::writeTo(_ros);
}
size_t size() const override
{
return BaseStream::size();
}
};
} // namespace impl
std::ostream& operator<<(std::ostream& _ros, const LogLineBase& _line);
class LoggerBase : NonCopyable {
// friend class Engine;
const std::string name_;
LogAtomicFlagsT flags_;
const size_t idx_;
protected:
LoggerBase(const std::string& _name, const LogCategoryBase& _rlc);
~LoggerBase();
std::ostream& doLog(std::ostream& _ros, const char* _flag_name, const char* _file, const char* _fnc, int _line) const;
void doDone(const LogLineBase& _log_ros) const;
public:
const std::string& name() const
{
return name_;
}
void remask(const LogAtomicFlagsBackT _msk)
{
flags_.store(_msk);
}
LogAtomicFlagsBackT flags() const
{
return flags_.load(std::memory_order_relaxed);
}
};
template <class Flgs = LogFlags, class LogCat = LogCategory>
class Logger : protected LoggerBase {
const LogCat& rcat_;
public:
using ThisT = Logger<Flgs, LogCat>;
using FlagT = Flgs;
Logger(const std::string& _name)
: LoggerBase(_name, LogCat::the())
, rcat_(LogCat::the())
{
}
bool shouldLog(const FlagT _flag) const
{
return (flags() & (1UL << static_cast<size_t>(_flag))) != 0;
}
std::ostream& log(std::ostream& _ros, const FlagT _flag, const char* _file, const char* _fnc, int _line) const
{
return this->doLog(_ros, rcat_.flagName(_flag), _file, _fnc, _line);
}
void done(const LogLineBase& _log_ros) const
{
doDone(_log_ros);
}
};
using LoggerT = Logger<>;
extern const LoggerT generic_logger;
void log_stop();
struct LogRecorder : NonCopyable {
virtual ~LogRecorder();
virtual void recordLine(const solid::LogLineBase& /*_rlog_line*/);
};
struct LogStreamRecorder : LogRecorder {
std::ostream& ros_;
LogStreamRecorder(std::ostream& _ros)
: ros_(_ros)
{
}
void recordLine(const solid::LogLineBase& _rlog_line) override;
};
using LogRecorderPtrT = std::shared_ptr<LogRecorder>;
ErrorConditionT log_start(
LogRecorderPtrT&& _rec_ptr,
const std::vector<std::string>& _rmodule_mask_vec);
ErrorConditionT log_start(
std::ostream& _ros,
const std::vector<std::string>& _rmodule_mask_vec);
ErrorConditionT log_start(
const char* _fname,
const std::vector<std::string>& _rmodule_mask_vec,
bool _buffered = true,
uint32_t _respincnt = 2,
uint64_t _respinsize = 1024 * 1024 * 1024);
ErrorConditionT log_start(
const char* _addr,
const char* _port,
const std::vector<std::string>& _rmodule_mask_vec,
bool _buffered = true);
#ifndef SOLID_LOG_BUFFER_SIZE
constexpr size_t log_buffer_size = 2 * 1024;
#else
constexpr size_t log_buffer_size = (SOLID_LOG_BUFFER_SIZE);
#endif
} // namespace solid
#ifndef SOLID_FUNCTION_NAME
#ifdef SOLID_ON_WINDOWS
#define SOLID_FUNCTION_NAME __func__
#else
#define SOLID_FUNCTION_NAME __FUNCTION__
#endif
#endif
#ifdef SOLID_HAS_DEBUG
#define solid_dbg(Lgr, Flg, Txt) \
if (Lgr.shouldLog(std::remove_reference<decltype(Lgr)>::type::FlagT::Flg)) { \
solid::impl::LogLineStream<solid::log_buffer_size> os; \
Lgr.log(os, std::remove_reference<decltype(Lgr)>::type::FlagT::Flg, __FILE__, static_cast<const char*>((SOLID_FUNCTION_NAME)), __LINE__) << Txt << std::endl; \
Lgr.done(os); \
}
#else
#define solid_dbg(...)
#endif
#define solid_log(Lgr, Flg, Txt) \
if (Lgr.shouldLog(std::remove_reference<decltype(Lgr)>::type::FlagT::Flg)) { \
solid::impl::LogLineStream<solid::log_buffer_size> os; \
Lgr.log(os, std::remove_reference<decltype(Lgr)>::type::FlagT::Flg, __FILE__, static_cast<const char*>((SOLID_FUNCTION_NAME)), __LINE__) << Txt << std::endl; \
Lgr.done(os); \
}
#define solid_log_raw(Txt) \
if (solid::generic_logger.shouldLog(solid::LogFlags::Raw)) { \
solid::impl::LogLineStream<solid::log_buffer_size> os; \
os << Txt; \
Lgr.done(os)
#ifdef SOLID_HAS_STATISTICS
#define solid_collect(om, ...) \
om(__VA_ARGS__)
#else
#define solid_collect(...)
#endif