mirror of
https://github.com/WinDurango/WinDurango.git
synced 2026-01-31 00:55:17 +01:00
feat: added logging and config
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -27,4 +27,4 @@ ZERO_CHECK.dir
|
||||
/cmake-build-debug
|
||||
/out
|
||||
.suo
|
||||
projects/WinDurango/WinDurango.Testing/.vs
|
||||
projects/WinDurango.Testing/.vs
|
||||
@@ -25,3 +25,28 @@ add_custom_target(WinDurango ALL DEPENDS
|
||||
WinDurango.KernelX
|
||||
WinDurango.D3D11X
|
||||
)
|
||||
|
||||
file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/bin/AppX/")
|
||||
add_custom_command(
|
||||
TARGET WinDurango POST_BUILD
|
||||
COMMAND copy /Y "*.dll" "AppX"
|
||||
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/bin/Debug/"
|
||||
)
|
||||
|
||||
add_custom_command(
|
||||
TARGET WinDurango POST_BUILD
|
||||
COMMAND copy /Y "*.pdb" "AppX"
|
||||
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/bin/Debug/"
|
||||
)
|
||||
|
||||
add_custom_command(
|
||||
TARGET WinDurango POST_BUILD
|
||||
COMMAND copy /Y "*.lib" "AppX"
|
||||
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/bin/Debug/"
|
||||
)
|
||||
|
||||
add_custom_command(
|
||||
TARGET WinDurango POST_BUILD
|
||||
COMMAND copy /Y "*.exp" "AppX"
|
||||
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/bin/Debug/"
|
||||
)
|
||||
@@ -10,12 +10,13 @@ add_compile_definitions(WD_API_EXPORTS)
|
||||
|
||||
set(FILES
|
||||
include/WinDurango.Common/WinDurango.h
|
||||
src/WinDurango.cpp
|
||||
|
||||
include/WinDurango.Common/Config.h
|
||||
include/WinDurango.Common/Logging.h
|
||||
include/WinDurango.Common/Interfaces/Storage/Directory.h
|
||||
include/WinDurango.Common/Interfaces/Storage/File.h
|
||||
src/WinDurango.cpp
|
||||
src/Config.cpp
|
||||
src/Logging.cpp
|
||||
)
|
||||
|
||||
FetchContent_Declare(json URL https://github.com/nlohmann/json/releases/download/v3.12.0/json.tar.xz)
|
||||
|
||||
@@ -20,6 +20,7 @@ namespace wd::common::interfaces::storage {
|
||||
virtual bool close() = 0;
|
||||
|
||||
virtual std::filesystem::path filepath() = 0;
|
||||
virtual std::filesystem::path fullfilepath() = 0;
|
||||
|
||||
virtual bool rename(std::string) = 0;
|
||||
virtual bool remove() = 0;
|
||||
|
||||
@@ -1,4 +1,99 @@
|
||||
/*
|
||||
* Logging File
|
||||
* WinDurango.Common::Logging
|
||||
*/
|
||||
*/
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <spdlog/sinks/stdout_color_sinks.h>
|
||||
#include <spdlog/sinks/basic_file_sink.h>
|
||||
#include <iostream>
|
||||
#include "WinDurango.Common/Interfaces/Storage/File.h"
|
||||
#include "WinDurango.Common/exports.h"
|
||||
|
||||
namespace wd::common {
|
||||
class WD_API Logging {
|
||||
public:
|
||||
Logging() : pFile(nullptr), isConstructed(false) {}
|
||||
Logging(std::shared_ptr<interfaces::storage::File> file) : pFile(file), isConstructed(true) {}
|
||||
|
||||
void Initialize();
|
||||
|
||||
void AddLogger(std::string codespace);
|
||||
|
||||
/*
|
||||
* Example:
|
||||
* Log("WinDurango::Common::Storage", "Failed to do smth - Code %s", 5);
|
||||
* >> [WinDurango::Common::Storage] 11:25AM 26/12/2025 - Failed to do smth - Code 5
|
||||
*/
|
||||
template<typename... Args>
|
||||
void Log(std::string codespace, Args&&... args) {
|
||||
if (!isInitialized) {
|
||||
std::cout << "[WinDurango::Common::Logging.Initialize] - Critical: Logging isn't Initiailized\n";
|
||||
return;
|
||||
}
|
||||
try {
|
||||
if (log.find(codespace) != log.end()) {
|
||||
log[codespace]->info(std::forward<Args>(args)...);
|
||||
} else {
|
||||
AddLogger(codespace);
|
||||
log[codespace]->info(std::forward<Args>(args)...);
|
||||
}
|
||||
} catch (const spdlog::spdlog_ex& e) {
|
||||
std::cout << "[WinDurango::Common::Logging.spdlog] - Critical: " << e.what() << "\n";
|
||||
} catch (const std::exception& e) {
|
||||
std::cout << "[WinDurango::Common::Logging.exception] - Critical: " << e.what() << "\n";
|
||||
} catch (...) {
|
||||
std::cout << "[WinDurango::Common::Logging.(...))] - Critical: Unknown Error\n";
|
||||
}
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
void Warn(std::string codespace, Args&&... args) {
|
||||
if (!isInitialized) {
|
||||
std::cout << "[WinDurango::Common::Logging.Initialize] - Critical: Logging isn't Initiailized\n";
|
||||
return;
|
||||
}
|
||||
try {
|
||||
if (log.find(codespace) != log.end()) {
|
||||
log[codespace]->warn(std::forward<Args>(args)...);
|
||||
} else {
|
||||
AddLogger(codespace);
|
||||
log[codespace]->warn(std::forward<Args>(args)...);
|
||||
}
|
||||
} catch (const spdlog::spdlog_ex& e) {
|
||||
std::cout << "[WinDurango::Common::Logging.spdlog] - Critical: " << e.what() << "\n";
|
||||
} catch (const std::exception& e) {
|
||||
std::cout << "[WinDurango::Common::Logging.exception] - Critical: " << e.what() << "\n";
|
||||
} catch (...) {
|
||||
std::cout << "[WinDurango::Common::Logging.(...))] - Critical: Unknown Error\n";
|
||||
}
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
void Error(std::string codespace, Args&&... args) {
|
||||
if (!isInitialized) {
|
||||
std::cout << "[WinDurango::Common::Logging.Initialize] - Critical: Logging isn't Initiailized\n";
|
||||
return;
|
||||
}
|
||||
try {
|
||||
if (log.find(codespace) != log.end()) {
|
||||
log[codespace]->error(std::forward<Args>(args)...);
|
||||
} else {
|
||||
AddLogger(codespace);
|
||||
log[codespace]->error(std::forward<Args>(args)...);
|
||||
}
|
||||
} catch (const spdlog::spdlog_ex& e) {
|
||||
std::cout << "[WinDurango::Common::Logging.spdlog] - Critical: " << e.what() << "\n";
|
||||
} catch (const std::exception& e) {
|
||||
std::cout << "[WinDurango::Common::Logging.exception] - Critical: " << e.what() << "\n";
|
||||
} catch (...) {
|
||||
std::cout << "[WinDurango::Common::Logging.(...))] - Critical: Unknown Error\n";
|
||||
}
|
||||
}
|
||||
private:
|
||||
std::unordered_map<std::string, std::shared_ptr<spdlog::logger>> log;
|
||||
std::shared_ptr<interfaces::storage::File> pFile;
|
||||
public:
|
||||
bool isConstructed = false;
|
||||
bool isInitialized = false;
|
||||
};
|
||||
}
|
||||
@@ -2,25 +2,28 @@
|
||||
// Created by DexrnZacAttack on 1/23/26 using zPc-i2.
|
||||
//
|
||||
#pragma once
|
||||
|
||||
#include <ctime>
|
||||
#include "Interfaces/Storage/Directory.h"
|
||||
#include "Config.h"
|
||||
#include "Logging.h"
|
||||
#include "exports.h"
|
||||
|
||||
namespace wd::common
|
||||
{
|
||||
class WD_API WinDurango
|
||||
{
|
||||
namespace wd::common {
|
||||
class WD_API WinDurango {
|
||||
public:
|
||||
static WinDurango *GetInstance();
|
||||
static WinDurango *GetInstance();
|
||||
|
||||
WinDurango() = default;
|
||||
WinDurango() = default;
|
||||
|
||||
void Init();
|
||||
void Init(std::shared_ptr<interfaces::storage::Directory> rootDir);
|
||||
|
||||
Config Config;
|
||||
Config config;
|
||||
Logging log;
|
||||
|
||||
private:
|
||||
bool _inited = false;
|
||||
};
|
||||
bool _inited = false;
|
||||
std::shared_ptr<interfaces::storage::Directory> rootDir;
|
||||
std::shared_ptr<interfaces::storage::Directory> WinDurangoRoot;
|
||||
};
|
||||
} // namespace wd::common
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
bool wd::common::Config::parse() {
|
||||
try {
|
||||
pFile->open();
|
||||
std::string jsonData = pFile->read();
|
||||
data = nlohmann::json::parse(jsonData);
|
||||
return true;
|
||||
|
||||
54
projects/WinDurango.Common/src/Logging.cpp
Normal file
54
projects/WinDurango.Common/src/Logging.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
#include "Logging.h"
|
||||
|
||||
void wd::common::Logging::Initialize() {
|
||||
if (!isConstructed) {
|
||||
std::cout << "[WinDurango::Common::Logging.Initialize] - Critical: Logging isn't Constructed\n";
|
||||
return;
|
||||
}
|
||||
try {
|
||||
pFile->open();
|
||||
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
|
||||
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(pFile->fullfilepath().string(), true);
|
||||
|
||||
console_sink->set_pattern("[%n] [%H:%M:%S] [thread %t] - %^%l%$: %v");
|
||||
file_sink->set_pattern("[%n] [%H:%M:%S] [thread %t] - %^%l%$: %v");
|
||||
|
||||
std::shared_ptr<spdlog::logger> logg = std::make_shared<spdlog::logger>("WinDurango", spdlog::sinks_init_list{console_sink, file_sink});
|
||||
|
||||
log["WinDurango"] = logg;
|
||||
log["WinDurango"]->set_pattern("[%n] [%H:%M:%S] [thread %t] - %^%l%$: %v");
|
||||
logg->info("Setting Log File: {}", pFile->fullfilepath().string());
|
||||
isInitialized = true;
|
||||
} catch (const spdlog::spdlog_ex& e) {
|
||||
std::cout << "[WinDurango::Common::Logging.spdlog] - Critical: " << e.what() << "\n";
|
||||
} catch (const std::exception& e) {
|
||||
std::cout << "[WinDurango::Common::Logging.exception] - Critical: " << e.what() << "\n";
|
||||
} catch (...) {
|
||||
std::cout << "[WinDurango::Common::Logging.(...))] - Critical: Unknown Error\n";
|
||||
}
|
||||
}
|
||||
|
||||
void wd::common::Logging::AddLogger(std::string codespace) {
|
||||
if (!isInitialized) {
|
||||
std::cout << "[WinDurango::Common::Logging.Initialize] - Critical: Logging isn't Initiailized\n";
|
||||
return;
|
||||
}
|
||||
try {
|
||||
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
|
||||
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(pFile->fullfilepath().string(), true);
|
||||
|
||||
console_sink->set_pattern("[%n] [%H:%M:%S] [thread %t] - %^%l%$: %v");
|
||||
file_sink->set_pattern("[%n] [%H:%M:%S] [thread %t] - %^%l%$: %v");
|
||||
|
||||
std::shared_ptr<spdlog::logger> logg = std::make_shared<spdlog::logger>(codespace, spdlog::sinks_init_list{console_sink, file_sink});
|
||||
|
||||
log[codespace] = logg;
|
||||
log[codespace]->set_pattern("[%n] [%H:%M:%S] [thread %t] - %^%l%$: %v");
|
||||
} catch (const spdlog::spdlog_ex& e) {
|
||||
std::cout << "[WinDurango::Common::Logging.spdlog] - Critical: " << e.what() << "\n";
|
||||
} catch (const std::exception& e) {
|
||||
std::cout << "[WinDurango::Common::Logging.exception] - Critical: " << e.what() << "\n";
|
||||
} catch (...) {
|
||||
std::cout << "[WinDurango::Common::Logging.(...))] - Critical: Unknown Error\n";
|
||||
}
|
||||
}
|
||||
@@ -9,16 +9,38 @@ namespace wd::common
|
||||
{
|
||||
static WinDurango Instance = WinDurango(); // if we don't declare it in src, it will make multiple instances per
|
||||
// header import in different libs afaik
|
||||
if (!Instance._inited)
|
||||
Instance.Init(); // lazy
|
||||
|
||||
return &Instance;
|
||||
}
|
||||
|
||||
void WinDurango::Init()
|
||||
{
|
||||
// todo load config
|
||||
void WinDurango::Init(std::shared_ptr<interfaces::storage::Directory> rootDir) {
|
||||
try {
|
||||
rootDir->open();
|
||||
|
||||
this->_inited = true;
|
||||
std::time_t timestamp = std::time(nullptr);
|
||||
std::tm datetm = *std::localtime(×tamp);
|
||||
|
||||
char* buf;
|
||||
std::strftime(buf, sizeof(buf), "%d.%m.%Y", &datetm);
|
||||
|
||||
std::string date(buf);
|
||||
WinDurangoRoot = rootDir->CreateFolder("WinDurango");
|
||||
WinDurangoRoot->open();
|
||||
|
||||
std::shared_ptr<interfaces::storage::File> LogFile = WinDurangoRoot->CreateFile("windurango_log_" + date + ".log");
|
||||
std::shared_ptr<interfaces::storage::File> ConfigFile = WinDurangoRoot->CreateFile("windurango.json");
|
||||
|
||||
config = Config(ConfigFile);
|
||||
log = Logging(LogFile);
|
||||
|
||||
config.parse();
|
||||
log.Initialize();
|
||||
|
||||
this->_inited = true;
|
||||
} catch (const std::exception& e) {
|
||||
std::cout << "[WinDurango::Common::WinDurango.exception] - Critical: " << e.what() << "\n";
|
||||
} catch (...) {
|
||||
std::cout << "[WinDurango::Common::WinDurango.(...))] - Critical: Unknown Error\n";
|
||||
}
|
||||
}
|
||||
} // namespace wd::common
|
||||
|
||||
@@ -23,6 +23,7 @@ namespace wd::impl::winrt::interfaces::storage {
|
||||
virtual bool close() override;
|
||||
|
||||
virtual std::filesystem::path filepath() override;
|
||||
virtual std::filesystem::path fullfilepath() override;
|
||||
|
||||
virtual bool rename(std::string) override;
|
||||
virtual bool remove() override;
|
||||
|
||||
@@ -91,6 +91,13 @@ namespace wd::impl::winrt::interfaces::storage {
|
||||
return path;
|
||||
}
|
||||
|
||||
std::filesystem::path WinRTFile::fullfilepath() {
|
||||
StorageFolder sf = ApplicationData::Current().LocalFolder();
|
||||
std::filesystem::path rootPath { sf.Path().c_str() };
|
||||
|
||||
return rootPath / path;
|
||||
}
|
||||
|
||||
bool WinRTFile::rename(std::string name) {
|
||||
if (file == nullptr) {
|
||||
return false;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "pch.h"
|
||||
#include "WinDurango.Implementation.WinRT/Interfaces/Storage/Directory.h"
|
||||
#include "WinDurango.Common/Logging.h"
|
||||
#include <iostream>
|
||||
|
||||
using namespace winrt;
|
||||
@@ -19,6 +20,7 @@ struct App : implements<App, IFrameworkViewSource, IFrameworkView>
|
||||
Visual m_selected{ nullptr };
|
||||
float2 m_offset{};
|
||||
bool runFirst = true;
|
||||
wd::common::Logging logthing;
|
||||
|
||||
IFrameworkView CreateView()
|
||||
{
|
||||
@@ -104,8 +106,14 @@ struct App : implements<App, IFrameworkViewSource, IFrameworkView>
|
||||
auto wdlog = wddir->CreateFile("log.txt");
|
||||
wdlog->open();
|
||||
(*wdlog) << std::string("Testing");
|
||||
logthing = wd::common::Logging(wdlog);
|
||||
logthing.Initialize();
|
||||
logthing.Log("WinDurango.Testing", "Hi");
|
||||
logthing.Warn("WinDurango.Testing", "Bye");
|
||||
logthing.Error("WinDurango.Testing", "Oops");
|
||||
runFirst = false;
|
||||
}
|
||||
logthing.Log("WinDurango.Testing", "Click");
|
||||
}
|
||||
|
||||
void OnPointerMoved(IInspectable const &, PointerEventArgs const & args)
|
||||
|
||||
@@ -88,7 +88,7 @@
|
||||
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
|
||||
<PrecompiledHeaderOutputFile>$(IntDir)pch.pch</PrecompiledHeaderOutputFile>
|
||||
<WarningLevel>Level4</WarningLevel>
|
||||
<AdditionalOptions>%(AdditionalOptions) /bigobj</AdditionalOptions>
|
||||
<AdditionalOptions>%(AdditionalOptions) /bigobj /utf-8</AdditionalOptions>
|
||||
<PreprocessorDefinitions>WIN32_LEAN_AND_MEAN;WINRT_LEAN_AND_MEAN;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
@@ -98,10 +98,10 @@
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)'=='Debug'">
|
||||
<ClCompile>
|
||||
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AdditionalIncludeDirectories Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(ProjectDir)..\WinDurango.Implementation.WinRT\include;$(ProjectDir)..\WinDurango.Common\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<AdditionalIncludeDirectories Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(ProjectDir)..\WinDurango.Implementation.WinRT\include;$(ProjectDir)..\WinDurango.Common\include;$(ProjectDir)..\..\build\_deps\spdlog-src\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">..\..\build\bin\Debug\WinDurango.Implementation.WinRT.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">..\..\build\bin\Debug\WinDurango.Implementation.WinRT.lib;..\..\build\bin\Debug\WinDurango.Common.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<SubSystem Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Console</SubSystem>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
|
||||
Reference in New Issue
Block a user