DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
FileLoggerProvider.cpp
Go to the documentation of this file.
1#include "pch.h"
5#include "System/IO/Path.h"
8#include "FileLoggerContext.h"
9
10namespace DotNetDupe {
11 namespace Extensions {
12 namespace Logging {
13
18
20 using namespace DotNetDupe::System::IO;
22 DotNetDupe::System::String sTargetPath = rawFilePath.IsEmpty() ? DotNetDupe::System::String("logs/app.log") : rawFilePath;
23
25 DotNetDupe::System::String sFullPath = Path::GetFullPath(sTargetPath);
27
29 if (!sParentDir.IsEmpty() && !Directory::Exists(sParentDir)) {
30 Directory::CreateDirectory(sParentDir, true);
31 }
32
34 return sFullPath;
35 }
36
37 struct FileLoggerProvider::Impl {
38 LoggerConfiguration config;
40 };
41
43 : m_pImpl(DotNetDupe::System::SmartPointer<Impl>::NewShared()) {
45 m_pImpl->config = config;
46 m_pImpl->config.FilePath = ResolveAndPrepareLogPath(config.FilePath);
47
50 m_pImpl->pContext->fileMutex = std::make_shared<std::mutex>();
51 m_pImpl->pContext->fileStream = std::make_shared<std::ofstream>(m_pImpl->config.FilePath.GetRawString(), std::ios::out | std::ios::app);
52 }
53
55 : m_pImpl(DotNetDupe::System::SmartPointer<Impl>::NewShared()) {
57 m_pImpl->config.FilePath = ResolveAndPrepareLogPath(filePath);
58 m_pImpl->config.IsJsonFormat = isJsonFormat;
59 m_pImpl->config.MinLevel = minLevel;
60
63 m_pImpl->pContext->fileMutex = std::make_shared<std::mutex>();
64 m_pImpl->pContext->fileStream = std::make_shared<std::ofstream>(m_pImpl->config.FilePath.GetRawString(), std::ios::out | std::ios::app);
65 }
66
69 if (m_pImpl && m_pImpl->pContext && m_pImpl->pContext->fileMutex && m_pImpl->pContext->fileStream) {
70 std::lock_guard<std::mutex> lock(*(m_pImpl->pContext->fileMutex));
71 if (m_pImpl->pContext->fileStream->is_open()) {
72 m_pImpl->pContext->fileStream->close();
73 }
74 }
75 }
76
79 return m_pImpl->config.FilePath;
80 }
81
87
88 }
89 }
90}
The exception that is thrown when an I/O error occurs.
const DotNetDupe::System::String & GetFilePath() const
Gets the configured destination file path.
FileLoggerProvider()
Initializes a new FileLoggerProvider using default configuration.
DotNetDupe::System::SmartPointer< ILogger > CreateLogger(const DotNetDupe::System::String &categoryName) override
Creates a FileLogger instance for the specified category.
~FileLoggerProvider() override
Destructor releasing file provider resources.
Central thread-safe static registry and entry point for application logging.
Definition LogManager.h:18
static void CreateDirectory(const String &sPath)
Creates all directories and subdirectories in the specified path unless they already exist.
Definition Directory.cpp:64
static bool Exists(const String &sPath)
Determines whether the given path refers to an existing directory on disk.
Definition Directory.cpp:56
static String GetFullPath(const String &sPath)
Returns the absolute path for the specified path string.
Definition Path.cpp:131
static String GetDirectoryName(const String &sFilePath)
Returns the directory information for the specified path string.
Definition Path.cpp:92
A unified smart pointer that supports both unique and shared ownership semantics.
static SmartPointer< T > NewShared()
Creates a Shared SmartPointer, default constructing T.
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
Definition String.h:74
LogLevel
Defines logging severity levels.
Definition ILogger.h:15
static DotNetDupe::System::String ResolveAndPrepareLogPath(const DotNetDupe::System::String &rawFilePath)
Encapsulates logging system configuration options.
DotNetDupe::System::String FilePath
Destination file path when file logging is configured.