snapshot logger

This commit is contained in:
seraphima
2024-06-12 22:00:11 +02:00
parent 6bfe924234
commit a5c21d3432
4 changed files with 25 additions and 9 deletions

View File

@@ -71,6 +71,8 @@ struct LogSettings
inline const static std::string cmdNotFoundLoggerName = "cmd-not-found"; inline const static std::string cmdNotFoundLoggerName = "cmd-not-found";
inline const static std::string projectsLauncherLoggerName = "projects-launcher"; inline const static std::string projectsLauncherLoggerName = "projects-launcher";
inline const static std::wstring projectsLauncherLogPath = L"projects-launcher-log.txt"; inline const static std::wstring projectsLauncherLogPath = L"projects-launcher-log.txt";
inline const static std::string projectsSnapshotToolLoggerName = "projects-snapshot-tool";
inline const static std::wstring projectsSnapshotToolLogPath = L"projects-snapshot-tool-log.txt";
inline const static int retention = 30; inline const static int retention = 30;
std::wstring logLevel; std::wstring logLevel;
LogSettings(); LogSettings();

View File

@@ -101,7 +101,7 @@
<Link> <Link>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>shcore.lib;Shell32.lib;propsys.lib;%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>shcore.lib;Shell32.lib;propsys.lib;DbgHelp.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)'=='Release'"> <ItemDefinitionGroup Condition="'$(Configuration)'=='Release'">
@@ -119,7 +119,7 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding> <EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences> <OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>shcore.lib;Shell32.lib;propsys.lib;%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>shcore.lib;Shell32.lib;propsys.lib;DbgHelp.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>

View File

@@ -11,23 +11,31 @@
#include <MonitorUtils.h> #include <MonitorUtils.h>
int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, PSTR cmdline, int cmdshow) #include <common/utils/logger_helper.h>
#include <common/utils/UnhandledExceptionHandler.h>
const std::wstring moduleName = L"Projects\\ProjectsSnapshotTool";
const std::wstring internalPath = L"";
int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR cmdLine, int cmdshow)
{ {
LoggerHelpers::init_logger(moduleName, internalPath, LogSettings::projectsLauncherLoggerName);
InitUnhandledExceptionHandler();
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
HRESULT comInitHres = CoInitializeEx(0, COINIT_MULTITHREADED); HRESULT comInitHres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(comInitHres)) if (FAILED(comInitHres))
{ {
std::wcout << L"Failed to initialize COM library. " << comInitHres << std::endl; Logger::error(L"Failed to initialize COM library. {}", comInitHres);
return -1; return -1;
} }
std::wstring fileName = JsonUtils::ProjectsFile(); std::wstring fileName = JsonUtils::ProjectsFile();
int len = MultiByteToWideChar(CP_ACP, 0, cmdline, -1, NULL, 0); std::string cmdLineStr(cmdLine);
if (len > 1) if (!cmdLineStr.empty())
{ {
std::wstring fileNameParam(len, L'\0'); std::wstring fileNameParam(cmdLineStr.begin(), cmdLineStr.end());
MultiByteToWideChar(CP_ACP, 0, cmdline, -1, &fileNameParam[0], len);
fileName = fileNameParam; fileName = fileNameParam;
} }
@@ -45,8 +53,9 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, PSTR cmdline, int cmd
} }
} }
} }
catch (std::exception) catch (std::exception ex)
{ {
Logger::error("Error reading projects file. {}", ex.what());
} }
// new project name // new project name
@@ -72,6 +81,7 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, PSTR cmdline, int cmd
std::wstring projectName = defaultNamePrefix + L" " + std::to_wstring(nextProjectIndex + 1); std::wstring projectName = defaultNamePrefix + L" " + std::to_wstring(nextProjectIndex + 1);
time_t creationTime = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); time_t creationTime = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
Project project{ .id = CreateGuidString(), .name = projectName, .creationTime = creationTime }; Project project{ .id = CreateGuidString(), .name = projectName, .creationTime = creationTime };
Logger::trace(L"Creating project {}:{}", project.name, project.id);
// save monitor configuration // save monitor configuration
project.monitors = MonitorUtils::IdentifyMonitors(); project.monitors = MonitorUtils::IdentifyMonitors();
@@ -144,5 +154,6 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, PSTR cmdline, int cmd
projects.push_back(project); projects.push_back(project);
json::to_file(fileName, JsonUtils::ProjectsListJSON::ToJson(projects)); json::to_file(fileName, JsonUtils::ProjectsListJSON::ToJson(projects));
Logger::trace(L"Project {}:{} created", project.name, project.id);
return 0; return 0;
} }

View File

@@ -1,3 +1,6 @@
#pragma once #pragma once
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <common/logger/logger.h>
#include <winrt/Windows.Foundation.h> #include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h> #include <winrt/Windows.Foundation.Collections.h>