trantor
Non-blocking I/O cross-platform TCP network library, using C++14
Loading...
Searching...
No Matches
trantor::AsyncFileLogger Class Reference

This class implements utility functions for writing logs to files asynchronously. More...

#include <trantor/utils/AsyncFileLogger.h>

Inheritance diagram for trantor::AsyncFileLogger:
Collaboration diagram for trantor::AsyncFileLogger:

Classes

class  LoggerFile

Public Member Functions

void output (const char *msg, const uint64_t len)
 Write the message to the log file.
void flush ()
 Flush data from memory buffer to the log file.
void startLogging ()
 Start writing log files.
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.
void setMaxFiles (size_t maxFiles)
 Set the max number of log files. When the number exceeds the limit, the oldest log file will be deleted.
void setSwitchOnLimitOnly (bool flag=true)
 Set whether to switch the log file when the AsyncFileLogger object is destroyed. If this flag is set to true, the log file is not switched when the AsyncFileLogger object is destroyed.
void setFileName (const std::string &baseName, const std::string &extName=".log", const std::string &path="./")
 Set the log file name.

Protected Member Functions

void writeLogToFile (const StringPtr buf)
void logThreadFunc ()
void swapBuffer ()

Protected Attributes

std::mutex mutex_
std::condition_variable cond_
StringPtr logBufferPtr_
StringPtr nextBufferPtr_
StringPtrQueue writeBuffers_
StringPtrQueue tmpBuffers_
std::unique_ptr< std::thread > threadPtr_
bool stopFlag_ {false}
std::string filePath_ {"./"}
std::string fileBaseName_ {"trantor"}
std::string fileExtName_ {".log"}
uint64_t sizeLimit_ {20 * 1024 * 1024}
bool switchOnLimitOnly_ {false}
size_t maxFiles_ {0}
std::unique_ptr< LoggerFileloggerFilePtr_
uint64_t lostCounter_ {0}

Detailed Description

This class implements utility functions for writing logs to files asynchronously.

Member Function Documentation

◆ output()

void trantor::AsyncFileLogger::output ( const char * msg,
const uint64_t len )

Write the message to the log file.

Parameters
msg
len

◆ setFileName()

void trantor::AsyncFileLogger::setFileName ( const std::string & baseName,
const std::string & extName = ".log",
const std::string & path = "./" )
inline

Set the log file name.

Parameters
baseNameThe base name of the log file.
extNameThe extended name of the log file.
pathThe location where the log file is stored.

◆ setFileSizeLimit()

void trantor::AsyncFileLogger::setFileSizeLimit ( uint64_t limit)
inline

Set the size limit of log files. When the log file size reaches the limit, the log file is switched.

Parameters
limit

◆ setMaxFiles()

void trantor::AsyncFileLogger::setMaxFiles ( size_t maxFiles)
inline

Set the max number of log files. When the number exceeds the limit, the oldest log file will be deleted.

Parameters
maxFiles

◆ setSwitchOnLimitOnly()

void trantor::AsyncFileLogger::setSwitchOnLimitOnly ( bool flag = true)
inline

Set whether to switch the log file when the AsyncFileLogger object is destroyed. If this flag is set to true, the log file is not switched when the AsyncFileLogger object is destroyed.

Parameters
flag

The documentation for this class was generated from the following file: