2013-12-25 21:40:33 -08:00
|
|
|
/*
|
|
|
|
* 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"
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
static int crashing = 0;
|
|
|
|
static void *log_param = NULL;
|
2014-04-14 04:02:11 -07:00
|
|
|
static void *crash_param = NULL;
|
2013-09-30 19:37:13 -07:00
|
|
|
|
2019-06-22 22:13:45 -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);
|
|
|
|
|
2022-03-30 03:10:28 -07:00
|
|
|
switch (log_level) {
|
|
|
|
case LOG_DEBUG:
|
|
|
|
fprintf(stdout, "debug: %s\n", out);
|
|
|
|
fflush(stdout);
|
|
|
|
break;
|
2013-09-30 19:37:13 -07:00
|
|
|
|
2022-03-30 03:10:28 -07:00
|
|
|
case LOG_INFO:
|
|
|
|
fprintf(stdout, "info: %s\n", out);
|
|
|
|
fflush(stdout);
|
|
|
|
break;
|
2013-09-30 19:37:13 -07:00
|
|
|
|
2022-03-30 03:10:28 -07:00
|
|
|
case LOG_WARNING:
|
|
|
|
fprintf(stdout, "warning: %s\n", out);
|
|
|
|
fflush(stdout);
|
|
|
|
break;
|
2013-09-30 19:37:13 -07:00
|
|
|
|
2022-03-30 03:10:28 -07:00
|
|
|
case LOG_ERROR:
|
|
|
|
fprintf(stderr, "error: %s\n", out);
|
|
|
|
fflush(stderr);
|
2013-09-30 19:37:13 -07:00
|
|
|
}
|
2014-04-14 04:02:11 -07:00
|
|
|
|
|
|
|
UNUSED_PARAMETER(param);
|
2013-09-30 19:37:13 -07:00
|
|
|
}
|
|
|
|
|
2021-12-08 17:40:51 -08:00
|
|
|
OBS_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);
|
2014-04-14 04:02:11 -07:00
|
|
|
|
|
|
|
UNUSED_PARAMETER(param);
|
2013-09-30 19:37:13 -07:00
|
|
|
}
|
|
|
|
|
2014-05-14 17:43:23 -07:00
|
|
|
static log_handler_t log_handler = def_log_handler;
|
2014-04-14 04:02:11 -07:00
|
|
|
static void (*crash_handler)(const char *, va_list, void *) = def_crash_handler;
|
2013-09-30 19:37:13 -07:00
|
|
|
|
2014-05-14 17:43:23 -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
|
|
|
{
|
2014-04-14 04:02:11 -07:00
|
|
|
if (!handler)
|
|
|
|
handler = def_log_handler;
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
log_param = param;
|
2013-09-30 19:37:13 -07:00
|
|
|
log_handler = handler;
|
|
|
|
}
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
void base_set_crash_handler(void (*handler)(const char *, va_list, void *),
|
|
|
|
void *param)
|
2013-09-30 19:37:13 -07:00
|
|
|
{
|
2019-06-22 22:13:45 -07:00
|
|
|
crash_param = param;
|
2013-09-30 19:37:13 -07:00
|
|
|
crash_handler = handler;
|
|
|
|
}
|
|
|
|
|
2021-12-08 17:40:51 -08:00
|
|
|
OBS_NORETURN void bcrash(const char *format, ...)
|
2013-09-30 19:37:13 -07:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
if (crashing) {
|
|
|
|
fputs("Crashed in the crash handler", stderr);
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
crashing = 1;
|
|
|
|
va_start(args, format);
|
2014-04-14 04:02:11 -07:00
|
|
|
crash_handler(format, args, crash_param);
|
2013-09-30 19:37:13 -07:00
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2014-02-28 19:02:29 -08:00
|
|
|
void blogva(int log_level, const char *format, va_list args)
|
2014-02-07 02:03:54 -08:00
|
|
|
{
|
2014-04-14 04:02:11 -07:00
|
|
|
log_handler(log_level, format, args, log_param);
|
2014-02-07 02:03:54 -08:00
|
|
|
}
|
|
|
|
|
2014-02-28 19:02:29 -08:00
|
|
|
void blog(int log_level, const char *format, ...)
|
2013-09-30 19:37:13 -07:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, format);
|
2014-04-14 04:02:11 -07:00
|
|
|
blogva(log_level, format, args);
|
2013-09-30 19:37:13 -07:00
|
|
|
va_end(args);
|
|
|
|
}
|