trantor
Non-blocking I/O cross-platform TCP network library, using C++14
Toggle main menu visibility
Loading...
Searching...
No Matches
AsyncFileLogger.h
Go to the documentation of this file.
1
14
15
#pragma once
16
17
#include <
trantor/utils/NonCopyable.h
>
18
#include <
trantor/utils/Date.h
>
19
#include <trantor/exports.h>
20
#include <thread>
21
#include <mutex>
22
#include <string>
23
#include <condition_variable>
24
#include <sstream>
25
#include <memory>
26
#include <queue>
27
28
namespace
trantor
29
{
30
using
StringPtr = std::shared_ptr<std::string>;
31
using
StringPtrQueue = std::queue<StringPtr>;
32
38
class
TRANTOR_EXPORT AsyncFileLogger : NonCopyable
39
{
40
public
:
47
void
output
(
const
char
*msg,
const
uint64_t len);
48
53
void
flush
();
54
59
void
startLogging
();
60
67
void
setFileSizeLimit
(uint64_t limit)
68
{
69
sizeLimit_ = limit;
70
}
71
78
void
setMaxFiles
(
size_t
maxFiles)
79
{
80
maxFiles_ = maxFiles;
81
}
82
90
void
setSwitchOnLimitOnly
(
bool
flag =
true
)
91
{
92
switchOnLimitOnly_ = flag;
93
}
94
102
void
setFileName
(
const
std::string &baseName,
103
const
std::string &extName =
".log"
,
104
const
std::string &path =
"./"
)
105
{
106
fileBaseName_ = baseName;
107
extName[0] ==
'.'
? fileExtName_ = extName
108
: fileExtName_ = std::string(
"."
) + extName;
109
filePath_ = path;
110
if
(filePath_.length() == 0)
111
filePath_ =
"./"
;
112
if
(filePath_[filePath_.length() - 1] !=
'/'
)
113
filePath_ = filePath_ +
"/"
;
114
}
115
~AsyncFileLogger
();
116
AsyncFileLogger
();
117
118
protected
:
119
std::mutex mutex_;
120
std::condition_variable cond_;
121
StringPtr logBufferPtr_;
122
StringPtr nextBufferPtr_;
123
StringPtrQueue writeBuffers_;
124
StringPtrQueue tmpBuffers_;
125
void
writeLogToFile(
const
StringPtr buf);
126
std::unique_ptr<std::thread> threadPtr_;
127
bool
stopFlag_{
false
};
128
void
logThreadFunc();
129
std::string filePath_{
"./"
};
130
std::string fileBaseName_{
"trantor"
};
131
std::string fileExtName_{
".log"
};
132
uint64_t sizeLimit_{20 * 1024 * 1024};
133
bool
switchOnLimitOnly_{
false
};
// by default false, will generate new
134
// file name on each destroy.
135
size_t
maxFiles_{0};
136
137
class
LoggerFile : NonCopyable
138
{
139
public
:
140
LoggerFile(
const
std::string &filePath,
141
const
std::string &fileBaseName,
142
const
std::string &fileExtName,
143
bool
switchOnLimitOnly =
false
,
144
size_t
maxFiles = 0);
145
~LoggerFile();
146
void
writeLog(
const
StringPtr buf);
147
void
open();
148
void
switchLog(
bool
openNewOne);
149
uint64_t getLength();
150
explicit
operator
bool()
const
151
{
152
return
fp_ !=
nullptr
;
153
}
154
void
flush();
155
156
protected
:
157
void
initFilenameQueue();
158
void
deleteOldFiles();
159
160
FILE *fp_{
nullptr
};
161
Date
creationDate_;
162
std::string fileFullName_;
163
std::string filePath_;
164
std::string fileBaseName_;
165
std::string fileExtName_;
166
static
uint64_t fileSeq_;
167
bool
switchOnLimitOnly_{
false
};
// by default false, will generate new
168
// file name on each destroy
169
170
size_t
maxFiles_{0};
171
// store generated filenames
172
std::deque<std::string> filenameQueue_;
173
};
174
std::unique_ptr<LoggerFile> loggerFilePtr_;
175
176
uint64_t lostCounter_{0};
177
void
swapBuffer();
178
};
179
180
}
// namespace trantor
Date.h
NonCopyable.h
trantor::AsyncFileLogger
This class implements utility functions for writing logs to files asynchronously.
Definition
AsyncFileLogger.h:39
trantor::AsyncFileLogger::startLogging
void startLogging()
Start writing log files.
trantor::AsyncFileLogger::setFileSizeLimit
void setFileSizeLimit(uint64_t limit)
Set the size limit of log files. When the log file size reaches the limit, the log file is switched.
Definition
AsyncFileLogger.h:67
trantor::AsyncFileLogger::setFileName
void setFileName(const std::string &baseName, const std::string &extName=".log", const std::string &path="./")
Set the log file name.
Definition
AsyncFileLogger.h:102
trantor::AsyncFileLogger::flush
void flush()
Flush data from memory buffer to the log file.
trantor::AsyncFileLogger::setMaxFiles
void setMaxFiles(size_t maxFiles)
Set the max number of log files. When the number exceeds the limit, the oldest log file will be delet...
Definition
AsyncFileLogger.h:78
trantor::AsyncFileLogger::output
void output(const char *msg, const uint64_t len)
Write the message to the log file.
trantor::AsyncFileLogger::setSwitchOnLimitOnly
void setSwitchOnLimitOnly(bool flag=true)
Set whether to switch the log file when the AsyncFileLogger object is destroyed. If this flag is set ...
Definition
AsyncFileLogger.h:90
trantor::Date
This class represents a time point.
Definition
Date.h:28
trantor
Definition
EventLoop.h:34
trantor
utils
AsyncFileLogger.h
Generated by
1.17.0