orca/src/platform/native_debug.c

138 lines
3.4 KiB
C
Raw Normal View History

This commit restructures the codebase to melt the milepost platform layer into the main orca codebase. Here's a list of commits squashed in this update: - move angle files to ext/ and pull includes/libs from there when needed - remove milepost/ext/angle_headers - Collapsed milepost/ext into ext - Collapse milepost/scripts into scripts/ - collapse milepost/resources into resources/. WARN: this temporarily breaks milepost's native examples - collapse milepost/test into test/ - renamed test/ to tests/ - build milepost directly into build/bin - remove unused GLES and KHR folders from sdk/ - reorganizing milepost directory tree into app, graphics, platfrom, ui, util - collapse milepost/src to src/ - Move all native examples to sketches/ and remove milepost repo - Moving sketches resources into their own shared folder separate from the runtime's resource folder - Moving all binding code to src/wasmbind - Moving all binding code to src/wasmbind - pong: fix typo in error log - fixing log parameter order - add error logs to mg_image_create_* - Fix build scripts on windows - fixed include mistake after rebase - collapsing milepost.{h|c|m} to orca.{h|c|m} and moving from sdk/ to src/ - rename orca_app.h/main.c to runtime.h/c - collapsed sdk/ to src/ - put old sdk files into their respective dirs - renamed canvas_api.json to surface_api.json - moved all stb headers in ext/stb/ - remove unused OpenSansLatinSubset.ttf font - moving cstdlib to src/wasmlibc and removing some duplicates with src/platform - move libc stdarg and string functions from platform/ to wasmlibc/ - rename wasmlibc to libc-shim to reflect non-completeness - Expose abort/assert variadic functions and macros to orca apps, and forward libc-shim abort/assert to these - move Orca API runtime implementations to runtime_xxx - fix missing math constants when including math.h with msvc in graphics_common.c - Change name of runtime to orca_runtime. When bundling on Windows, change name of runtime executable to the name of the app.
2023-08-09 11:06:32 +00:00
/************************************************************//**
*
* @file: native_debug.c
* @author: Martin Fouilleul
* @date: 13/08/2023
*
*****************************************************************/
#include<stdio.h>
#include"app/mp_app.h"
#include"platform_debug.c"
//----------------------------------------------------------------
// Logging
//----------------------------------------------------------------
#if PLATFORM_WINDOWS
#include<io.h>
#define isatty _isatty
#define fileno _fileno
#elif PLATFORM_MACOS || PLATFORM_LINUX
#include<unistd.h>
#endif
static const char* LOG_HEADINGS[LOG_LEVEL_COUNT] = {
"Error",
"Warning",
"Info"};
static const char* LOG_FORMATS[LOG_LEVEL_COUNT] = {
"\033[38;5;9m\033[1m",
"\033[38;5;13m\033[1m",
"\033[38;5;10m\033[1m"};
static const char* LOG_FORMAT_STOP = "\033[m";
typedef struct log_output
{
FILE* f;
} log_output;
static log_output _logDefaultOutput = {0};
log_output* LOG_DEFAULT_OUTPUT = &_logDefaultOutput;
void platform_log_push(log_output* output,
log_level level,
const char* file,
const char* function,
int line,
const char* fmt,
va_list ap)
{
if(output == 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: ",
LOG_FORMATS[level],
LOG_HEADINGS[level],
LOG_FORMAT_STOP,
function,
file,
line);
}
else
{
fprintf(output->f,
"%s: %s() in %s:%i: ",
LOG_HEADINGS[level],
function,
file,
line);
}
vfprintf(output->f, fmt, ap);
}
//----------------------------------------------------------------
// Assert/Abort
//----------------------------------------------------------------
_Noreturn void orca_abort(const char* file, const char* function, int line, const char* fmt, ...)
{
mem_arena* scratch = mem_scratch();
va_list ap;
va_start(ap, fmt);
str8 note = str8_pushfv(scratch, fmt, ap);
va_end(ap);
str8 msg = str8_pushf(scratch,
"Fatal error in function %s() in file \"%s\", line %i:\n%.*s\n",
function,
file,
line,
(int)note.len,
note.ptr);
const char* msgCStr = str8_to_cstring(scratch, msg);
log_error(msgCStr);
const char* options[] = {"OK"};
mp_alert_popup("Fatal Error", msgCStr, 1, options);
//TODO: could terminate more gracefully?
exit(-1);
}
_Noreturn void orca_assert_fail(const char* file, const char* function, int line, const char* src, const char* fmt, ...)
{
mem_arena* scratch = mem_scratch();
va_list ap;
va_start(ap, fmt);
str8 note = str8_pushfv(scratch, fmt, ap);
va_end(ap);
str8 msg = str8_pushf(scratch,
"Assertion failed in function %s() in file \"%s\", line %i:\n%s\nNote: %.*s\n",
function,
file,
line,
src,
str8_ip(note));
const char* msgCStr = str8_to_cstring(scratch, msg);
log_error(msgCStr);
const char* options[] = {"OK"};
mp_alert_popup("Assertion Failed", msgCStr, 1, options);
//TODO: could terminate more gracefully?
exit(-1);
}