From be38d5e2bbd03cab4f31707d8a012a4ce119fc40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sat, 14 Jun 2025 09:48:22 +0200 Subject: [PATCH] Platform: prefix error messages coming from SDL2 and GLFW. So it's clear the error comes from Magnum and not something else. Also unify these and make GLFW not report the error on two lines if the init fails. Ugh, except that glfwGetError() is only there since 3.3, so I have to keep the old code as well. --- src/Magnum/Platform/GlfwApplication.cpp | 28 ++++++++++++++++++++++--- src/Magnum/Platform/Sdl2Application.cpp | 2 +- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/Magnum/Platform/GlfwApplication.cpp b/src/Magnum/Platform/GlfwApplication.cpp index 9b0357f9d..f643ef0eb 100644 --- a/src/Magnum/Platform/GlfwApplication.cpp +++ b/src/Magnum/Platform/GlfwApplication.cpp @@ -95,10 +95,16 @@ GlfwApplication::GlfwApplication(const Arguments& arguments, NoCreateT): .parse(arguments.argc, arguments.argv); #endif - /* Init GLFW */ + /* GLFW before 3.3 doesn't have glfwGetError(), use glfwSetErrorCallback() + even before glfwInit() in that case. It'll be on two lines but better + than getting no reason at all. */ + #if GLFW_VERSION_MAJOR*100 + GLFW_VERSION_MINOR < 303 glfwSetErrorCallback([](int, const char* const description) { - Error{} << description; + Error{} << "Platform::GlfwApplication:" << description; }); + #endif + + /* Init GLFW */ #ifdef CORRADE_TARGET_APPLE /* Don't change current working directory to Resources/ in the app bundle on Apple platforms. Not sure why this would be done only on a single @@ -108,10 +114,26 @@ GlfwApplication::GlfwApplication(const Arguments& arguments, NoCreateT): glfwInitHint(GLFW_COCOA_CHDIR_RESOURCES, false); #endif if(!glfwInit()) { - Error() << "Could not initialize GLFW"; + /* On GLFW before 3.3 the error is printed by the callback that's set + up above */ + #if GLFW_VERSION_MAJOR*100 + GLFW_VERSION_MINOR < 303 + Error{} << "Platform::GlfwApplication: could not initialize GLFW"; + #else + const char* error = nullptr; + CORRADE_INTERNAL_ASSERT_OUTPUT(glfwGetError(&error) != GLFW_NO_ERROR && error); + Error{} << "Platform::GlfwApplication: could not initialize GLFW:" << error; + #endif std::exit(8); } + /* Set error callback for further errors. On GLFW 3.3+ done after init so + we don't print the error message twice. */ + #if GLFW_VERSION_MAJOR*100 + GLFW_VERSION_MINOR >= 303 + glfwSetErrorCallback([](int, const char* const description) { + Error{} << "Platform::GlfwApplication:" << description; + }); + #endif + /* Save command-line arguments */ if(args.value("log") == "verbose") _verboseLog = true; const Containers::StringView dpiScaling = args.value("dpi-scaling"); diff --git a/src/Magnum/Platform/Sdl2Application.cpp b/src/Magnum/Platform/Sdl2Application.cpp index be7d1f11e..8b25a643d 100644 --- a/src/Magnum/Platform/Sdl2Application.cpp +++ b/src/Magnum/Platform/Sdl2Application.cpp @@ -239,7 +239,7 @@ Sdl2Application::Sdl2Application(const Arguments& arguments, NoCreateT): #endif if(SDL_Init(SDL_INIT_VIDEO) < 0) { - Error() << "Cannot initialize SDL:" << SDL_GetError(); + Error{} << "Platform::Sdl2Application: could not initialize SDL:" << SDL_GetError(); std::exit(1); }