Skip to content

Instantly share code, notes, and snippets.

@sridenour

sridenour/log.c Secret

Last active January 18, 2023 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sridenour/a90879a2f7ee3866aa1a48223c0b65fb to your computer and use it in GitHub Desktop.
Save sridenour/a90879a2f7ee3866aa1a48223c0b65fb to your computer and use it in GitHub Desktop.
Demonstrate logging errors, info messages, etc. with SDL2 logging facilities
/* build this with
cc log.c -o log `sdl2-config --cflags --libs`
run with
./log -l <loglevel>
And you will only see log messages that are at least that priority.
*/
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <SDL2/SDL.h>
static void setLogPriority(const char *arg);
int main(int argc, char **argv)
{
(void)SDL_Init(SDL_INIT_VIDEO);
int option;
while((option = getopt(argc, argv, "l:")) != -1) {
switch(option) {
case 'l':
setLogPriority(optarg);
break;
case '?':
fprintf(stderr, "unknown option '%c'\n", optopt);
break;
}
}
/* These are all in the APPLICATION category, but we can still log errors etc */
SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, "Critical\n");
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error\n");
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Warning\n");
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Info\n");
SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "Debug\n");
SDL_LogVerbose(SDL_LOG_CATEGORY_APPLICATION, "Verbose\n");
SDL_Quit();
return 0;
}
static void setLogPriority(const char *arg)
{
SDL_LogPriority priority = SDL_LOG_PRIORITY_VERBOSE;
if(strncasecmp("debug", arg, strlen("debug")) == 0) {
priority = SDL_LOG_PRIORITY_DEBUG;
} else if(strncasecmp("info", arg, strlen("info")) == 0) {
priority = SDL_LOG_PRIORITY_INFO;
} else if(strncasecmp("warn", arg, strlen("warn")) == 0) {
priority = SDL_LOG_PRIORITY_WARN;
} else if(strncasecmp("error", arg, strlen("error")) == 0) {
priority = SDL_LOG_PRIORITY_ERROR;
} else if(strncasecmp("critical", arg, strlen("critical")) == 0) {
priority = SDL_LOG_PRIORITY_CRITICAL;
}
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, priority);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment