diff --git a/engine/pch.hpp b/engine/pch.hpp index 7a4822a..3ebad0c 100644 --- a/engine/pch.hpp +++ b/engine/pch.hpp @@ -6,9 +6,12 @@ #include #include #include +#include #include #include #include #include #include #include +#include +#include diff --git a/engine/src/core/ICEApplication.cpp b/engine/src/core/ICEApplication.cpp index ad7f6ee..db2fad3 100644 --- a/engine/src/core/ICEApplication.cpp +++ b/engine/src/core/ICEApplication.cpp @@ -6,6 +6,29 @@ #include "pch.hpp" namespace ICEngine { + ICEApplication::TerminationHandler ICEApplication::customHandler = ICEApplication::DefaultTerminationHandler; + + void ICEApplication::SetTerminationHandler(TerminationHandler handler) { + if (handler) { + customHandler = handler; + } else { + customHandler = DefaultTerminationHandler; + } + } + + void ICEApplication::HandleFatalError(const std::string& message) { + if (customHandler) { + customHandler(message); + } else { + DefaultTerminationHandler(message); + } + } + + void ICEApplication::DefaultTerminationHandler(const std::string& message) { + Log::GetCoreLogger()->Log(CRITICAL, __FILE__, __LINE__, __FUNCTION__, message); + std::exit(EXIT_FAILURE); + } + void StartApplication() { Log::Init(); CORE_LOG_TRACE("Starting ICEngine"); @@ -21,4 +44,4 @@ namespace ICEngine { CORE_LOG_TRACE("Shutting down ICEngine"); } -} \ No newline at end of file +} diff --git a/engine/src/core/ICEApplication.hpp b/engine/src/core/ICEApplication.hpp index a72cce5..8c0857b 100644 --- a/engine/src/core/ICEApplication.hpp +++ b/engine/src/core/ICEApplication.hpp @@ -4,16 +4,29 @@ #pragma once +#include +#include "Logger.hpp" + namespace ICEngine { class ICEApplication { public: ICEApplication() {} virtual ~ICEApplication() {} + virtual void Run() = 0; + + using TerminationHandler = std::function; + + static void SetTerminationHandler(TerminationHandler handler); + static void HandleFatalError(const std::string& message); + + private: + static TerminationHandler customHandler; + static void DefaultTerminationHandler(const std::string& message); }; - void StartApplication(); // Starts the application, managed by the engine. + void StartApplication(); // Application-specific function that will be defined by the user extern ICEApplication* CreateICEApplication(); -} \ No newline at end of file +} diff --git a/engine/src/core/Logger.hpp b/engine/src/core/Logger.hpp index 132f524..b516659 100644 --- a/engine/src/core/Logger.hpp +++ b/engine/src/core/Logger.hpp @@ -174,33 +174,41 @@ namespace ICEngine { Log &operator=(const Log &) = delete; }; +#define CORE_LOG_FATAL(message) \ + do { \ + ICEngine::Log::GetCoreLogger()->Log(ICEngine::FATAL, __FILE__, __LINE__, __FUNCTION__, message); \ + ICEngine::ICEApplication::HandleFatalError(message); \ + } while (0) + +#define LOG_FATAL(message) \ + do { \ + ICEngine::Log::GetAppLogger()->Log(ICEngine::FATAL, __FILE__, __LINE__, __FUNCTION__, message); \ + ICEngine::ICEApplication::HandleFatalError(message); \ + } while (0) + #ifndef NDEBUG - #define CORE_LOG_TRACE(message, ...) ICEngine::Log::GetCoreLogger()->Log(ICEngine::TRACE, __FILE__, __LINE__, __FUNCTION__, message, ##__VA_ARGS__) - #define CORE_LOG_DEBUG(message, ...) ICEngine::Log::GetCoreLogger()->Log(ICEngine::DEBUG, __FILE__, __LINE__, __FUNCTION__, message, ##__VA_ARGS__) - #define CORE_LOG_WARN(message, ...) ICEngine::Log::GetCoreLogger()->Log(ICEngine::WARN, __FILE__, __LINE__, __FUNCTION__, message, ##__VA_ARGS__) - #define CORE_LOG_ERROR(message, ...) ICEngine::Log::GetCoreLogger()->Log(ICEngine::ERROR, __FILE__, __LINE__, __FUNCTION__, message, ##__VA_ARGS__) - #define CORE_LOG_CRITICAL(message, ...) ICEngine::Log::GetCoreLogger()->Log(ICEngine::CRITICAL, __FILE__, __LINE__, __FUNCTION__, message, ##__VA_ARGS__) - #define CORE_LOG_FATAL(message, ...) ICEngine::Log::GetCoreLogger()->Log(ICEngine::FATAL, __FILE__, __LINE__, __FUNCTION__, message, ##__VA_ARGS__) + #define CORE_LOG_TRACE(message, ...) ICEngine::Log::GetCoreLogger()->Log(ICEngine::TRACE, __FILE__, __LINE__, __FUNCTION__, message, ##__VA_ARGS__) + #define CORE_LOG_DEBUG(message, ...) ICEngine::Log::GetCoreLogger()->Log(ICEngine::DEBUG, __FILE__, __LINE__, __FUNCTION__, message, ##__VA_ARGS__) + #define CORE_LOG_WARN(message, ...) ICEngine::Log::GetCoreLogger()->Log(ICEngine::WARN, __FILE__, __LINE__, __FUNCTION__, message, ##__VA_ARGS__) + #define CORE_LOG_ERROR(message, ...) ICEngine::Log::GetCoreLogger()->Log(ICEngine::ERROR, __FILE__, __LINE__, __FUNCTION__, message, ##__VA_ARGS__) + #define CORE_LOG_CRITICAL(message, ...) ICEngine::Log::GetCoreLogger()->Log(ICEngine::CRITICAL, __FILE__, __LINE__, __FUNCTION__, message, ##__VA_ARGS__) - #define LOG_TRACE(message, ...) ICEngine::Log::GetAppLogger()->Log(ICEngine::TRACE, __FILE__, __LINE__, __FUNCTION__, message, ##__VA_ARGS__) - #define LOG_DEBUG(message, ...) ICEngine::Log::GetAppLogger()->Log(ICEngine::DEBUG, __FILE__, __LINE__, __FUNCTION__, message, ##__VA_ARGS__) - #define LOG_WARN(message, ...) ICEngine::Log::GetAppLogger()->Log(ICEngine::WARN, __FILE__, __LINE__, __FUNCTION__, message, ##__VA_ARGS__) - #define LOG_ERROR(message, ...) ICEngine::Log::GetAppLogger()->Log(ICEngine::ERROR, __FILE__, __LINE__, __FUNCTION__, message, ##__VA_ARGS__) - #define LOG_CRITICAL(message, ...) ICEngine::Log::GetAppLogger()->Log(ICEngine::CRITICAL, __FILE__, __LINE__, __FUNCTION__, message, ##__VA_ARGS__) - #define LOG_FATAL(message, ...) ICEngine::Log::GetAppLogger()->Log(ICEngine::FATAL, __FILE__, __LINE__, __FUNCTION__, message, ##__VA_ARGS__) + #define LOG_TRACE(message, ...) ICEngine::Log::GetAppLogger()->Log(ICEngine::TRACE, __FILE__, __LINE__, __FUNCTION__, message, ##__VA_ARGS__) + #define LOG_DEBUG(message, ...) ICEngine::Log::GetAppLogger()->Log(ICEngine::DEBUG, __FILE__, __LINE__, __FUNCTION__, message, ##__VA_ARGS__) + #define LOG_WARN(message, ...) ICEngine::Log::GetAppLogger()->Log(ICEngine::WARN, __FILE__, __LINE__, __FUNCTION__, message, ##__VA_ARGS__) + #define LOG_ERROR(message, ...) ICEngine::Log::GetAppLogger()->Log(ICEngine::ERROR, __FILE__, __LINE__, __FUNCTION__, message, ##__VA_ARGS__) + #define LOG_CRITICAL(message, ...) ICEngine::Log::GetAppLogger()->Log(ICEngine::CRITICAL, __FILE__, __LINE__, __FUNCTION__, message, ##__VA_ARGS__) #else - #define CORE_LOG_TRACE(message, ...) - #define CORE_LOG_DEBUG(message, ...) - #define CORE_LOG_WARN(message, ...) - #define CORE_LOG_ERROR(message, ...) - #define CORE_LOG_CRITICAL(message, ...) - #define CORE_LOG_FATAL(message, ...) + #define CORE_LOG_TRACE(message, ...) + #define CORE_LOG_DEBUG(message, ...) + #define CORE_LOG_WARN(message, ...) + #define CORE_LOG_ERROR(message, ...) + #define CORE_LOG_CRITICAL(message, ...) - #define LOG_TRACE(message, ...) - #define LOG_DEBUG(message, ...) - #define LOG_WARN(message, ...) - #define LOG_ERROR(message, ...) - #define LOG_CRITICAL(message, ...) - #define LOG_FATAL(message, ...) + #define LOG_TRACE(message, ...) + #define LOG_DEBUG(message, ...) + #define LOG_WARN(message, ...) + #define LOG_ERROR(message, ...) + #define LOG_CRITICAL(message, ...) #endif }