18
18
#include < trantor/utils/Date.h>
19
19
#include < trantor/utils/LogStream.h>
20
20
#include < trantor/exports.h>
21
- #include < string.h >
21
+ #include < cstring >
22
22
#include < functional>
23
23
#include < iostream>
24
+ #include < vector>
24
25
25
26
namespace trantor
26
27
{
@@ -79,6 +80,11 @@ class TRANTOR_EXPORT Logger : public NonCopyable
79
80
Logger (SourceFile file, int line, bool isSysErr);
80
81
Logger (SourceFile file, int line, LogLevel level, const char *func);
81
82
~Logger ();
83
+ Logger &setIndex (int index)
84
+ {
85
+ index_ = index;
86
+ return *this ;
87
+ }
82
88
LogStream &stream ();
83
89
84
90
/* *
@@ -90,10 +96,19 @@ class TRANTOR_EXPORT Logger : public NonCopyable
90
96
*/
91
97
static void setOutputFunction (
92
98
std::function<void (const char *msg, const uint64_t len)> outputFunc,
93
- std::function<void()> flushFunc)
99
+ std::function<void()> flushFunc,
100
+ int index = -1)
94
101
{
95
- outputFunc_ () = outputFunc;
96
- flushFunc_ () = flushFunc;
102
+ if (index < 0 )
103
+ {
104
+ outputFunc_ () = outputFunc;
105
+ flushFunc_ () = flushFunc;
106
+ }
107
+ else
108
+ {
109
+ outputFunc_ (index) = outputFunc;
110
+ flushFunc_ (index) = flushFunc;
111
+ }
97
112
}
98
113
99
114
/* *
@@ -147,11 +162,60 @@ class TRANTOR_EXPORT Logger : public NonCopyable
147
162
static std::function<void ()> flushFunc = Logger::defaultFlushFunction;
148
163
return flushFunc;
149
164
}
165
+ static std::function<void (const char *msg, const uint64_t len)>
166
+ &outputFunc_ (size_t index)
167
+ {
168
+ static std::vector<
169
+ std::function<void (const char *msg, const uint64_t len)>>
170
+ outputFuncs;
171
+ if (index < outputFuncs.size ())
172
+ {
173
+ return outputFuncs[index];
174
+ }
175
+ while (index >= outputFuncs.size ())
176
+ {
177
+ outputFuncs.emplace_back (outputFunc_ ());
178
+ }
179
+ return outputFuncs[index];
180
+ }
181
+ static std::function<void ()> &flushFunc_ (size_t index)
182
+ {
183
+ static std::vector<std::function<void ()>> flushFuncs;
184
+ if (index < flushFuncs.size ())
185
+ {
186
+ return flushFuncs[index];
187
+ }
188
+ while (index >= flushFuncs.size ())
189
+ {
190
+ flushFuncs.emplace_back (flushFunc_ ());
191
+ }
192
+ return flushFuncs[index];
193
+ }
194
+ friend class RawLogger ;
150
195
LogStream logStream_;
151
196
Date date_{Date::now ()};
152
197
SourceFile sourceFile_;
153
198
int fileLine_;
154
199
LogLevel level_;
200
+ int index_{-1 };
201
+ };
202
+ class TRANTOR_EXPORT RawLogger : public NonCopyable
203
+ {
204
+ public:
205
+ ~RawLogger ();
206
+ RawLogger &setIndex (int index)
207
+ {
208
+ index_ = index;
209
+ return *this ;
210
+ }
211
+ LogStream &stream ()
212
+ {
213
+ return logStream_;
214
+ }
215
+
216
+ private:
217
+ LogStream logStream_;
218
+ int index_{-1 };
155
219
};
156
220
#ifdef NDEBUG
157
221
#define LOG_TRACE \
@@ -163,21 +227,53 @@ class TRANTOR_EXPORT Logger : public NonCopyable
163
227
if (trantor::Logger::logLevel() <= trantor::Logger::kTrace ) \
164
228
trantor::Logger (__FILE__, __LINE__, trantor::Logger::kTrace , __func__) \
165
229
.stream()
230
+ #define LOG_TRACE_TO (index ) \
231
+ if (trantor::Logger::logLevel() <= trantor::Logger::kTrace ) \
232
+ trantor::Logger (__FILE__, __LINE__, trantor::Logger::kTrace , __func__) \
233
+ .setIndex(index) \
234
+ .stream()
235
+
166
236
#endif
237
+
167
238
#define LOG_DEBUG \
168
239
if (trantor::Logger::logLevel() <= trantor::Logger::kDebug ) \
169
240
trantor::Logger (__FILE__, __LINE__, trantor::Logger::kDebug , __func__) \
170
241
.stream()
242
+ #define LOG_DEBUG_TO (index ) \
243
+ if (trantor::Logger::logLevel() <= trantor::Logger::kDebug ) \
244
+ trantor::Logger (__FILE__, __LINE__, trantor::Logger::kDebug , __func__) \
245
+ .setIndex(index) \
246
+ .stream()
171
247
#define LOG_INFO \
172
248
if (trantor::Logger::logLevel() <= trantor::Logger::kInfo ) \
173
249
trantor::Logger (__FILE__, __LINE__).stream()
250
+ #define LOG_INFO_TO (index ) \
251
+ if (trantor::Logger::logLevel() <= trantor::Logger::kInfo ) \
252
+ trantor::Logger (__FILE__, __LINE__).setIndex(index).stream()
174
253
#define LOG_WARN \
175
254
trantor::Logger (__FILE__, __LINE__, trantor::Logger::kWarn ).stream()
255
+ #define LOG_WARN_TO (index ) \
256
+ trantor::Logger (__FILE__, __LINE__, trantor::Logger::kWarn ) \
257
+ .setIndex(index) \
258
+ .stream()
176
259
#define LOG_ERROR \
177
260
trantor::Logger (__FILE__, __LINE__, trantor::Logger::kError ).stream()
261
+ #define LOG_ERROR_TO (index ) \
262
+ trantor::Logger (__FILE__, __LINE__, trantor::Logger::kError ) \
263
+ .setIndex(index) \
264
+ .stream()
178
265
#define LOG_FATAL \
179
266
trantor::Logger (__FILE__, __LINE__, trantor::Logger::kFatal ).stream()
267
+ #define LOG_FATAL_TO (index ) \
268
+ trantor::Logger (__FILE__, __LINE__, trantor::Logger::kFatal ) \
269
+ .setIndex(index) \
270
+ .stream()
180
271
#define LOG_SYSERR trantor::Logger (__FILE__, __LINE__, true ).stream()
272
+ #define LOG_SYSERR_TO (index ) \
273
+ trantor::Logger (__FILE__, __LINE__, true ).setIndex(index).stream()
274
+
275
+ #define LOG_RAW trantor::RawLogger ().stream()
276
+ #define LOG_RAW_TO (index ) trantor::RawLogger().setIndex(index).stream()
181
277
182
278
#define LOG_TRACE_IF (cond ) \
183
279
if ((trantor::Logger::logLevel() <= trantor::Logger::kTrace ) && (cond)) \
0 commit comments