constexpr size_t max_size_size_t { (size_t)-1 }; constexpr size_t pageSize = #if defined(_WIN32) && not defined(_WIN64) static_cast<size_t>(2 * MB); #elif defined _WIN64 static_cast<size_t>(4 * MB); #else static_cast<size_t>(DEFAULT_BUFFER_SIZE); #endif // _WIN32 && !_WIN64 class FileHelper { public: explicit FileHelper(const std::string_view fpath); FileHelper(FileHelper&) = delete; FileHelper& operator=(FileHelper&) = delete; ~FileHelper() = default; bool OpenFile(bool truncate = false); void WriteToFile(size_t writeLimit = max_size_size_t, bool truncateRest = false); void Flush(); bool CloseFile(bool onRotation = false); std::vector<char>& FileBuffer(); size_t FileBufferSize(); void SetBufferCapacity(size_t value); void InitializeFilePath(std::string_view fileName = ""); virtual bool RenameFile(utf_utils::InputSource newFileName); void BackgroundFlushThread(std::stop_token stopToken); void StopBackgroundThread(); void StartBackgroundThread(); void PauseBackgroundThread(); void ResumeBackgroundThread(); void SyncTargetHelpers(std::shared_ptr<BaseTargetHelper>& syncToHelper); const std::unique_ptr<FileCache>& FileCacheHelper() const; const std::unique_ptr<BackgroundThread>& BackgoundThreadInfo() const; private: void OpenImpl(bool truncate); void WriteImpl(); void WriteImpl(size_t writeLimit, bool truncateRest = false); void FlushImpl(); void CloseImpl(); private: int retryAttempt; bool fileOpen; int file; std::vector<char> buffer; std::mutex fileHelperMutex; std::unique_ptr<FileCache> fileCache; std::shared_ptr<BaseTargetHelper> targetHelper; std::unique_ptr<BackgroundThread> flushWorker; };
Namespace Constexpr Globals | |||||
---|---|---|---|---|---|
constexpr size_t |
max_size_size_t
This constexpr variable is set to the max size that type size_t can store and is simply used in checks in
WriteToFile()
|
||||
constexpr size_t |
pageSize
This constexpr variable is used in initially reserving the file buffer size and is used in
writes to disk for chunking operations.
|
||||
Public Functions | |||||
explicit |
FileHelper(const std::string_view fpath);
This is the only constructor available for this class.
|
||||
bool |
OpenFile(bool truncate);
This function opens the file that was cached from the constructor.
|
||||
void |
WriteToFile(size_t writeLimit, bool truncateRest);
This function is what's in charge of writing the log messages from the file buffer to disk.
|
||||
void |
Flush();
This function is in charge of flushing the file buffer to the disk.
|
||||
bool |
CloseFile(bool onRotation);
This function is in control of closing the file that was cached.
|
||||
std::vector<char>& |
FileBuffer();
This function simply returns a reference to the file buffer being used.
|
||||
size_t |
FileBufferSize();
This function returns the file buffer's current size.
|
||||
void |
SetBufferCapacity(size_t value);
This function is used to set the file buffer's capacity.
|
||||
void |
InitializeFilePath(std::string_view
fileName);
This function is mainly used in the default constructors of the in-house targets, as well as
the constructors that don't take in a file path, but do take a file name as a parameter.
|
||||
virtual bool |
RenameFile(utf_utils::InputSource newFileName);
This virtual function is in charge of renaming the log file. This function is made virtual
so that the targets may override the default behavior of this function if desired.
|
||||
void |
BackgroundFlushThread(std::stop_token
stopToken);
This function is the workload of the background thread in charge of flushing the file
buffer to disk after some set interval has elapsed.
|
||||
void |
StopBackgroundThread();
This function is in charge of stopping the background thread in charge of flushing the file
buffer to disk.
|
||||
void |
StartBackgroundThread();
This function is in charge of starting the background thread in charge of flushing the file
buffer to disk.
|
||||
void |
PauseBackgroundThread();
This function temporarily pauses the background thread in charge of flushing until it's
notified that it can resume again.
|
||||
void | ResumeBackgroundThread(); | ||||
void |
SyncTargetHelpers(std::shared_ptr<BaseTargetHelper>&
syncToHelper);
This function simply points the member variable targetHelper to `syncToHelper` in order for this class to utilize
the
logging target's settings.
|
||||
const std::unique_ptr<FileCache>& |
FileCacheHelper() const;
This function simply returns a const reference to the smart pointer member variable fileCache
|
||||
const std::unique_ptr<BackgroundThread>& |
BackgoundThreadInfo() const;
This function simply returns a const reference to the smart pointer member variable flushWorker
|
||||
Private Functions | |||||
void |
OpenImpl(bool truncate);
This function is called in OpenFile().
|
||||
void |
WriteImpl();
This function is called in WriteToFile() and
is the implementation block that writes bytes to disk.
|
||||
void |
WriteImpl(size_t writeLimit, bool
truncateRest);
This function is called in WriteToFile() and
is the implementation block that writes bytes to disk.
|
||||
void |
FlushImpl();
This function is called in Flush() and is in
charge of flushing the entire file buffer to disk in an efficient manner
using page size chunks to write.
|
||||
void |
CloseImpl();
This function is in charge of the actual file close logic and is called from CloseFile()
|
constexpr size_t max_size_size_t |
---|
This constexpr variable is set to the max size that type size_t can store and is simply used in checks in
WriteToFile()
|
constexpr size_t pageSize |
This constexpr variable is used in initially reserving the file buffer size and is used in
writes to disk for chunking operations.
|
explicit FileHelper(const std::string_view fpath); |
This is the only constructor available for this class.
This class constructor:
The member variable `targetHelper` is what is used to set the flush policy as well as what determines whether or not multi-threading support is enabled or disabled. This class is merely a link-step for the targets that use FileHelper in their composition and the values/handles are synced from the target side by calling this class's function `void SyncTargetHelpers(std::shared_ptr The member variable `flushWorker` is what is used for thread cooperation between the logging thread(s) and the background flushing thread when multi-thread support is enabled and/or when the flush policy is set to an interval based flush option. The relevant thread-related functions in this class use this smart pointer as well as `targetHelper` to minimize thread contention while logs are being written.. |
bool OpenFile(bool truncate = false); |
This function opens the file that was cached from the constructor.
|
void WriteToFile(size_t writeLimit = max_size_size_t, bool truncateRest = false); |
This function is what's in charge of writing the log messages from the file buffer to disk.
|
void Flush(); |
This function is in charge of flushing the file buffer to the disk.
|
bool CloseFile(bool onRotation = false); |
This function is in control of closing the file that was cached.
|
std::vector<char>& FileBuffer(); |
This function simply returns a reference to the file buffer being used.
|
size_t FileBufferSize(); |
This function returns the file buffer's current size.
|
void SetBufferCapacity(size_t value); |
This function is used to set the file buffer's capacity.
|
void InitializeFilePath(std::string_view fileName = ""); |
This function is mainly used in the default constructors of the in-house targets, as well as
the constructors that don't take in a file path, but do take a file name as a parameter.
This function will:
|
virtual bool RenameFile(utf_utils::InputSource newFileName); |
This virtual function is in charge of renaming the log file. This function is made virtual
so that the targets may override the default behavior of this function if desired. An
example of this is in RotateTarget's `RenameFile()` override.
This function's default behavior will:
|
void BackgroundFlushThread(std::stop_token stopToken); |
This function is the workload of the background thread in charge of flushing the file
buffer to disk after some set interval has elapsed.
|
void StopBackgroundThread(); |
This function is in charge of stopping the background thread in charge of flushing the file
buffer to disk.
This function will:
|
void StartBackgroundThread(); |
This function is in charge of starting the background thread in charge of flushing the file
buffer to disk.
This function will:
|
void PauseBackgroundThread(); |
This function temporarily pauses the background thread in charge of flushing until it's
notified that it can resume again.
The atomic pauseThread is located in the struct BackgroundThread |
void ResumeBackgroundThread(); |
This function resumes the background thread in charge of flushing.
The atomic pauseThread is located in the struct BackgroundThread |
void SyncTargetHelpers(std::shared_ptr<BaseTargetHelper>& syncToHelper); |
This function simply points the member variable targetHelper to `syncToHelper` in order for this class to utilize
the logging target's settings.
|
const std::unique_ptr<FileCache>& FileCacheHelper() const; |
This function simply returns a const reference to the smart pointer member variable fileCache
|
const std::unique_ptr<BackgroundThread>& BackgoundThreadInfo() const; |
This function simply returns a const reference to the smart pointer member variable flushWorker
|
private: void OpenImpl(bool truncate); |
This function is called in OpenFile().
|
private: void WriteImpl(); |
This function is called in WriteToFile() and
is the implementation block that writes bytes to disk.
|
private: void WriteImpl(size_t writeLimit, bool truncateRest = false); |
This function is called in WriteToFile() and
is the implementation block that writes bytes to disk.
|
private: void FlushImpl(); |
This function is called in Flush() and is in
charge of flushing the entire file buffer to disk in an efficient manner
using page size chunks to write.
|
private: void CloseImpl(); |
This function is in charge of the actual file close logic and is called from CloseFile()
|