DotNetDupe
4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Toggle main menu visibility
Loading...
Searching...
No Matches
FileLoggerProvider.cpp
Go to the documentation of this file.
1
#include "
pch.h
"
2
#include "
Extensions/Logging/FileLoggerProvider.h
"
3
#include "
Extensions/Logging/FileLogger.h
"
4
#include "
Extensions/Logging/LogManager.h
"
5
#include "
System/IO/Path.h
"
6
#include "
System/IO/Directory.h
"
7
#include "
System/IOException.h
"
8
#include "
FileLoggerContext.h
"
9
10
namespace
DotNetDupe
{
11
namespace
Extensions
{
12
namespace
Logging
{
13
14
FileLoggerProvider::FileLoggerProvider
()
15
:
FileLoggerProvider
(
LogManager
::GetConfiguration()) {
17
}
18
19
static
DotNetDupe::System::String
ResolveAndPrepareLogPath
(
const
DotNetDupe::System::String
& rawFilePath) {
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);
26
DotNetDupe::System::String
sParentDir =
Path::GetDirectoryName
(sFullPath);
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;
39
DotNetDupe::System::SmartPointer<FileLoggerContext>
pContext;
40
};
41
42
FileLoggerProvider::FileLoggerProvider
(
const
LoggerConfiguration
& config)
43
: m_pImpl(
DotNetDupe
::
System
::
SmartPointer
<Impl>::NewShared()) {
45
m_pImpl->config = config;
46
m_pImpl->config.
FilePath
=
ResolveAndPrepareLogPath
(config.
FilePath
);
47
49
m_pImpl->pContext =
DotNetDupe::System::SmartPointer<FileLoggerContext>::NewShared
();
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
54
FileLoggerProvider::FileLoggerProvider
(
const
DotNetDupe::System::String
& filePath,
bool
isJsonFormat,
LogLevel
minLevel)
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
62
m_pImpl->pContext =
DotNetDupe::System::SmartPointer<FileLoggerContext>::NewShared
();
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
67
FileLoggerProvider::~FileLoggerProvider
() {
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
77
const
DotNetDupe::System::String
&
FileLoggerProvider::GetFilePath
()
const
{
79
return
m_pImpl->config.FilePath;
80
}
81
82
DotNetDupe::System::SmartPointer<ILogger>
FileLoggerProvider::CreateLogger
(
const
DotNetDupe::System::String
& categoryName) {
84
auto
pLogger =
DotNetDupe::System::SmartPointer<FileLogger>::NewShared
(categoryName, m_pImpl->config, m_pImpl->pContext);
85
return
DotNetDupe::System::SmartPointer<ILogger>
(pLogger);
86
}
87
88
}
89
}
90
}
Directory.h
FileLogger.h
FileLoggerContext.h
FileLoggerProvider.h
IOException.h
The exception that is thrown when an I/O error occurs.
LogManager.h
Path.h
DotNetDupe::Extensions::Logging::FileLoggerProvider::GetFilePath
const DotNetDupe::System::String & GetFilePath() const
Gets the configured destination file path.
Definition
FileLoggerProvider.cpp:77
DotNetDupe::Extensions::Logging::FileLoggerProvider::FileLoggerProvider
FileLoggerProvider()
Initializes a new FileLoggerProvider using default configuration.
Definition
FileLoggerProvider.cpp:14
DotNetDupe::Extensions::Logging::FileLoggerProvider::CreateLogger
DotNetDupe::System::SmartPointer< ILogger > CreateLogger(const DotNetDupe::System::String &categoryName) override
Creates a FileLogger instance for the specified category.
Definition
FileLoggerProvider.cpp:82
DotNetDupe::Extensions::Logging::FileLoggerProvider::~FileLoggerProvider
~FileLoggerProvider() override
Destructor releasing file provider resources.
Definition
FileLoggerProvider.cpp:67
DotNetDupe::Extensions::Logging::LogManager
Central thread-safe static registry and entry point for application logging.
Definition
LogManager.h:18
DotNetDupe::System::IO::Directory::CreateDirectory
static void CreateDirectory(const String &sPath)
Creates all directories and subdirectories in the specified path unless they already exist.
Definition
Directory.cpp:64
DotNetDupe::System::IO::Directory::Exists
static bool Exists(const String &sPath)
Determines whether the given path refers to an existing directory on disk.
Definition
Directory.cpp:56
DotNetDupe::System::IO::Path::GetFullPath
static String GetFullPath(const String &sPath)
Returns the absolute path for the specified path string.
Definition
Path.cpp:131
DotNetDupe::System::IO::Path::GetDirectoryName
static String GetDirectoryName(const String &sFilePath)
Returns the directory information for the specified path string.
Definition
Path.cpp:92
DotNetDupe::System::SmartPointer
A unified smart pointer that supports both unique and shared ownership semantics.
Definition
SmartPointer.h:72
DotNetDupe::System::SmartPointer::NewShared
static SmartPointer< T > NewShared()
Creates a Shared SmartPointer, default constructing T.
Definition
SmartPointer.h:277
DotNetDupe::System::String
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
Definition
String.h:74
DotNetDupe::System::String::IsEmpty
bool IsEmpty() const
Definition
String.cpp:398
DotNetDupe::Extensions::Logging
Definition
AggregateLogger.h:8
DotNetDupe::Extensions::Logging::LogLevel
LogLevel
Defines logging severity levels.
Definition
ILogger.h:15
DotNetDupe::Extensions::Logging::ResolveAndPrepareLogPath
static DotNetDupe::System::String ResolveAndPrepareLogPath(const DotNetDupe::System::String &rawFilePath)
Definition
FileLoggerProvider.cpp:19
DotNetDupe::Extensions
Definition
IServiceCollection.h:8
DotNetDupe::System::IO
Definition
Console.h:17
DotNetDupe::System
Definition
Action.h:11
DotNetDupe
Definition
IServiceCollection.h:7
pch.h
DotNetDupe::Extensions::Logging::LoggerConfiguration
Encapsulates logging system configuration options.
Definition
LoggerConfiguration.h:26
DotNetDupe::Extensions::Logging::LoggerConfiguration::FilePath
DotNetDupe::System::String FilePath
Destination file path when file logging is configured.
Definition
LoggerConfiguration.h:39
DotNetDupe
FileLoggerProvider.cpp
Generated by
1.18.0