mpacklog — Log dictionaries to file using MessagePack  v2.1.0
mpacklog::Logger Class Reference

Multi-threaded logger. More...

#include <Logger.h>

Public Member Functions

 Logger (const std::string &path, bool append=false)
 Open a new log file and start the logging thread. More...
 
 ~Logger ()
 Stop the logging thread. More...
 
bool put (char *data, size_t size)
 Save data to be written to the log. More...
 
bool put (const Dictionary &dict)
 Save a dictionary to be written to the log. More...
 
size_t last_size () const
 Size of the last message in bytes. More...
 

Detailed Description

Multi-threaded logger.

This logger stores input data to an internal circular memory buffer and flushes it to an output file in a separate thread.

Note
The input rate of a logger, i.e. the rate in MB/s at which its write function is called (data size / duration between calls), should always be lower than the writing rate (a.k.a. speed) of the filesystem. Otherwise circular buffer will likely fill to capacity and start dropping data.

Quick performance check for the writing rate:

dd if=/dev/random of=somefile bs=10M count=100

Definition at line 66 of file Logger.h.

Constructor & Destructor Documentation

◆ Logger()

mpacklog::Logger::Logger ( const std::string &  path,
bool  append = false 
)
explicit

Open a new log file and start the logging thread.

Parameters
[in]pathPath to the output log file.
[in]appendIf true, append to the log file if it exists.

Definition at line 30 of file Logger.cpp.

◆ ~Logger()

mpacklog::Logger::~Logger ( )

Stop the logging thread.

Definition at line 56 of file Logger.cpp.

Member Function Documentation

◆ last_size()

size_t mpacklog::Logger::last_size ( ) const
inline

Size of the last message in bytes.

Definition at line 105 of file Logger.h.

◆ put() [1/2]

bool mpacklog::Logger::put ( char *  data,
size_t  size 
)

Save data to be written to the log.

This function only copies the data to the circular buffer. It is then written later to file by the logging thread.

Parameters
dataRaw data to log.
sizeSize of data array.
Returns
Whether the item was successfully stored in the circular buffer (false means the buffer is full).

Definition at line 64 of file Logger.cpp.

◆ put() [2/2]

bool mpacklog::Logger::put ( const Dictionary &  dict)
inline

Save a dictionary to be written to the log.

Parameters
dictDictionary to log.
Returns
True if the dictionary was successfully stored for logging.
See also
put(char*, size_t)

Definition at line 99 of file Logger.h.


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