Skip to content

Commit 71f49c1

Browse files
GurliGebisdonho
authored andcommitted
Add logger
Close #41
1 parent b545deb commit 71f49c1

13 files changed

+255
-5
lines changed

ClassicEditWithNppExplorerCommandHandler.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
#include "pch.h"
22
#include "ClassicEditWithNppExplorerCommandHandler.h"
33

4+
#include "LoggingHelper.h"
45
#include "SharedState.h"
56

67
using namespace NppShell::CommandHandlers;
8+
using namespace NppShell::Helpers;
9+
10+
extern LoggingHelper g_loggingHelper;
11+
12+
ClassicEditWithNppExplorerCommandHandler::ClassicEditWithNppExplorerCommandHandler()
13+
{
14+
g_loggingHelper.LogMessage(L"ClassicEditWithNppExplorerCommandHandler::ctor", L"Creating object");
15+
}
16+
17+
ClassicEditWithNppExplorerCommandHandler::~ClassicEditWithNppExplorerCommandHandler()
18+
{
19+
g_loggingHelper.LogMessage(L"ClassicEditWithNppExplorerCommandHandler::~tor", L"Destroying object");
20+
}
721

822
const EXPCMDSTATE ClassicEditWithNppExplorerCommandHandler::State(IShellItemArray* psiItemArray)
923
{
@@ -13,6 +27,8 @@ const EXPCMDSTATE ClassicEditWithNppExplorerCommandHandler::State(IShellItemArra
1327
CounterState currentState = state->GetState();
1428
state->SetState(NotSet);
1529

30+
g_loggingHelper.LogMessage(L"ClassicEditWithNppExplorerCommandHandler::State", L"Current state: " + std::to_wstring(currentState));
31+
1632
// If it is set, it means the State function has been called in the Modern command handler last, which means we should hide this one.
1733
if (currentState == CounterState::Set)
1834
{

ClassicEditWithNppExplorerCommandHandler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ namespace NppShell::CommandHandlers
1212
#endif
1313
{
1414
public:
15+
ClassicEditWithNppExplorerCommandHandler();
16+
~ClassicEditWithNppExplorerCommandHandler();
17+
1518
const EXPCMDSTATE State(IShellItemArray* psiItemArray);
1619
};
1720
}

LoggingHelper.cpp

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#include "pch.h"
2+
#include "LoggingHelper.h"
3+
4+
#include <fstream>
5+
6+
using namespace NppShell::Helpers;
7+
8+
LoggingHelper::LoggingHelper()
9+
{
10+
appDataLoggingEnabled = IsAppDataLoggingEnabled();
11+
12+
if (!appDataLoggingEnabled)
13+
{
14+
return;
15+
}
16+
17+
const wstring logFileFolder = CreateAppDataFolder();
18+
logFilePath = logFileFolder + L"\\NppShell.log";
19+
}
20+
21+
void LoggingHelper::LogMessage(const wstring& source, const wstring& message)
22+
{
23+
if (!appDataLoggingEnabled)
24+
{
25+
return;
26+
}
27+
28+
wofstream file(logFilePath, ios_base::app);
29+
if (file.is_open())
30+
{
31+
file << GetTimestamp();
32+
file << L" - ";
33+
file << source;
34+
file << L": ";
35+
file << message;
36+
file << endl;
37+
file.close();
38+
}
39+
}
40+
41+
wstring LoggingHelper::GetRoamingAppDataFolderPath()
42+
{
43+
// Initialize COM
44+
CoInitialize(NULL);
45+
46+
// Create an instance of the KnownFolderManager interface
47+
IKnownFolderManager* pManager;
48+
HRESULT hr = CoCreateInstance(CLSID_KnownFolderManager, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pManager));
49+
50+
if (FAILED(hr))
51+
{
52+
// Handle error
53+
return L"";
54+
}
55+
56+
// Get the Roaming App Data folder
57+
IKnownFolder* pFolder;
58+
hr = pManager->GetFolder(FOLDERID_RoamingAppData, &pFolder);
59+
if (FAILED(hr))
60+
{
61+
// Handle error
62+
pManager->Release();
63+
return L"";
64+
}
65+
66+
// Get the path to the Roaming App Data folder
67+
PWSTR pszPath = nullptr;
68+
hr = pFolder->GetPath(KF_FLAG_DEFAULT, &pszPath);
69+
if (FAILED(hr))
70+
{
71+
// Handle error
72+
pFolder->Release();
73+
pManager->Release();
74+
75+
return L"";
76+
}
77+
78+
// Convert the path to a wstring
79+
wstring path(pszPath);
80+
81+
// Free the allocated memory
82+
CoTaskMemFree(pszPath);
83+
84+
// Release interfaces
85+
pFolder->Release();
86+
pManager->Release();
87+
88+
// Uninitialize COM
89+
CoUninitialize();
90+
91+
// Return the path to the Roaming App Data folder
92+
return path;
93+
}
94+
95+
wstring LoggingHelper::CreateAppDataFolder()
96+
{
97+
wstring folderPath = GetRoamingAppDataFolderPath() + L"\\NppShell";
98+
99+
if (!CreateDirectoryW(folderPath.c_str(), NULL) && GetLastError() != ERROR_ALREADY_EXISTS)
100+
{
101+
throw runtime_error("Failed to create folder");
102+
}
103+
104+
return folderPath;
105+
}
106+
107+
wstring LoggingHelper::GetTimestamp()
108+
{
109+
SYSTEMTIME st;
110+
GetLocalTime(&st);
111+
112+
wchar_t buffer[25];
113+
swprintf_s(buffer, L"%04d-%02d-%02d %02d:%02d:%02d", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
114+
115+
return wstring(buffer);
116+
}
117+
118+
bool LoggingHelper::IsAppDataLoggingEnabled()
119+
{
120+
const wstring environmentVariableName = L"NPPSHELL_LOGGING_ENABLED";
121+
122+
// Get the size of the buffer required to hold the environment variable value
123+
DWORD size = GetEnvironmentVariableW(environmentVariableName.c_str(), nullptr, 0);
124+
if (size == 0)
125+
{
126+
// The specified environment variable was not found
127+
return false;
128+
}
129+
130+
// Allocate a buffer to hold the environment variable value
131+
wstring buffer(size, L'\0');
132+
133+
// Retrieve the environment variable value
134+
DWORD result = GetEnvironmentVariableW(environmentVariableName.c_str(), buffer.data(), size);
135+
if (result == 0 || result > size)
136+
{
137+
// An error occurred while retrieving the environment variable value
138+
return false;
139+
}
140+
141+
// Return if the value is set to true
142+
return wstring(buffer.data()) == L"true";
143+
}

LoggingHelper.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#pragma once
2+
3+
namespace NppShell::Helpers
4+
{
5+
class LoggingHelper
6+
{
7+
public:
8+
LoggingHelper();
9+
void LogMessage(const wstring& source, const wstring& message);
10+
11+
private:
12+
wstring GetRoamingAppDataFolderPath();
13+
wstring CreateAppDataFolder();
14+
wstring GetTimestamp();
15+
bool IsAppDataLoggingEnabled();
16+
17+
bool appDataLoggingEnabled;
18+
wstring logFilePath;
19+
};
20+
}

ModernEditWithNppExplorerCommandHandler.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,30 @@
22
#include "ModernEditWithNppExplorerCommandHandler.h"
33

44
#include "SharedState.h"
5+
#include "LoggingHelper.h"
56

67
using namespace NppShell::CommandHandlers;
78
using namespace NppShell::Helpers;
89

10+
extern LoggingHelper g_loggingHelper;
11+
12+
ModernEditWithNppExplorerCommandHandler::ModernEditWithNppExplorerCommandHandler()
13+
{
14+
g_loggingHelper.LogMessage(L"ModernEditWithNppExplorerCommandHandler::ctor", L"Creating object");
15+
}
16+
17+
ModernEditWithNppExplorerCommandHandler::~ModernEditWithNppExplorerCommandHandler()
18+
{
19+
g_loggingHelper.LogMessage(L"ModernEditWithNppExplorerCommandHandler::~tor", L"Destroying object");
20+
}
21+
922
const EXPCMDSTATE ModernEditWithNppExplorerCommandHandler::State(IShellItemArray* psiItemArray)
1023
{
1124
UNREFERENCED_PARAMETER(psiItemArray);
1225

1326
state->SetState(Set);
1427

28+
g_loggingHelper.LogMessage(L"ModernEditWithNppExplorerCommandHandler::State", L"Current state: " + std::to_wstring(state->GetState()));
29+
1530
return ECS_ENABLED;
1631
}

ModernEditWithNppExplorerCommandHandler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ namespace NppShell::CommandHandlers
88
class __declspec(uuid("E6950302-61F0-4FEB-97DB-855E30D4A991")) ModernEditWithNppExplorerCommandHandler : public BaseNppExplorerCommandHandler
99
{
1010
public:
11+
ModernEditWithNppExplorerCommandHandler();
12+
~ModernEditWithNppExplorerCommandHandler();
13+
1114
const EXPCMDSTATE State(IShellItemArray* psiItemArray);
1215
};
1316
}

NppShell.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@
267267
<ClInclude Include="ClassicEditWithNppExplorerCommandHandler.h" />
268268
<ClInclude Include="ExplorerCommandBase.h" />
269269
<ClInclude Include="framework.h" />
270+
<ClInclude Include="LoggingHelper.h" />
270271
<ClInclude Include="ModernEditWithNppExplorerCommandHandler.h" />
271272
<ClInclude Include="PathHelper.h" />
272273
<ClInclude Include="Installer.h" />
@@ -284,6 +285,7 @@
284285
<ClCompile Include="ClassicEditWithNppExplorerCommandHandler.cpp" />
285286
<ClCompile Include="dllmain.cpp" />
286287
<ClCompile Include="ExplorerCommandBase.cpp" />
288+
<ClCompile Include="LoggingHelper.cpp" />
287289
<ClCompile Include="ModernEditWithNppExplorerCommandHandler.cpp" />
288290
<ClCompile Include="PathHelper.cpp" />
289291
<ClCompile Include="Installer.cpp" />

NppShell.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@
6868
<ClInclude Include="SharedCounter.h">
6969
<Filter>Helpers</Filter>
7070
</ClInclude>
71+
<ClInclude Include="LoggingHelper.h">
72+
<Filter>Helpers</Filter>
73+
</ClInclude>
7174
</ItemGroup>
7275
<ItemGroup>
7376
<ClCompile Include="Installer.cpp">
@@ -81,6 +84,9 @@
8184
<ClCompile Include="AclHelper.cpp">
8285
<Filter>Helpers</Filter>
8386
</ClCompile>
87+
<ClCompile Include="LoggingHelper.cpp">
88+
<Filter>Helpers</Filter>
89+
</ClCompile>
8490
<ClCompile Include="ExplorerCommandBase.cpp">
8591
<Filter>CommandHandlers\Base</Filter>
8692
</ClCompile>

RegistryKey.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
#include "pch.h"
22
#include "RegistryKey.h"
3+
#include "LoggingHelper.h"
34

45
using namespace NppShell::Registry;
6+
using namespace NppShell::Helpers;
7+
8+
extern LoggingHelper g_loggingHelper;
59

610
RegistryKey::RegistryKey(HKEY hKey, const wstring& subKey, REGSAM access, bool createIfMissing)
711
: m_hKey(nullptr), m_regsam(access), m_originalHKey(hKey), m_originalSubKey(subKey)
812
{
913
if (RegOpenKeyExW(hKey, subKey.data(), 0, access, &m_hKey) == ERROR_SUCCESS)
1014
{
15+
g_loggingHelper.LogMessage(L"RegistryKey::ctor", L"Opened sub key: " + subKey);
1116
return;
1217
}
1318

@@ -20,8 +25,13 @@ RegistryKey::RegistryKey(HKEY hKey, const wstring& subKey, REGSAM access, bool c
2025

2126
if (RegCreateKeyExW(hKey, subKey.data(), 0, nullptr, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, nullptr, &m_hKey, &disposition) != ERROR_SUCCESS)
2227
{
28+
g_loggingHelper.LogMessage(L"RegistryKey::ctor", L"Failed to create sub key: " + subKey);
2329
throw runtime_error("Failed to create registry key.");
2430
}
31+
else
32+
{
33+
g_loggingHelper.LogMessage(L"RegistryKey::ctor", L"Created sub key: " + subKey);
34+
}
2535
}
2636

2737
RegistryKey::~RegistryKey()
@@ -102,6 +112,7 @@ DWORD RegistryKey::GetDwordValue(const wstring& valueName)
102112

103113
if (RegGetValueW(m_hKey, nullptr, valueName.empty() ? NULL : valueName.data(), RRF_RT_REG_DWORD, nullptr, &value, &dataSize) != ERROR_SUCCESS)
104114
{
115+
g_loggingHelper.LogMessage(L"RegistryKey::GetDwordValue", L"Failed to get DWORD value: " + valueName);
105116
throw runtime_error("Failed to get registry value.");
106117
}
107118

@@ -119,13 +130,15 @@ wstring RegistryKey::GetStringValue(const wstring& valueName)
119130

120131
if (RegGetValueW(m_hKey, nullptr, valueName.empty() ? NULL : valueName.data(), RRF_RT_REG_SZ, nullptr, nullptr, &dataSize) != ERROR_SUCCESS)
121132
{
133+
g_loggingHelper.LogMessage(L"RegistryKey::GetStringValue", L"Failed to get REG_SZ value: " + valueName);
122134
throw runtime_error("Failed to get registry value size.");
123135
}
124136

125137
wstring value(dataSize / sizeof(wchar_t), L'\0');
126138

127139
if (RegGetValueW(m_hKey, nullptr, valueName.empty() ? NULL : valueName.data(), RRF_RT_REG_SZ, nullptr, bit_cast<BYTE*>(value.data()), &dataSize) != ERROR_SUCCESS)
128140
{
141+
g_loggingHelper.LogMessage(L"RegistryKey::GetStringValue", L"Failed to get REG_SZ value: " + valueName);
129142
throw runtime_error("Failed to get registry value.");
130143
}
131144

@@ -141,8 +154,12 @@ void RegistryKey::SetDwordValue(const wstring& valueName, DWORD value)
141154

142155
if (RegSetValueExW(m_hKey, valueName.empty() ? NULL : valueName.data(), 0, REG_DWORD, bit_cast<const BYTE*>(&value), sizeof(DWORD)) != ERROR_SUCCESS)
143156
{
157+
g_loggingHelper.LogMessage(L"RegistryKey::SetDwordValue", L"Failed to set DWORD value: " + valueName);
144158
throw runtime_error("Failed to set registry value.");
145159
}
160+
161+
wstring valueNameLog = valueName.empty() ? L"(default)" : valueName.data();
162+
g_loggingHelper.LogMessage(L"RegistryKey::SetDwordValue", L"Setting DWORD name: " + valueNameLog + L" to: " + to_wstring(value));
146163
}
147164

148165
void RegistryKey::SetStringValue(const wstring& valueName, const wstring& value)
@@ -154,8 +171,12 @@ void RegistryKey::SetStringValue(const wstring& valueName, const wstring& value)
154171

155172
if (RegSetValueExW(m_hKey, valueName.empty() ? NULL : valueName.data(), 0, REG_SZ, bit_cast<const BYTE*>(value.data()), static_cast<DWORD>((value.length() + 1) * sizeof(wchar_t))) != ERROR_SUCCESS)
156173
{
174+
g_loggingHelper.LogMessage(L"RegistryKey::SetStringValue", L"Failed to set REG_SZ value: " + valueName);
157175
throw runtime_error("Failed to set registry value.");
158176
}
177+
178+
wstring valueNameLog = valueName.empty() ? L"(default)" : valueName.data();
179+
g_loggingHelper.LogMessage(L"RegistryKey::SetStringValue", L"Setting REG_SZ name: " + valueNameLog + L" to: \"" + value + L"\"");
159180
}
160181

161182
void RegistryKey::DeleteKey()
@@ -167,9 +188,12 @@ void RegistryKey::DeleteKey()
167188

168189
if (RegDeleteTreeW(m_originalHKey, m_originalSubKey.data()) != ERROR_SUCCESS)
169190
{
191+
g_loggingHelper.LogMessage(L"RegistryKey::DeleteKey", L"Failed to delete subkey: " + m_originalSubKey);
170192
throw runtime_error("Failed to delete registry key.");
171193
}
172194

195+
g_loggingHelper.LogMessage(L"RegistryKey::DeleteKey", L"Deleted subkey: " + m_originalSubKey);
196+
173197
m_hKey = nullptr;
174198
m_originalHKey = nullptr;
175199
}

0 commit comments

Comments
 (0)