feat(core): add termination handling extensability

This commit is contained in:
illyum 2024-09-20 10:43:22 -06:00
parent 51de852ab1
commit 0eebacc28f
4 changed files with 74 additions and 27 deletions

View File

@ -6,9 +6,12 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <chrono> #include <chrono>
#include <csignal>
#include <cstdarg> #include <cstdarg>
#include <iomanip> #include <iomanip>
#include <utility> #include <utility>
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
#include <iostream> #include <iostream>
#include <exception>
#include <functional>

View File

@ -6,6 +6,29 @@
#include "pch.hpp" #include "pch.hpp"
namespace ICEngine { 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() { void StartApplication() {
Log::Init(); Log::Init();
CORE_LOG_TRACE("Starting ICEngine"); CORE_LOG_TRACE("Starting ICEngine");
@ -21,4 +44,4 @@ namespace ICEngine {
CORE_LOG_TRACE("Shutting down ICEngine"); CORE_LOG_TRACE("Shutting down ICEngine");
} }
} }

View File

@ -4,16 +4,29 @@
#pragma once #pragma once
#include <pch.hpp>
#include "Logger.hpp"
namespace ICEngine { namespace ICEngine {
class ICEApplication { class ICEApplication {
public: public:
ICEApplication() {} ICEApplication() {}
virtual ~ICEApplication() {} virtual ~ICEApplication() {}
virtual void Run() = 0; virtual void Run() = 0;
using TerminationHandler = std::function<void(const std::string&)>;
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 // Application-specific function that will be defined by the user
extern ICEApplication* CreateICEApplication(); extern ICEApplication* CreateICEApplication();
} }

View File

@ -174,33 +174,41 @@ namespace ICEngine {
Log &operator=(const Log &) = delete; 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 #ifndef NDEBUG
#define CORE_LOG_TRACE(message, ...) ICEngine::Log::GetCoreLogger()->Log(ICEngine::TRACE, __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_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_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_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_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 LOG_TRACE(message, ...) ICEngine::Log::GetAppLogger()->Log(ICEngine::TRACE, __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_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_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_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_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__)
#else #else
#define CORE_LOG_TRACE(message, ...) #define CORE_LOG_TRACE(message, ...)
#define CORE_LOG_DEBUG(message, ...) #define CORE_LOG_DEBUG(message, ...)
#define CORE_LOG_WARN(message, ...) #define CORE_LOG_WARN(message, ...)
#define CORE_LOG_ERROR(message, ...) #define CORE_LOG_ERROR(message, ...)
#define CORE_LOG_CRITICAL(message, ...) #define CORE_LOG_CRITICAL(message, ...)
#define CORE_LOG_FATAL(message, ...)
#define LOG_TRACE(message, ...) #define LOG_TRACE(message, ...)
#define LOG_DEBUG(message, ...) #define LOG_DEBUG(message, ...)
#define LOG_WARN(message, ...) #define LOG_WARN(message, ...)
#define LOG_ERROR(message, ...) #define LOG_ERROR(message, ...)
#define LOG_CRITICAL(message, ...) #define LOG_CRITICAL(message, ...)
#define LOG_FATAL(message, ...)
#endif #endif
} }