error_handling.hpp

This commit is contained in:
Angie 2024-05-25 15:45:44 -04:00 committed by angie
parent f5dd551e9e
commit d582b9e6fa
6 changed files with 46 additions and 16 deletions

View file

@ -11,6 +11,7 @@ set(CMAKE_CXX_EXTENSIONS OFF)
add_library(ultramodern STATIC
"${CMAKE_CURRENT_SOURCE_DIR}/src/audio.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/error_handling.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/events.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/mesgqueue.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/misc_ultra.cpp"

View file

@ -0,0 +1,24 @@
#ifndef __ERROR_HANDLING_HPP__
#define __ERROR_HANDLING_HPP__
namespace ultramodern {
namespace error_handling {
struct error_handling_callbacks_t {
using message_box_t = void(const char* msg);
/**
* Show an OS dialog with the given `msg`.
*
* The `msg` parameter is always non-`nullptr`.
*/
message_box_t *message_box;
};
void set_error_handling_callbacks(const error_handling_callbacks_t& callbacks);
void message_box(const char* msg);
}
}
#endif

View file

@ -26,6 +26,7 @@
#endif
#include <ultramodern/rsp_stuff.hpp>
#include <ultramodern/error_handling.hpp>
struct UltraThreadContext {
std::thread host_thread;

View file

@ -13,12 +13,6 @@ namespace ultramodern {
void (*update_rumble)();
void (*update_supported_options)();
/**
* Show an OS dialog with the given `msg`.
* `msg` is non-`nullptr`.
*/
void (*message_box)(const char* msg);
};
/**

View file

@ -0,0 +1,19 @@
#include <cstdio>
#include "ultramodern/error_handling.hpp"
static ultramodern::error_handling::error_handling_callbacks_t error_handling_callbacks{};
void ultramodern::error_handling::set_error_handling_callbacks(const ultramodern::error_handling::error_handling_callbacks_t& callbacks) {
error_handling_callbacks = callbacks;
}
void ultramodern::error_handling::message_box(const char* msg) {
// We print the message to stderr since the user may not have provided a message_box callback
// TODO: is fprintf ok? or do we prefer using something more C++'ish?
fprintf(stderr, "%s\n", msg);
if (error_handling_callbacks.message_box != nullptr) {
error_handling_callbacks.message_box(msg);
}
}

View file

@ -200,8 +200,6 @@ void task_thread_func(uint8_t* rdram, moodycamel::LightweightSemaphore* thread_r
// Notify the caller thread that this thread is ready.
thread_ready->signal();
auto& user_callbacks = ultramodern::get_user_callbacks();
while (true) {
// Wait until an RSP task has been sent
OSTask* task;
@ -543,16 +541,9 @@ void ultramodern::init_events(RDRAM_ARG ultramodern::WindowHandle window_handle)
ultramodern::RT64SetupResult setup_result = rt64_setup_result.load();
if (rt64_setup_result != ultramodern::RT64SetupResult::Success) {
auto show_rt64_error = [](const std::string& msg) {
auto& user_callbacks = ultramodern::get_user_callbacks();
std::string error_msg = "An error has been encountered on startup: " + msg;
// We print the message to stderr since the user may not have provided a message_box callback
// TODO: is fprintf ok? or do we prefer using something more C++'ish?
fprintf(stderr, "%s\n", error_msg.c_str());
if (user_callbacks.message_box != nullptr) {
user_callbacks.message_box(error_msg.c_str());
}
ultramodern::error_handling::message_box(error_msg.c_str());
};
const std::string driver_os_suffix = "\nPlease make sure your GPU drivers and your OS are up to date.";