Skip to content

Commit 8d21e95

Browse files
authored
Enable multiple log files or streams (#147)
1 parent 941dc48 commit 8d21e95

File tree

6 files changed

+141
-13
lines changed

6 files changed

+141
-13
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cmake_minimum_required(VERSION 3.5)
22
project(trantor)
33

4-
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake_modules/)
4+
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake_modules/)
55

66
set(TRANTOR_MAJOR_VERSION 1)
77
set(TRANTOR_MINOR_VERSION 4)

trantor/net/EventLoop.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include <assert.h>
2727
#ifdef _WIN32
2828
#include <io.h>
29-
using ssize_t = std::intptr_t;
29+
using ssize_t = long long;
3030
#else
3131
#include <poll.h>
3232
#endif

trantor/tests/LoggerTest.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,5 +1133,7 @@ int main()
11331133
""
11341134
<< 123 << 123.123 << "haha" << '\n'
11351135
<< std::string("12356");
1136+
LOG_RAW << "Testing finished\n";
1137+
LOG_RAW_TO(5) << "Testing finished\n";
11361138
thread_.join();
11371139
}

trantor/utils/Logger.cc

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,45 @@ Logger::Logger(SourceFile file, int line, bool)
165165
logStream_ << strerror_tl(errno) << " (errno=" << errno << ") ";
166166
}
167167
}
168+
RawLogger::~RawLogger()
169+
{
170+
if (index_ < 0)
171+
{
172+
auto &oFunc = Logger::outputFunc_();
173+
if (!oFunc)
174+
return;
175+
oFunc(logStream_.bufferData(), logStream_.bufferLength());
176+
}
177+
else
178+
{
179+
auto &oFunc = Logger::outputFunc_(index_);
180+
if (!oFunc)
181+
return;
182+
oFunc(logStream_.bufferData(), logStream_.bufferLength());
183+
}
184+
}
168185
Logger::~Logger()
169186
{
170187
logStream_ << T(" - ", 3) << sourceFile_ << ':' << fileLine_ << '\n';
171-
auto oFunc = Logger::outputFunc_();
172-
if (!oFunc)
173-
return;
174-
oFunc(logStream_.bufferData(), logStream_.bufferLength());
175-
if (level_ >= kError)
176-
Logger::flushFunc_()();
188+
if (index_ < 0)
189+
{
190+
auto &oFunc = Logger::outputFunc_();
191+
if (!oFunc)
192+
return;
193+
oFunc(logStream_.bufferData(), logStream_.bufferLength());
194+
if (level_ >= kError)
195+
Logger::flushFunc_()();
196+
}
197+
else
198+
{
199+
auto &oFunc = Logger::outputFunc_(index_);
200+
if (!oFunc)
201+
return;
202+
oFunc(logStream_.bufferData(), logStream_.bufferLength());
203+
if (level_ >= kError)
204+
Logger::flushFunc_(index_)();
205+
}
206+
177207
// logStream_.resetBuffer();
178208
}
179209
LogStream &Logger::stream()

trantor/utils/Logger.h

Lines changed: 100 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
#include <trantor/utils/Date.h>
1919
#include <trantor/utils/LogStream.h>
2020
#include <trantor/exports.h>
21-
#include <string.h>
21+
#include <cstring>
2222
#include <functional>
2323
#include <iostream>
24+
#include <vector>
2425

2526
namespace trantor
2627
{
@@ -79,6 +80,11 @@ class TRANTOR_EXPORT Logger : public NonCopyable
7980
Logger(SourceFile file, int line, bool isSysErr);
8081
Logger(SourceFile file, int line, LogLevel level, const char *func);
8182
~Logger();
83+
Logger &setIndex(int index)
84+
{
85+
index_ = index;
86+
return *this;
87+
}
8288
LogStream &stream();
8389

8490
/**
@@ -90,10 +96,19 @@ class TRANTOR_EXPORT Logger : public NonCopyable
9096
*/
9197
static void setOutputFunction(
9298
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)
94101
{
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+
}
97112
}
98113

99114
/**
@@ -147,11 +162,60 @@ class TRANTOR_EXPORT Logger : public NonCopyable
147162
static std::function<void()> flushFunc = Logger::defaultFlushFunction;
148163
return flushFunc;
149164
}
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;
150195
LogStream logStream_;
151196
Date date_{Date::now()};
152197
SourceFile sourceFile_;
153198
int fileLine_;
154199
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};
155219
};
156220
#ifdef NDEBUG
157221
#define LOG_TRACE \
@@ -163,21 +227,53 @@ class TRANTOR_EXPORT Logger : public NonCopyable
163227
if (trantor::Logger::logLevel() <= trantor::Logger::kTrace) \
164228
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kTrace, __func__) \
165229
.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+
166236
#endif
237+
167238
#define LOG_DEBUG \
168239
if (trantor::Logger::logLevel() <= trantor::Logger::kDebug) \
169240
trantor::Logger(__FILE__, __LINE__, trantor::Logger::kDebug, __func__) \
170241
.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()
171247
#define LOG_INFO \
172248
if (trantor::Logger::logLevel() <= trantor::Logger::kInfo) \
173249
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()
174253
#define LOG_WARN \
175254
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()
176259
#define LOG_ERROR \
177260
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()
178265
#define LOG_FATAL \
179266
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()
180271
#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()
181277

182278
#define LOG_TRACE_IF(cond) \
183279
if ((trantor::Logger::logLevel() <= trantor::Logger::kTrace) && (cond)) \

trantor/utils/MsgBuffer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include <assert.h>
2323
#include <string.h>
2424
#ifdef _WIN32
25-
using ssize_t = std::intptr_t;
25+
using ssize_t = long long;
2626
#endif
2727

2828
namespace trantor

0 commit comments

Comments
 (0)