Compare commits

..

3 Commits

Author SHA1 Message Date
5e76c8a382 chore(example)(client): update to work with latest engine changes
- Change logging to explain the new fatal handler.
- Add custom termination handler example
2024-09-20 10:45:08 -06:00
91cf0d25d5 feat(platform): add platform specific popups
Please note! These changes have not yet been tested on POSIX platforms
(mac OR linux) so please do not expect them to work!
2024-09-20 10:44:12 -06:00
0eebacc28f feat(core): add termination handling extensability 2024-09-20 10:43:22 -06:00
10 changed files with 166 additions and 38 deletions

View File

@ -50,6 +50,15 @@ INCLUDE_DIRECTORIES(engine/)
SET(EngineSources
engine/src/core/ICEApplication.cpp
)
IF (WIN32)
LIST(APPEND EngineSources
engine/src/platform/windows/WindowsPopup.cpp
)
ELSEIF (UNIX)
LIST(APPEND EngineSources
engine/src/platform/posix/PosixPopup.cpp
)
ENDIF()
SET(PrecompiledHeader "pch.h")

View File

@ -7,5 +7,6 @@
#include "src/core/Logger.hpp"
#include "src/core/ICEApplication.hpp"
#include "src/platform/Platform.hpp"
#endif //ICENGINE_HPP

View File

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

View File

@ -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");
}
}
}

View File

@ -4,16 +4,29 @@
#pragma once
#include <pch.hpp>
#include "Logger.hpp"
namespace ICEngine {
class ICEApplication {
public:
ICEApplication() {}
virtual ~ICEApplication() {}
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
extern ICEApplication* CreateICEApplication();
}
}

View File

@ -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
}

View File

@ -0,0 +1,14 @@
//
// Created by illyum on 9/20/2024.
//
#pragma once
#include <pch.hpp>
namespace Platform {
void ShowPopup(const char *message);
void ShowPopup(const char *title, const char *message);
void ShowPopup(const std::string &message);
void ShowPopup(const std::string &title, const std::string &message);
}

View File

@ -0,0 +1,17 @@
//
// Created by illyum on 9/20/2024.
//
#include "../Platform.hpp"
#include <pch.hpp>
namespace Platform {
void ShowPopup(const char* message) {
std::string command = "zenity --info --text=\"";
command += message;
command += "\"";
system(command.c_str());
}
}

View File

@ -0,0 +1,25 @@
//
// Created by illyum on 9/20/2024.
//
#include <pch.hpp>
#include "../Platform.hpp"
#include <windows.h>
namespace Platform {
void ShowPopup(const char *message) {
MessageBoxA(nullptr, message, "ICEngine", MB_OK);
}
void ShowPopup(const char *title, const char *message) {
MessageBoxA(nullptr, message, title, MB_OK);
}
void ShowPopup(const std::string &message) {
MessageBoxA(nullptr, message.c_str(), "ICEngine", MB_OK);
}
void ShowPopup(const std::string &title, const std::string &message) {
MessageBoxA(nullptr, message.c_str(), title.c_str(), MB_OK);
}
}

View File

@ -4,20 +4,21 @@
#include <ICEngine.hpp>
#include <fstream>
#include <src/platform/Platform.hpp>
void ShowLogger();
void CustomTermHandler(const std::string& message);
int main() {
try {
ShowLogger();
ICEngine::StartApplication();
} catch (const std::exception& e) {
CORE_LOG_FATAL("Unhandled exception: {}", e.what());
return -1;
} catch (...) {
CORE_LOG_FATAL("Unknown fatal error occurred.");
return -1;
}
// You can set a custom termination handler/function.
// Make sure you do this before you start the application!!!
ICEngine::ICEApplication::SetTerminationHandler(CustomTermHandler);
// This is optional
// Logger Information
ShowLogger();
ICEngine::StartApplication();
return 0;
}
@ -49,7 +50,10 @@ void ShowLogger() {
// take ownership of the streams and clean them up when the app
// is complete
ICEngine::Log::GetCoreLogger()->AddSink(std::make_shared<ICEngine::FileSink>("core_logs.txt"));
ICEngine::Log::GetCoreLogger()->AddSink(std::make_shared<ICEngine::ConsoleSink>());
ICEngine::Log::GetAppLogger()->AddSink(std::make_shared<ICEngine::FileSink>("app_logs.txt"));
ICEngine::Log::GetAppLogger()->AddSink(std::make_shared<ICEngine::ConsoleSink>());
// You can log multiple different types with formatting
int age = 30;
@ -62,7 +66,7 @@ void ShowLogger() {
CORE_LOG_DEBUG("This is an example of a debug message"); // Logs a debug message, typically used for debugging purposes
CORE_LOG_WARN("This is an example of a warn message"); // Logs a warning, indicating a non-critical issue that should be investigated
CORE_LOG_ERROR("This is an example of an error message"); // Logs an error, signaling that something has gone wrong but the engine can still run
CORE_LOG_CRITICAL("This is an example of a critical message\n"); // Logs a critical issue, often indicating a major problem in the core system that needs immediate attention.
CORE_LOG_CRITICAL("This is an example of a critical message"); // Logs a critical issue, often indicating a major problem in the core system that needs immediate attention.
// CORE_LOG_FATAL: Logs a fatal error that crashes the application. Fatal errors in the core usually lead to dumping information into crash logs
// and stopping the program. Use with extreme caution and only in situations where the application can't recover
// CORE_LOG_FATAL("This is an example of a fatal message");
@ -81,10 +85,21 @@ void ShowLogger() {
// LOG_FATAL("This is an example of log fatal crash!");
}
void CustomTermHandler(const std::string& message) {
// Perform any other task here, like showing a popup warning indicating something crashed
Platform::ShowPopup("Demo App", message);
// You don't have to specify a title, it will default to ICEngine
// Platform::ShowPopup(message);
std::exit(EXIT_FAILURE); // Terminate here if you need to
}
class DemoApp : public ICEngine::ICEApplication {
public:
void Run() override {
LOG_TRACE("Running demo app!");
CORE_LOG_FATAL("Fatal error"); // This will terminate the application
LOG_TRACE("Demo app completed successfully!"); // Therefore this will not run
}
};