obs-studio/libobs/util/base.c

136 lines
3.0 KiB
C
Raw Normal View History

/*
* Copyright (c) 2013 Hugh Bailey <obs.jim@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
2013-09-30 19:37:13 -07:00
#include <stdio.h>
#include <stdlib.h>
#include "c99defs.h"
#include "base.h"
#ifdef _DEBUG
static int log_output_level = LOG_DEBUG;
#else
static int log_output_level = LOG_INFO;
#endif
static int crashing = 0;
static void *log_param = NULL;
static void *crash_param = NULL;
2013-09-30 19:37:13 -07:00
static void def_log_handler(int log_level, const char *format,
va_list args, void *param)
2013-09-30 19:37:13 -07:00
{
char out[4096];
vsnprintf(out, sizeof(out), format, args);
2014-04-03 14:41:07 -07:00
if (log_level <= log_output_level) {
switch (log_level) {
2013-09-30 19:37:13 -07:00
case LOG_DEBUG:
2014-04-05 13:09:25 -07:00
fprintf(stdout, "debug: %s\n", out);
fflush(stdout);
2013-09-30 19:37:13 -07:00
break;
case LOG_INFO:
2014-04-05 13:09:25 -07:00
fprintf(stdout, "info: %s\n", out);
fflush(stdout);
2013-09-30 19:37:13 -07:00
break;
case LOG_WARNING:
2014-04-05 13:09:25 -07:00
fprintf(stdout, "warning: %s\n", out);
fflush(stdout);
2013-09-30 19:37:13 -07:00
break;
case LOG_ERROR:
fprintf(stderr, "error: %s\n", out);
2014-04-05 13:09:25 -07:00
fflush(stderr);
2013-09-30 19:37:13 -07:00
}
}
UNUSED_PARAMETER(param);
2013-09-30 19:37:13 -07:00
}
#ifdef _MSC_VER
#define NORETURN __declspec(noreturn)
#else
#define NORETURN __attribute__((noreturn))
#endif
NORETURN static void def_crash_handler(const char *format, va_list args,
void *param)
2013-09-30 19:37:13 -07:00
{
vfprintf(stderr, format, args);
exit(0);
UNUSED_PARAMETER(param);
2013-09-30 19:37:13 -07:00
}
static log_handler_t log_handler = def_log_handler;
static void (*crash_handler)(const char *, va_list, void *) = def_crash_handler;
2013-09-30 19:37:13 -07:00
void base_get_log_handler(log_handler_t *handler, void **param)
{
if (handler)
*handler = log_handler;
if (param)
*param = log_param;
}
void base_set_log_handler(log_handler_t handler, void *param)
2013-09-30 19:37:13 -07:00
{
if (!handler)
handler = def_log_handler;
log_param = param;
2013-09-30 19:37:13 -07:00
log_handler = handler;
}
void base_set_crash_handler(
void (*handler)(const char *, va_list, void *),
void *param)
2013-09-30 19:37:13 -07:00
{
crash_param = param;
2013-09-30 19:37:13 -07:00
crash_handler = handler;
}
void bcrash(const char *format, ...)
{
va_list args;
if (crashing) {
fputs("Crashed in the crash handler", stderr);
exit(2);
}
crashing = 1;
va_start(args, format);
crash_handler(format, args, crash_param);
2013-09-30 19:37:13 -07:00
va_end(args);
}
void blogva(int log_level, const char *format, va_list args)
{
log_handler(log_level, format, args, log_param);
}
void blog(int log_level, const char *format, ...)
2013-09-30 19:37:13 -07:00
{
va_list args;
va_start(args, format);
blogva(log_level, format, args);
2013-09-30 19:37:13 -07:00
va_end(args);
}