From 9c9ba87d68285cd5fa5b5eb965c0e6556627d137 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Sat, 28 Sep 2024 22:12:44 -0700 Subject: [PATCH] Clean up the debug example a little --- examples/aldebug.cpp | 79 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 48 insertions(+), 31 deletions(-) diff --git a/examples/aldebug.cpp b/examples/aldebug.cpp index f93d88bd..45983e59 100644 --- a/examples/aldebug.cpp +++ b/examples/aldebug.cpp @@ -39,6 +39,7 @@ #include "AL/alext.h" #include "alspan.h" +#include "alstring.h" #include "win_main_utf8.h" @@ -114,7 +115,7 @@ int main(al::span args) /* Print out usage if -h was specified */ if(args.size() > 1 && (args[1] == "-h" || args[1] == "--help")) { - std::cerr<< "Usage: "<]\n"; + std::cerr<< "Usage: "<] [-nodebug]\n"; return 1; } @@ -157,11 +158,13 @@ int main(al::span args) LOAD_PROC(alGetPointervEXT); #undef LOAD_PROC + /* Create a debug context and set it as current. If -nodebug was specified, + * create a non-debug context (to see how debug messages react). + */ auto flags = ALCint{ALC_CONTEXT_DEBUG_BIT_EXT}; if(!args.empty() && args[0] == "-nodebug") flags &= ~ALC_CONTEXT_DEBUG_BIT_EXT; - /* Create a debug context and set it as current. */ const auto attribs = std::array{{ ALC_CONTEXT_FLAGS_EXT, flags, 0 /* end-of-list */ @@ -173,6 +176,14 @@ int main(al::span args) return 1; } + /* Enable notification and low-severity debug messages, which are disabled + * by default. + */ + alDebugMessageControlEXT(AL_DONT_CARE_EXT, AL_DONT_CARE_EXT, + AL_DEBUG_SEVERITY_NOTIFICATION_EXT, 0, nullptr, AL_TRUE); + alDebugMessageControlEXT(AL_DONT_CARE_EXT, AL_DONT_CARE_EXT, AL_DEBUG_SEVERITY_LOW_EXT, 0, + nullptr, AL_TRUE); + printf("Context flags: 0x%08x\n", alGetInteger(AL_CONTEXT_FLAGS_EXT)); /* A debug context has debug output enabled by default. But in case this @@ -184,7 +195,8 @@ int main(al::span args) alEnable(AL_DEBUG_OUTPUT_EXT); /* The max debug message length property will allow us to define message - * storage of sufficient length. + * storage of sufficient length. This includes space for the null + * terminator. */ const auto maxloglength = alGetInteger(AL_MAX_DEBUG_MESSAGE_LENGTH_EXT); printf("Max debug message length: %d\n", maxloglength); @@ -192,21 +204,22 @@ int main(al::span args) fputs("\n", stdout); /* Doppler Velocity is deprecated since AL 1.1, so this should generate a - * deprecation debug message. + * deprecation debug message. We'll first handle debug messages through the + * message log, meaning we'll query for and read it afterward. */ printf("Calling alDopplerVelocity(0.5f)...\n"); alDopplerVelocity(0.5f); - auto numlogs = alGetInteger(AL_DEBUG_LOGGED_MESSAGES_EXT); - while(numlogs > 0) + for(auto numlogs = alGetInteger(AL_DEBUG_LOGGED_MESSAGES_EXT);numlogs > 0;--numlogs) { - auto message = std::vector(static_cast(maxloglength), '\0'); + auto message = std::vector(static_cast(maxloglength), '\0'); auto source = ALenum{}; auto type = ALenum{}; auto id = ALuint{}; auto severity = ALenum{}; auto msglength = ALsizei{}; + /* Getting the message removes it from the log. */ const auto read = alGetDebugMessageLogEXT(1, maxloglength, &source, &type, &id, &severity, &msglength, message.data()); if(read != 1) @@ -215,35 +228,45 @@ int main(al::span args) break; } - printf("Got message log:\n" + /* The message lengths returned by alGetDebugMessageLogEXT include the + * null terminator, so subtract one for the string_view length. If we + * read more than one message at a time, the length could be used as + * the offset to the next message. + */ + const auto msgstr = std::string_view{message.data(), + static_cast(msglength ? msglength-1 : 0)}; + printf("Got message from log:\n" " Source: %s\n" " Type: %s\n" " ID: %u\n" " Severity: %s\n" - " Message: \"%s\"\n", GetDebugSourceName(source), GetDebugTypeName(type), id, - GetDebugSeverityName(severity), message.data()); - - --numlogs; + " Message: \"%.*s\"\n", GetDebugSourceName(source), GetDebugTypeName(type), id, + GetDebugSeverityName(severity), al::sizei(msgstr), msgstr.data()); } fputs("\n", stdout); - /* Now set up a callback. */ - auto debug_callback = [](ALenum source, ALenum type, ALuint id, ALenum severity, - ALsizei length [[maybe_unused]], const ALchar *message, void *userParam [[maybe_unused]]) + /* Now set up a callback function. This lets us print the debug messages as + * they happen without having to explicitly query and get them. + */ + static constexpr auto debug_callback = [](ALenum source, ALenum type, ALuint id, + ALenum severity, ALsizei length, const ALchar *message, void *userParam [[maybe_unused]]) noexcept -> void { - printf("Got message callback:\n" + /* The message length provided to the callback does not include the + * null terminator. + */ + const auto msgstr = std::string_view{message, static_cast(length)}; + printf("Got message from callback:\n" " Source: %s\n" " Type: %s\n" " ID: %u\n" " Severity: %s\n" - " Message: \"%s\"\n", GetDebugSourceName(source), GetDebugTypeName(type), id, - GetDebugSeverityName(severity), message); + " Message: \"%.*s\"\n", GetDebugSourceName(source), GetDebugTypeName(type), id, + GetDebugSeverityName(severity), al::sizei(msgstr), msgstr.data()); }; alDebugMessageCallbackEXT(debug_callback, nullptr); - numlogs = alGetInteger(AL_DEBUG_LOGGED_MESSAGES_EXT); - if(numlogs != 0) + if(const auto numlogs = alGetInteger(AL_DEBUG_LOGGED_MESSAGES_EXT)) fprintf(stderr, "%d left over logged message%s!\n", numlogs, (numlogs==1)?"":"s"); /* This should also generate a deprecation debug message, which will now go @@ -254,21 +277,13 @@ int main(al::span args) fputs("\n", stdout); /* These functions are notoriously unreliable for their behavior, they will - * likely generate debug messages. + * likely generate portability debug messages. */ printf("Calling alcSuspendContext and alcProcessContext...\n"); alcSuspendContext(context.get()); alcProcessContext(context.get()); fputs("\n", stdout); - /* Enable notification and low-severity debug messages, which are disabled - * by default. - */ - alDebugMessageControlEXT(AL_DONT_CARE_EXT, AL_DONT_CARE_EXT, - AL_DEBUG_SEVERITY_NOTIFICATION_EXT, 0, nullptr, AL_TRUE); - alDebugMessageControlEXT(AL_DONT_CARE_EXT, AL_DONT_CARE_EXT, AL_DEBUG_SEVERITY_LOW_EXT, 0, - nullptr, AL_TRUE); - printf("Pushing a debug group, making some invalid calls, and popping the debug group...\n"); alPushDebugGroupEXT(AL_DEBUG_SOURCE_APPLICATION_EXT, 0, -1, "Error test group"); alSpeedOfSound(0.0f); @@ -277,9 +292,11 @@ int main(al::span args) alPopDebugGroupEXT(); fputs("\n", stdout); - /* All done, unset the callback, the context and device will clean - * themselves up. + /* All done, insert a custom message and unset the callback. The context + * and device will clean themselves up. */ + alDebugMessageInsertEXT(AL_DEBUG_SOURCE_APPLICATION_EXT, AL_DEBUG_TYPE_MARKER_EXT, 0, + AL_DEBUG_SEVERITY_NOTIFICATION_EXT, -1, "End of run, cleaning up"); alDebugMessageCallbackEXT(nullptr, nullptr); return 0; -- 2.11.4.GIT