2023-08-19 12:49:23 +00:00
|
|
|
/************************************************************/ /**
|
2023-08-09 11:06:32 +00:00
|
|
|
*
|
|
|
|
* @file: native_debug.c
|
|
|
|
* @author: Martin Fouilleul
|
|
|
|
* @date: 13/08/2023
|
|
|
|
*
|
|
|
|
*****************************************************************/
|
2023-08-19 12:49:23 +00:00
|
|
|
#include <stdio.h>
|
2023-08-09 11:06:32 +00:00
|
|
|
|
2023-08-19 12:49:23 +00:00
|
|
|
#include "app/app.h"
|
|
|
|
#include "platform_debug.c"
|
2023-08-09 11:06:32 +00:00
|
|
|
//----------------------------------------------------------------
|
|
|
|
// Logging
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2023-08-14 08:26:11 +00:00
|
|
|
#if OC_PLATFORM_WINDOWS
|
2023-08-19 12:49:23 +00:00
|
|
|
#include <io.h>
|
|
|
|
#define isatty _isatty
|
|
|
|
#define fileno _fileno
|
2023-08-14 08:26:11 +00:00
|
|
|
#elif OC_PLATFORM_MACOS || PLATFORM_LINUX
|
2023-08-19 12:49:23 +00:00
|
|
|
#include <unistd.h>
|
2023-08-09 11:06:32 +00:00
|
|
|
#endif
|
|
|
|
|
2023-08-14 08:26:11 +00:00
|
|
|
static const char* OC_LOG_HEADINGS[OC_LOG_LEVEL_COUNT] = {
|
2023-08-19 12:49:23 +00:00
|
|
|
"Error",
|
|
|
|
"Warning",
|
|
|
|
"Info"
|
|
|
|
};
|
2023-08-09 11:06:32 +00:00
|
|
|
|
2023-08-14 08:26:11 +00:00
|
|
|
static const char* OC_LOG_FORMATS[OC_LOG_LEVEL_COUNT] = {
|
2023-08-19 12:49:23 +00:00
|
|
|
"\033[38;5;9m\033[1m",
|
|
|
|
"\033[38;5;13m\033[1m",
|
|
|
|
"\033[38;5;10m\033[1m"
|
|
|
|
};
|
2023-08-09 11:06:32 +00:00
|
|
|
|
2023-08-14 08:26:11 +00:00
|
|
|
static const char* OC_LOG_FORMAT_STOP = "\033[m";
|
2023-08-09 11:06:32 +00:00
|
|
|
|
2023-08-14 08:26:11 +00:00
|
|
|
typedef struct oc_log_output
|
2023-08-09 11:06:32 +00:00
|
|
|
{
|
2023-08-19 12:49:23 +00:00
|
|
|
FILE* f;
|
2023-08-14 08:26:11 +00:00
|
|
|
} oc_log_output;
|
2023-08-09 11:06:32 +00:00
|
|
|
|
2023-08-19 12:49:23 +00:00
|
|
|
static oc_log_output oc_logDefaultOutput = { 0 };
|
2023-08-14 08:26:11 +00:00
|
|
|
oc_log_output* OC_LOG_DEFAULT_OUTPUT = &oc_logDefaultOutput;
|
2023-08-09 11:06:32 +00:00
|
|
|
|
2023-08-14 08:26:11 +00:00
|
|
|
void platform_log_push(oc_log_output* output,
|
|
|
|
oc_log_level level,
|
2023-08-09 11:06:32 +00:00
|
|
|
const char* file,
|
|
|
|
const char* function,
|
|
|
|
int line,
|
|
|
|
const char* fmt,
|
|
|
|
va_list ap)
|
|
|
|
{
|
2023-08-19 12:49:23 +00:00
|
|
|
if(output == OC_LOG_DEFAULT_OUTPUT && output->f == 0)
|
|
|
|
{
|
|
|
|
output->f = stdout;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fd = fileno(output->f);
|
|
|
|
if(isatty(fd))
|
|
|
|
{
|
|
|
|
fprintf(output->f,
|
|
|
|
"%s%s:%s %s() in %s:%i: ",
|
|
|
|
OC_LOG_FORMATS[level],
|
|
|
|
OC_LOG_HEADINGS[level],
|
|
|
|
OC_LOG_FORMAT_STOP,
|
|
|
|
function,
|
|
|
|
file,
|
|
|
|
line);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(output->f,
|
|
|
|
"%s: %s() in %s:%i: ",
|
|
|
|
OC_LOG_HEADINGS[level],
|
|
|
|
function,
|
|
|
|
file,
|
|
|
|
line);
|
|
|
|
}
|
|
|
|
vfprintf(output->f, fmt, ap);
|
2023-08-09 11:06:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
// Assert/Abort
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2023-08-14 08:26:11 +00:00
|
|
|
_Noreturn void oc_abort_ext(const char* file, const char* function, int line, const char* fmt, ...)
|
2023-08-09 11:06:32 +00:00
|
|
|
{
|
2023-08-19 12:49:23 +00:00
|
|
|
oc_arena* scratch = oc_scratch();
|
2023-08-09 11:06:32 +00:00
|
|
|
|
2023-08-19 12:49:23 +00:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
oc_str8 note = oc_str8_pushfv(scratch, fmt, ap);
|
|
|
|
va_end(ap);
|
2023-08-09 11:06:32 +00:00
|
|
|
|
2023-08-19 12:49:23 +00:00
|
|
|
oc_str8 msg = oc_str8_pushf(scratch,
|
|
|
|
"Fatal error in function %s() in file \"%s\", line %i:\n%.*s\n",
|
|
|
|
function,
|
|
|
|
file,
|
|
|
|
line,
|
|
|
|
(int)note.len,
|
|
|
|
note.ptr);
|
2023-08-09 11:06:32 +00:00
|
|
|
|
2023-08-19 12:49:23 +00:00
|
|
|
oc_log_error(msg.ptr);
|
2023-08-09 11:06:32 +00:00
|
|
|
|
2023-08-19 12:49:23 +00:00
|
|
|
oc_str8_list options = { 0 };
|
|
|
|
oc_str8_list_push(scratch, &options, OC_STR8("OK"));
|
2023-08-14 08:26:11 +00:00
|
|
|
|
2023-08-19 12:49:23 +00:00
|
|
|
oc_alert_popup(OC_STR8("Fatal Error"), msg, options);
|
2023-08-09 11:06:32 +00:00
|
|
|
|
2023-08-19 12:49:23 +00:00
|
|
|
//TODO: could terminate more gracefully?
|
|
|
|
exit(-1);
|
2023-08-09 11:06:32 +00:00
|
|
|
}
|
|
|
|
|
2023-08-14 08:26:11 +00:00
|
|
|
_Noreturn void oc_assert_fail(const char* file, const char* function, int line, const char* src, const char* fmt, ...)
|
2023-08-09 11:06:32 +00:00
|
|
|
{
|
2023-08-19 12:49:23 +00:00
|
|
|
oc_arena* scratch = oc_scratch();
|
2023-08-09 11:06:32 +00:00
|
|
|
|
2023-08-19 12:49:23 +00:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
oc_str8 note = oc_str8_pushfv(scratch, fmt, ap);
|
|
|
|
va_end(ap);
|
2023-08-09 11:06:32 +00:00
|
|
|
|
2023-08-19 12:49:23 +00:00
|
|
|
oc_str8 msg = oc_str8_pushf(scratch,
|
|
|
|
"Assertion failed in function %s() in file \"%s\", line %i:\n%s\nNote: %.*s\n",
|
|
|
|
function,
|
|
|
|
file,
|
|
|
|
line,
|
|
|
|
src,
|
|
|
|
oc_str8_ip(note));
|
2023-08-14 08:26:11 +00:00
|
|
|
|
2023-08-19 12:49:23 +00:00
|
|
|
oc_log_error(msg.ptr);
|
2023-08-09 11:06:32 +00:00
|
|
|
|
2023-08-19 12:49:23 +00:00
|
|
|
oc_str8_list options = { 0 };
|
|
|
|
oc_str8_list_push(scratch, &options, OC_STR8("OK"));
|
2023-08-09 11:06:32 +00:00
|
|
|
|
2023-08-19 12:49:23 +00:00
|
|
|
oc_alert_popup(OC_STR8("Assertion Failed"), msg, options);
|
2023-08-09 11:06:32 +00:00
|
|
|
|
2023-08-19 12:49:23 +00:00
|
|
|
//TODO: could terminate more gracefully?
|
|
|
|
exit(-1);
|
2023-08-09 11:06:32 +00:00
|
|
|
}
|