forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileLogger.cpp
More file actions
59 lines (50 loc) · 1.76 KB
/
Copy pathFileLogger.cpp
File metadata and controls
59 lines (50 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "Public/AppInstallerFileLogger.h"
#include "Public/AppInstallerRuntime.h"
#include "Public/AppInstallerDateTime.h"
#define AICLI_FILELOGGER_DEFAULT_FILE_PREFIX "WinGet-"
#define AICLI_FILELOGGER_DEFAULT_FILE_EXT ".log"
namespace AppInstaller::Logging
{
FileLogger::FileLogger(const std::filesystem::path& filePath)
{
if (filePath.empty())
{
m_name = "file";
m_filePath = Runtime::GetPathToDefaultLogLocation();
m_filePath /= AICLI_FILELOGGER_DEFAULT_FILE_PREFIX + Utility::GetCurrentTimeForFilename() + AICLI_FILELOGGER_DEFAULT_FILE_EXT;
}
else
{
m_name = GetNameForPath(filePath);
m_filePath = filePath;
}
m_stream.open(m_filePath);
}
FileLogger::~FileLogger()
{
m_stream.flush();
}
std::string FileLogger::GetNameForPath(const std::filesystem::path& filePath)
{
using namespace std::string_literals;
return "file :: "s + filePath.u8string();
}
std::string FileLogger::GetName() const
{
return m_name;
}
void FileLogger::Write(Channel channel, Level, std::string_view message) noexcept try
{
// Send to a string first to create a single block to write to a file.
std::stringstream strstr;
strstr << std::chrono::system_clock::now() << " [" << std::setw(GetMaxChannelNameLength()) << std::left << std::setfill(' ') << GetChannelName(channel) << "] " << message;
m_stream << strstr.str() << std::endl;
}
catch (...)
{
// Just eat any exceptions here; better than losing logs
}
}