diff --git a/.gitignore b/.gitignore index 0111f20..420942d 100644 --- a/.gitignore +++ b/.gitignore @@ -27,4 +27,4 @@ ZERO_CHECK.dir /cmake-build-debug /out .suo -projects/WinDurango/WinDurango.Testing/.vs \ No newline at end of file +projects/WinDurango.Testing/.vs \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index f398b92..26e1b3a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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/" +) \ No newline at end of file diff --git a/projects/WinDurango.Common/CMakeLists.txt b/projects/WinDurango.Common/CMakeLists.txt index dfb9294..40a25e2 100644 --- a/projects/WinDurango.Common/CMakeLists.txt +++ b/projects/WinDurango.Common/CMakeLists.txt @@ -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) diff --git a/projects/WinDurango.Common/include/WinDurango.Common/Interfaces/Storage/File.h b/projects/WinDurango.Common/include/WinDurango.Common/Interfaces/Storage/File.h index 50392c7..cce49fa 100644 --- a/projects/WinDurango.Common/include/WinDurango.Common/Interfaces/Storage/File.h +++ b/projects/WinDurango.Common/include/WinDurango.Common/Interfaces/Storage/File.h @@ -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; diff --git a/projects/WinDurango.Common/include/WinDurango.Common/Logging.h b/projects/WinDurango.Common/include/WinDurango.Common/Logging.h index 0c8745b..570065a 100644 --- a/projects/WinDurango.Common/include/WinDurango.Common/Logging.h +++ b/projects/WinDurango.Common/include/WinDurango.Common/Logging.h @@ -1,4 +1,99 @@ /* * Logging File * WinDurango.Common::Logging -*/ \ No newline at end of file +*/ +#include +#include +#include +#include +#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 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 + 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)...); + } else { + AddLogger(codespace); + log[codespace]->info(std::forward(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 + 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)...); + } else { + AddLogger(codespace); + log[codespace]->warn(std::forward(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 + 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)...); + } else { + AddLogger(codespace); + log[codespace]->error(std::forward(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> log; + std::shared_ptr pFile; + public: + bool isConstructed = false; + bool isInitialized = false; + }; +} \ No newline at end of file diff --git a/projects/WinDurango.Common/include/WinDurango.Common/WinDurango.h b/projects/WinDurango.Common/include/WinDurango.Common/WinDurango.h index 44b25aa..4bafae2 100644 --- a/projects/WinDurango.Common/include/WinDurango.Common/WinDurango.h +++ b/projects/WinDurango.Common/include/WinDurango.Common/WinDurango.h @@ -2,25 +2,28 @@ // Created by DexrnZacAttack on 1/23/26 using zPc-i2. // #pragma once - +#include +#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 rootDir); - Config Config; + Config config; + Logging log; private: - bool _inited = false; - }; + bool _inited = false; + std::shared_ptr rootDir; + std::shared_ptr WinDurangoRoot; + }; } // namespace wd::common diff --git a/projects/WinDurango.Common/src/Config.cpp b/projects/WinDurango.Common/src/Config.cpp index bbe6ded..8799db5 100644 --- a/projects/WinDurango.Common/src/Config.cpp +++ b/projects/WinDurango.Common/src/Config.cpp @@ -2,6 +2,7 @@ bool wd::common::Config::parse() { try { + pFile->open(); std::string jsonData = pFile->read(); data = nlohmann::json::parse(jsonData); return true; diff --git a/projects/WinDurango.Common/src/Logging.cpp b/projects/WinDurango.Common/src/Logging.cpp new file mode 100644 index 0000000..abfab79 --- /dev/null +++ b/projects/WinDurango.Common/src/Logging.cpp @@ -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(); + auto file_sink = std::make_shared(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 logg = std::make_shared("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(); + auto file_sink = std::make_shared(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 logg = std::make_shared(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"; + } +} diff --git a/projects/WinDurango.Common/src/WinDurango.cpp b/projects/WinDurango.Common/src/WinDurango.cpp index 67044fd..8f27426 100644 --- a/projects/WinDurango.Common/src/WinDurango.cpp +++ b/projects/WinDurango.Common/src/WinDurango.cpp @@ -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 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 LogFile = WinDurangoRoot->CreateFile("windurango_log_" + date + ".log"); + std::shared_ptr 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 diff --git a/projects/WinDurango.Implementation.WinRT/include/WinDurango.Implementation.WinRT/Interfaces/Storage/File.h b/projects/WinDurango.Implementation.WinRT/include/WinDurango.Implementation.WinRT/Interfaces/Storage/File.h index d8d18f6..412b6bd 100644 --- a/projects/WinDurango.Implementation.WinRT/include/WinDurango.Implementation.WinRT/Interfaces/Storage/File.h +++ b/projects/WinDurango.Implementation.WinRT/include/WinDurango.Implementation.WinRT/Interfaces/Storage/File.h @@ -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; diff --git a/projects/WinDurango.Implementation.WinRT/src/interfaces/Storage/File.cpp b/projects/WinDurango.Implementation.WinRT/src/interfaces/Storage/File.cpp index 9834bc6..1ac89dc 100644 --- a/projects/WinDurango.Implementation.WinRT/src/interfaces/Storage/File.cpp +++ b/projects/WinDurango.Implementation.WinRT/src/interfaces/Storage/File.cpp @@ -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; diff --git a/projects/WinDurango.Testing/App.cpp b/projects/WinDurango.Testing/App.cpp index 9e8b12e..7ba820b 100644 --- a/projects/WinDurango.Testing/App.cpp +++ b/projects/WinDurango.Testing/App.cpp @@ -1,5 +1,6 @@ #include "pch.h" #include "WinDurango.Implementation.WinRT/Interfaces/Storage/Directory.h" +#include "WinDurango.Common/Logging.h" #include using namespace winrt; @@ -19,6 +20,7 @@ struct App : implements Visual m_selected{ nullptr }; float2 m_offset{}; bool runFirst = true; + wd::common::Logging logthing; IFrameworkView CreateView() { @@ -104,8 +106,14 @@ struct App : implements 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) diff --git a/projects/WinDurango.Testing/WinDurango.Testing.vcxproj b/projects/WinDurango.Testing/WinDurango.Testing.vcxproj index 18f4c31..6900c6f 100644 --- a/projects/WinDurango.Testing/WinDurango.Testing.vcxproj +++ b/projects/WinDurango.Testing/WinDurango.Testing.vcxproj @@ -88,7 +88,7 @@ pch.h $(IntDir)pch.pch Level4 - %(AdditionalOptions) /bigobj + %(AdditionalOptions) /bigobj /utf-8 WIN32_LEAN_AND_MEAN;WINRT_LEAN_AND_MEAN;%(PreprocessorDefinitions) @@ -98,10 +98,10 @@ _DEBUG;%(PreprocessorDefinitions) - $(ProjectDir)..\WinDurango.Implementation.WinRT\include;$(ProjectDir)..\WinDurango.Common\include;%(AdditionalIncludeDirectories) + $(ProjectDir)..\WinDurango.Implementation.WinRT\include;$(ProjectDir)..\WinDurango.Common\include;$(ProjectDir)..\..\build\_deps\spdlog-src\include;%(AdditionalIncludeDirectories) - ..\..\build\bin\Debug\WinDurango.Implementation.WinRT.lib;%(AdditionalDependencies) + ..\..\build\bin\Debug\WinDurango.Implementation.WinRT.lib;..\..\build\bin\Debug\WinDurango.Common.lib;%(AdditionalDependencies) Console