Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathLoggingHelper.cpp
More file actions
Latest commit
143 lines (114 loc) · 3.36 KB
/
Copy pathLoggingHelper.cpp
File metadata and controls
143 lines (114 loc) · 3.36 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include"pch.h"
#include"LoggingHelper.h"
#include<fstream>
usingnamespaceNppShell::Helpers;
LoggingHelper::LoggingHelper()
{
appDataLoggingEnabled = IsAppDataLoggingEnabled();
if (!appDataLoggingEnabled)
{
return;
}
const wstring logFileFolder = CreateAppDataFolder();
logFilePath = logFileFolder + L"\\NppShell.log";
}
voidLoggingHelper::LogMessage(const wstring& source, const wstring& message)
{
if (!appDataLoggingEnabled)
{
return;
}
wofstream file(logFilePath, ios_base::app);
if (file.is_open())
{
file << GetTimestamp();
file << L" - ";
file << source;
file << L": ";
file << message;
file << endl;
file.close();
}
}
wstring LoggingHelper::GetRoamingAppDataFolderPath()
{
// Initialize COM
CoInitialize(NULL);
// Create an instance of the KnownFolderManager interface
IKnownFolderManager* pManager;
HRESULT hr = CoCreateInstance(CLSID_KnownFolderManager, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pManager));
if (FAILED(hr))
{
// Handle error
returnL"";
}
// Get the Roaming App Data folder
IKnownFolder* pFolder;
hr = pManager->GetFolder(FOLDERID_RoamingAppData, &pFolder);
if (FAILED(hr))
{
// Handle error
pManager->Release();
returnL"";
}
// Get the path to the Roaming App Data folder
PWSTR pszPath = nullptr;
hr = pFolder->GetPath(KF_FLAG_DEFAULT, &pszPath);
if (FAILED(hr))
{
// Handle error
pFolder->Release();
pManager->Release();
returnL"";
}
// Convert the path to a wstring
wstring path(pszPath);
// Free the allocated memory
CoTaskMemFree(pszPath);
// Release interfaces
pFolder->Release();
pManager->Release();
// Uninitialize COM
CoUninitialize();
// Return the path to the Roaming App Data folder
return path;
}
wstring LoggingHelper::CreateAppDataFolder()
{
wstring folderPath = GetRoamingAppDataFolderPath() + L"\\NppShell";
if (!CreateDirectoryW(folderPath.c_str(), NULL) && GetLastError() != ERROR_ALREADY_EXISTS)
{
throwruntime_error("Failed to create folder");
}
return folderPath;
}
wstring LoggingHelper::GetTimestamp()
{
SYSTEMTIME st;
GetLocalTime(&st);
wchar_t buffer[25];
swprintf_s(buffer, L"%04d-%02d-%02d %02d:%02d:%02d", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
returnwstring(buffer);
}
boolLoggingHelper::IsAppDataLoggingEnabled()
{
const wstring environmentVariableName = L"NPPSHELL_LOGGING_ENABLED";
// Get the size of the buffer required to hold the environment variable value
DWORD size = GetEnvironmentVariableW(environmentVariableName.c_str(), nullptr, 0);
if (size == 0)
{
// The specified environment variable was not found
returnfalse;
}
// Allocate a buffer to hold the environment variable value
wstring buffer(size, L'\0');
// Retrieve the environment variable value
DWORD result = GetEnvironmentVariableW(environmentVariableName.c_str(), buffer.data(), size);
if (result == 0 || result > size)
{
// An error occurred while retrieving the environment variable value
returnfalse;
}
// Return if the value is set to true
returnwstring(buffer.data()) == L"true";
}