Simple-Log
alpha-v0.7
|
Class for logging into files. More...
#include <FileSink.hpp>
Classes | |
struct | CleanupRule |
Type for configuring FileSink cleanup rules. More... | |
struct | RotationRule |
Type for configuring FileSink rotation rules. More... | |
Public Types | |
using | Record_t = std::remove_cvref_t< TRecord > |
Used Record type. More... | |
using | FlushPolicy_t = std::unique_ptr< detail::AbstractFlushPolicyWrapper< Record_t > > |
using | Projections_t = RecordGetters< Record_t > |
using | Formatter_t = std::function< std::string(const Record_t &)> |
using | Filter_t = std::function< bool(const Record_t &)> |
Public Member Functions | |
FileSink (std::string fileNamePattern, std::filesystem::path directory=std::filesystem::current_path()) | |
Constructor. More... | |
~FileSink () noexcept | |
Destructor. More... | |
FileSink (const FileSink &)=delete | |
Deleted copy constructor. More... | |
FileSink & | operator= (const FileSink &)=delete |
Deleted copy assign operator. More... | |
FileSink (FileSink &&)=delete | |
Deleted move constructor. More... | |
FileSink & | operator= (FileSink &&)=delete |
Deleted move assign operator. More... | |
void | setRotationRule (RotationRule rule) noexcept |
Applies a new RotationRule configuration. More... | |
RotationRule | rotationRule () const noexcept |
Returns a copy of the current RotationRule configuration. More... | |
void | rotate () |
Rotates the current file. More... | |
void | setCleanupRule (CleanupRule rule) noexcept |
Applies a new CleanupRule configuration. More... | |
CleanupRule | cleanupRule () const noexcept |
Returns a copy of the current CleanupRule configuration. More... | |
template<FileStateHandler THandler> | |
void | setOpeningHandler (THandler &&handler) noexcept |
Applies a new handler for opening files. More... | |
void | removeOpeningHandler () noexcept |
Removes the active opening handler. More... | |
template<FileStateHandler THandler> | |
void | setClosingHandler (THandler &&handler) noexcept |
Applies a new handler for closing files. More... | |
void | removeClosingHandler () noexcept |
Removes the active closing handler. More... | |
void | setDirectory (std::filesystem::path directory) |
Sets the directory in which the log files will be created. More... | |
std::filesystem::path | directory () const noexcept |
Getter of the directory member. More... | |
void | setFileNamePattern (std::string fileNamePattern) |
Sets the file name pattern for generated log files. More... | |
std::string | fileNamePattern () const noexcept |
Getter of the used file name pattern string. More... | |
template<FlushPolicyFor< Record_t > TPolicy> | |
void | setFlushPolicy (TPolicy &&policy) |
Sets the active Flush-Policy. More... | |
void | removeFlushPolicy () |
Replaces the current Flush-Policy with the default one. More... | |
void | flush () |
Flushes all pending output of the internal stream. More... | |
void | log (const Record_t &record) final override |
Handles the given Record object. More... | |
void | setEnabled (bool enable=true) noexcept final override |
Enables or disables the Sink object. More... | |
bool | isEnabled () const noexcept final override |
Checks if the Sink object is enabled. More... | |
template<RecordFormatterFor< Record_t > TFormatter> | |
void | setFormatter (TFormatter &&formatter) |
Sets the active formatter. More... | |
void | removeFormatter () |
Replaces the active formatter with the default one. More... | |
template<RecordFilterFor< Record_t > TFilter> | |
void | setFilter (TFilter &&filter) |
Sets the active filter. More... | |
void | removeFilter () |
Replaces the active filter with the default one. More... | |
Protected Member Functions | |
template<class TData > | |
void | writeToStream (TData &&data) |
Writes directly to the internal stream. More... | |
virtual void | afterMessageWrite (const Record_t &record, std::string_view message) |
Virtual method which will be called after the actual message is written to the stream. More... | |
Static Protected Member Functions | |
static constexpr FlushPolicy_t | defaultFlushPolicy () noexcept |
static constexpr Formatter_t | defaultFormatter () noexcept |
static constexpr Filter_t | defaultFilter () noexcept |
Class for logging into files.
TRecord | Used Record type. |
Instances of this class are linked to a specific file and writes every Record into it. Users can setup rotation and cleanup rules.
Users can provide a pattern string, which will be used for newly generated file names. Those file names will be generated when a new file shall be opened. There are mainly two parts of those strings:
Each token begins with %
Token | Effect |
---|---|
%Y | year in 4 digits |
%m | month in 2 digits (including leading zeros) |
%d | day of month in 2 digits (including leading zeros) |
%j | day of year |
%H | hour in 24h format (2 digits with leading zeros) |
%M | minute (2 digits with leading zeros) |
%S | second (2 digits with leading zeros) |
%N | incrementing number starting at 1 |
%xN | incrementing number with x digits and leading zeros. Replace x with an actual number (might be multiple digits). Starts at 1. |
Rotation rules are used as an indicator when to switch to a new file. Currently are two basic rules implemented:
Each rule will be checked before a new Record will be written to file. If a new file shall be opened, a new file name will be generated via provided file name pattern. If this file already exists, it will be overriden (or an exception raises if it is already in use).
While rotation is used for indicating when to switch files, CleanupRule s are used for determining when to delete old files. Cleanup process will watch at the currently provided directory and will only observe files with equally file extensions as provided in the file name pattern. If no extension (e.g. .abcd) is used, all files without an extension will be observed.
These handler are used to generate messages on state changes for the internal managed file. Users can provide invokable objects, which will be called and shall return a std::string, which will then written synchronized to the std::fstream. The synchronization is the reason, why the user is not allowed to write to the stream themselves.
Handlers shall have the following signature:
|
inherited |
|
inherited |
|
inherited |
|
inherited |
using sl::log::ISink< TRecord >::Record_t = std::remove_cvref_t<TRecord> |
Used Record type.
|
inlineexplicit |
Constructor.
fileNamePattern | Pattern string from which new file names will be generated. |
directory | The directory where all files of this sink will be generated. |
Constructs a new FileSink instance, which uses the provided file name pattern for newly opened files at the specified directory.
SinkException | if pattern string is empty or contains directory information |
|
inlinenoexcept |
Destructor.
Finalizes and closes the current opened file (if any).
|
delete |
Deleted copy constructor.
|
delete |
Deleted move constructor.
|
inlineprotectedvirtualinherited |
Virtual method which will be called after the actual message is written to the stream.
record | The current handled Record object |
message | The final message |
|
inlinenoexcept |
Returns a copy of the current CleanupRule configuration.
|
inlinestaticconstexprprotectednoexceptinherited |
|
inlinestaticconstexprprotectednoexceptinherited |
|
inlinestaticconstexprprotectednoexceptinherited |
|
inlinenoexcept |
Getter of the directory member.
|
inlinenoexcept |
Getter of the used file name pattern string.
|
inlineinherited |
Flushes all pending output of the internal stream.
|
inlinefinaloverridevirtualnoexceptinherited |
Checks if the Sink object is enabled.
Implements sl::log::ISink< TRecord >.
|
inlinefinaloverridevirtualinherited |
Handles the given Record object.
Before the Record gets passed to the actual destination, it will be checked if the Sink object is enabled and if the Record should be filtered. If these checks are passed, the abstract writeMessage function will be invoked with the finally formatted message string.
record | Record object |
Implements sl::log::ISink< TRecord >.
|
delete |
Deleted copy assign operator.
|
delete |
Deleted move assign operator.
|
inlinenoexcept |
Removes the active closing handler.
|
inlineinherited |
Replaces the active filter with the default one.
|
inlineinherited |
Replaces the current Flush-Policy with the default one.
The default Flush-Policy flushes after each handled Record.
|
inlineinherited |
Replaces the active formatter with the default one.
|
inlinenoexcept |
Removes the active opening handler.
|
inline |
Rotates the current file.
This function has no effect, if the file stream is not already open.
|
inlinenoexcept |
Returns a copy of the current RotationRule configuration.
|
inlinenoexcept |
Applies a new CleanupRule configuration.
rule | The new CleanupRule configuration. |
|
inlinenoexcept |
Applies a new handler for closing files.
For further details look at file state handler.
THandler | Type of handler (automatically deduced) |
handler | invokable handler object |
|
inline |
Sets the directory in which the log files will be created.
directory | Path object |
If the given directory does not exist, it will be created.
|
inlinefinaloverridevirtualnoexceptinherited |
Enables or disables the Sink object.
Disabled Sinks will not handle any incoming Record s
enable | True will enable the Sink object. |
Implements sl::log::ISink< TRecord >.
|
inline |
Sets the file name pattern for generated log files.
fileNamePattern | Pattern string |
For further details look here.
SinkException | if pattern string is empty or contains directory information |
|
inlineinherited |
Sets the active filter.
It's the filters job to decide, which Record will be printed (filter returns true) and which will be skipped (filter returns false). Therefore a filter must be an invokable of the following signature:
TFilter | Type of the passed filter (automatically deduced) |
filter | An invokable filter object |
|
inlineinherited |
Sets the active Flush-Policy.
TPolicy | Type of the passed Flush-Policy (automatically deduced) |
policy | The new Flush-Policy object |
|
inlineinherited |
Sets the active formatter.
It's the formatters job to extract the necessary information from Records and built the final message string.
A custom formatter should use the following signature:
TFormatter | Type of the passed formatter (automatically deduced) |
formatter | An invokable formatter object |
|
inlinenoexcept |
Applies a new handler for opening files.
For further details look at file state handler.
THandler | Type of handler (automatically deduced) |
handler | invokable handler object |
|
inlinenoexcept |
Applies a new RotationRule configuration.
rule | The new RotationRule configuration. |
|
inlineprotectedinherited |
Writes directly to the internal stream.
TData | Type of data (automatically deduced) |
data | Data which will be written to the stream. |
This functions writes directly to the stream object. No filter or formatter will be involved and stream will be flush afterwards. This might be useful for writing custom header or footer data to the stream.