DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
FileLogger.cpp
Go to the documentation of this file.
1#include "pch.h"
3#include <cstdio>
4#include "FileLoggerContext.h"
5
6namespace DotNetDupe {
7 namespace Extensions {
8 namespace Logging {
9
10 struct FileLogger::Impl {
11 DotNetDupe::System::SmartPointer<FileLoggerContext> pContext;
12 };
13
16 : LoggerBase(categoryName, config), m_pImpl(DotNetDupe::System::SmartPointer<Impl>::NewShared()) {
18 m_pImpl->pContext = pContext;
19 }
20
21 FileLogger::~FileLogger() = default;
22
23 void FileLogger::Log(LogLevel logLevel, const DotNetDupe::System::String& message) {
25 LoggerBase::Log(logLevel, message);
26 }
27
28 static void PerformFileRollover(const LoggerConfiguration& config, FileLoggerContext* pContext) {
30 pContext->fileStream->close();
31 std::string sBaseName = config.FilePath.GetRawString();
32
34 for (int iIdx = config.Rollover.MaxBackupFiles - 1; iIdx >= 1; --iIdx) {
35 std::string sOldBackup = sBaseName + "." + std::to_string(iIdx);
36 std::string sNewBackup = sBaseName + "." + std::to_string(iIdx + 1);
37 std::remove(sNewBackup.c_str());
38 std::rename(sOldBackup.c_str(), sNewBackup.c_str());
39 }
40
42 std::string sFirstBackup = sBaseName + ".1";
43 std::remove(sFirstBackup.c_str());
44 std::rename(sBaseName.c_str(), sFirstBackup.c_str());
45
47 pContext->fileStream->open(sBaseName, std::ios::out | std::ios::app);
48 }
49
50 static void CheckAndRollover(const LoggerConfiguration& config, FileLoggerContext* pContext, int iLineLength) {
52 if (!config.Rollover.EnableRollover || !pContext->fileStream || !pContext->fileStream->is_open()) return;
53
55 auto currentPos = pContext->fileStream->tellp();
56 if (currentPos != std::streampos(-1) && static_cast<long long>(currentPos) + iLineLength >= config.Rollover.MaxFileSizeInBytes) {
57 PerformFileRollover(config, pContext);
58 }
59 }
60
61 void FileLogger::Log(LogLevel logLevel, const DotNetDupe::System::String& message,
64 if (!IsEnabled(logLevel)) return;
65 if (!m_pImpl->pContext || !m_pImpl->pContext->fileStream || !m_pImpl->pContext->fileStream->is_open()) return;
66
68 DotNetDupe::System::String sLine = BuildLogMessage(logLevel, message, properties);
69 std::lock_guard<std::mutex> lock(*(m_pImpl->pContext->fileMutex));
70
72 CheckAndRollover(m_config, m_pImpl->pContext.Get(), sLine.GetLength());
73
75 if (m_pImpl->pContext->fileStream && m_pImpl->pContext->fileStream->is_open()) {
76 (*(m_pImpl->pContext->fileStream)) << sLine.GetRawString() << std::endl;
77 }
78 }
79
80 }
81 }
82}
void Log(LogLevel logLevel, const DotNetDupe::System::String &message) override
Appends a plain message entry to the log file.
~FileLogger() override
Destructor releasing logger resources.
FileLogger(const DotNetDupe::System::String &categoryName, const LoggerConfiguration &config, const DotNetDupe::System::SmartPointer< FileLoggerContext > &context)
Initializes a new FileLogger instance.
void Log(LogLevel logLevel, const DotNetDupe::System::String &message) override
Writes a log entry without extra structured properties.
bool IsEnabled(LogLevel logLevel) const override
Checks whether logging is enabled for the specified log level.
DotNetDupe::System::String BuildLogMessage(LogLevel logLevel, const DotNetDupe::System::String &message, const DotNetDupe::System::Collections::Generic::Dictionary< DotNetDupe::System::String, DotNetDupe::System::String > &properties) const
Builds the final log message string applying plain text or JSON serialization.
LoggerBase(const DotNetDupe::System::String &categoryName, const LoggerConfiguration &config)
Initializes a new LoggerBase instance with category name and configuration.
Represents a collection of keys and values.
Definition Dictionary.h:66
A unified smart pointer that supports both unique and shared ownership semantics.
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
Definition String.h:74
const char * GetRawString() const
Definition String.cpp:230
LogLevel
Defines logging severity levels.
Definition ILogger.h:15
static void CheckAndRollover(const LoggerConfiguration &config, FileLoggerContext *pContext, int iLineLength)
static void PerformFileRollover(const LoggerConfiguration &config, FileLoggerContext *pContext)
long long MaxFileSizeInBytes
Maximum size in bytes of the primary log file before rotation (default: 5 MB).
bool EnableRollover
Indicates whether file rotation is enabled when file size threshold is reached.
int MaxBackupFiles
Maximum number of rotated backup log files to retain (default: 3).
Encapsulates logging system configuration options.
DotNetDupe::System::String FilePath
Destination file path when file logging is configured.
FileRolloverConfig Rollover
File rotation settings.