abstracting logging for orca

This commit is contained in:
Martin Fouilleul 2023-04-18 18:05:54 +02:00
parent 86a16d3775
commit 794f47b141
31 changed files with 2377 additions and 287 deletions

View File

@ -27,7 +27,7 @@ mg_font create_font()
FILE* fontFile = fopen(fontPathCString, "r");
if(!fontFile)
{
LOG_ERROR("Could not load font file '%s': %s\n", fontPathCString, strerror(errno));
log_error("Could not load font file '%s': %s\n", fontPathCString, strerror(errno));
return(mg_font_nil());
}
unsigned char* fontData = 0;

View File

@ -69,7 +69,7 @@ mg_font create_font(const char* path)
FILE* fontFile = fopen(fontPathCString, "r");
if(!fontFile)
{
LOG_ERROR("Could not load font file '%s'\n", fontPathCString);
log_error("Could not load font file '%s'\n", fontPathCString);
return(mg_font_nil());
}
unsigned char* fontData = 0;

View File

@ -29,7 +29,7 @@ mg_font create_font()
FILE* fontFile = fopen(fontPathCString, "r");
if(!fontFile)
{
LOG_ERROR("Could not load font file '%s': %s\n", fontPathCString, strerror(errno));
log_error("Could not load font file '%s': %s\n", fontPathCString, strerror(errno));
return(mg_font_nil());
}
unsigned char* fontData = 0;

View File

@ -52,7 +52,7 @@ int main()
free(fontPath);
if(!fontFile)
{
LOG_ERROR("Could not load font file '%s'\n", fontPath);
log_error("Could not load font file '%s'\n", fontPath);
return(-1);
}
unsigned char* fontData = 0;

View File

@ -28,7 +28,7 @@ mg_font create_font()
FILE* fontFile = fopen(fontPathCString, "r");
if(!fontFile)
{
LOG_ERROR("Could not load font file '%s': %s\n", fontPathCString, strerror(errno));
log_error("Could not load font file '%s': %s\n", fontPathCString, strerror(errno));
return(mg_font_nil());
}
unsigned char* fontData = 0;

View File

@ -159,7 +159,7 @@ mg_font create_font()
FILE* fontFile = fopen(fontPathCString, "r");
if(!fontFile)
{
LOG_ERROR("Could not load font file '%s': %s\n", fontPathCString, strerror(errno));
log_error("Could not load font file '%s': %s\n", fontPathCString, strerror(errno));
return(mg_font_nil());
}
unsigned char* fontData = 0;

1906
ext/stb_sprintf.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -349,7 +349,7 @@ static int mg_gl_canvas_compile_compute_program_named(const char* name, const ch
char buffer[256];
int size = 0;
glGetProgramInfoLog(program, 256, &size, buffer);
LOG_ERROR("Shader link error (%s): %.*s\n", name, size, buffer);
log_error("Shader link error (%s): %.*s\n", name, size, buffer);
res = -1;
}
@ -391,7 +391,7 @@ int mg_gl_canvas_compile_render_program_named(const char* progName,
char buffer[256];
int size = 0;
glGetProgramInfoLog(program, 256, &size, buffer);
LOG_ERROR("Shader link error (%s): %.*s\n", progName, size, buffer);
log_error("Shader link error (%s): %.*s\n", progName, size, buffer);
res = -1;
}
else

View File

@ -16,7 +16,7 @@
#define STB_IMAGE_IMPLEMENTATION
#include"stb_image.h"
#include"debug_log.h"
#include"platform_log.h"
#include"graphics_internal.h"
typedef struct mg_glyph_map_entry
@ -618,7 +618,7 @@ void mg_matrix_stack_push(mg_canvas_data* canvas, mg_mat2x3 transform)
{
if(canvas->matrixStackSize >= MG_MATRIX_STACK_MAX_DEPTH)
{
LOG_ERROR("matrix stack overflow\n");
log_error("matrix stack overflow\n");
}
else
{
@ -631,7 +631,7 @@ void mg_matrix_stack_pop(mg_canvas_data* canvas)
{
if(canvas->matrixStackSize == 0)
{
LOG_ERROR("matrix stack underflow\n");
log_error("matrix stack underflow\n");
}
else
{
@ -656,7 +656,7 @@ void mg_clip_stack_push(mg_canvas_data* canvas, mp_rect clip)
{
if(canvas->clipStackSize >= MG_CLIP_STACK_MAX_DEPTH)
{
LOG_ERROR("clip stack overflow\n");
log_error("clip stack overflow\n");
}
else
{
@ -669,7 +669,7 @@ void mg_clip_stack_pop(mg_canvas_data* canvas)
{
if(canvas->clipStackSize == 0)
{
LOG_ERROR("clip stack underflow\n");
log_error("clip stack underflow\n");
}
else
{
@ -2772,7 +2772,7 @@ void mg_flush_commands(int primitiveCount, mg_primitive* primitives, mg_path_elt
{
if(nextIndex >= primitiveCount)
{
LOG_ERROR("invalid location '%i' in graphics command buffer would cause an overrun\n", nextIndex);
log_error("invalid location '%i' in graphics command buffer would cause an overrun\n", nextIndex);
break;
}
mg_primitive* primitive = &(primitives[nextIndex]);
@ -2812,7 +2812,7 @@ void mg_flush_commands(int primitiveCount, mg_primitive* primitives, mg_path_elt
}
else if(primitive->jump >= primitiveCount)
{
LOG_ERROR("invalid jump location '%i' in graphics command buffer\n", primitive->jump);
log_error("invalid jump location '%i' in graphics command buffer\n", primitive->jump);
goto exit_command_loop;
}
else
@ -3231,7 +3231,7 @@ mp_rect mg_glyph_outlines_from_font_data(mg_font_data* fontData, str32 glyphIndi
if(!glyphIndex || glyphIndex >= fontData->glyphCount)
{
LOG_WARNING("code point is not present in font ranges\n");
log_warning("code point is not present in font ranges\n");
//NOTE(martin): try to find the replacement character
glyphIndex = mg_font_get_glyph_index_from_font_data(fontData, 0xfffd);
if(!glyphIndex)

View File

@ -7,21 +7,13 @@
*
*****************************************************************/
//---------------------------------------------------------------
// utilities implementations
//---------------------------------------------------------------
#include"util/debug_log.c"
#include"util/memory.c"
#include"util/strings.c"
#include"util/utf8.c"
#include"util/hash.c"
#include"util/ringbuffer.c"
//---------------------------------------------------------------
// platform implementations
//---------------------------------------------------------------
#include"platform.h"
#include"platform/std_log.c"
#if defined(PLATFORM_WIN64)
#include"platform/win32_memory.c"
#include"platform/win32_clock.c"
@ -47,6 +39,15 @@
#error "Unsupported platform"
#endif
//---------------------------------------------------------------
// utilities implementations
//---------------------------------------------------------------
#include"util/memory.c"
#include"util/strings.c"
#include"util/utf8.c"
#include"util/hash.c"
#include"util/ringbuffer.c"
//---------------------------------------------------------------
// app/graphics layer
//---------------------------------------------------------------

View File

@ -15,7 +15,7 @@
#include"platform.h"
#include"typedefs.h"
#include"macro_helpers.h"
#include"debug_log.h"
#include"platform_log.h"
#include"lists.h"
#include"memory.h"
#include"strings.h"

View File

@ -99,7 +99,7 @@ void mp_queue_event(mp_event* event)
{
if(ringbuffer_write_available(&__mpApp.eventQueue) < sizeof(mp_event))
{
LOG_ERROR("event queue full\n");
log_error("event queue full\n");
}
else
{
@ -223,7 +223,7 @@ static void mp_update_text(utf32 codepoint)
}
else
{
LOG_WARNING("too many input codepoints per frame, dropping input");
log_warning("too many input codepoints per frame, dropping input");
}
}

View File

@ -80,7 +80,7 @@ void mg_mtl_print_log(int bufferIndex, id<MTLBuffer> logBuffer, id<MTLBuffer> lo
if(size)
{
LOG_INFO("Log from buffer %i:\n", bufferIndex);
log_info("Log from buffer %i:\n", bufferIndex);
int index = 0;
while(index < size)
@ -1160,7 +1160,7 @@ mg_canvas_backend* mg_mtl_canvas_create(mg_surface surface)
if(err != nil)
{
const char* errStr = [[err localizedDescription] UTF8String];
LOG_ERROR("error : %s\n", errStr);
log_error("error : %s\n", errStr);
return(0);
}
id<MTLFunction> pathFunction = [library newFunctionWithName:@"mtl_path_setup"];

View File

@ -16,7 +16,7 @@
#include"ringbuffer.h"
#include"memory.h"
#include"macro_helpers.h"
#include"debug_log.h"
#include"platform_log.h"
#include"platform_clock.h"
#include"graphics_internal.h"
@ -269,14 +269,14 @@ static void mp_update_keyboard_layout()
__mpApp.osx.kbLayoutInputSource = TISCopyCurrentKeyboardLayoutInputSource();
if(!__mpApp.osx.kbLayoutInputSource)
{
LOG_ERROR("Failed to load keyboard layout input source");
log_error("Failed to load keyboard layout input source");
}
__mpApp.osx.kbLayoutUnicodeData = TISGetInputSourceProperty(__mpApp.osx.kbLayoutInputSource,
kTISPropertyUnicodeKeyLayoutData);
if(!__mpApp.osx.kbLayoutUnicodeData)
{
LOG_ERROR("Failed to load keyboard layout unicode data");
log_error("Failed to load keyboard layout unicode data");
}
memset(__mpApp.keyLabels, 0, sizeof(mp_key_utf8)*MP_KEY_COUNT);
@ -1352,7 +1352,7 @@ mp_window mp_window_create(mp_rect contentRect, const char* title, mp_window_sty
mp_window_data* window = mp_window_alloc();
if(!window)
{
LOG_ERROR("Could not allocate window data\n");
log_error("Could not allocate window data\n");
return((mp_window){0});
}
@ -1833,14 +1833,14 @@ mp_view mp_view_create(mp_window windowHandle, mp_rect frame)
mp_window_data* window = mp_window_ptr_from_handle(windowHandle);
if(!window)
{
LOG_ERROR("Can't create view for nil window\n");
log_error("Can't create view for nil window\n");
return(mp_view_nil());
}
mp_view_data* view = mp_view_alloc();
if(!view)
{
LOG_ERROR("Could not allocate view data\n");
log_error("Could not allocate view data\n");
return(mp_view_nil());
}

74
src/platform/orca_log.c Normal file
View File

@ -0,0 +1,74 @@
/************************************************************//**
*
* @file: orca_log.c
* @author: Martin Fouilleul
* @date: 18/04/2023
*
*****************************************************************/
#include"platform_log.c"
#include"util/memory.h"
#include"util/strings.h"
typedef enum
{
ORCA_LOG_OUTPUT_CONSOLE,
ORCA_LOG_OUTPUT_FILE
} orca_log_output_kind;
typedef struct log_output
{
orca_log_output_kind kind;
//TODO: file output
} log_output;
static log_output _logDefaultOutput = {.kind = ORCA_LOG_OUTPUT_CONSOLE};
log_output* LOG_DEFAULT_OUTPUT = &_logDefaultOutput;
extern void orca_log_entry(log_level level,
int fileLen,
const char* file,
int functionLen,
const char* function,
int line,
int msgLen,
const char* msg);
typedef struct orca_log_context
{
mem_arena* arena;
str8_list list;
} orca_log_context;
char* log_stbsp_callback(char const* buf, void* user, int len)
{
orca_log_context* ctx = (orca_log_context*)user;
str8 string = str8_push_buffer(ctx->arena, len, (char*)buf);
str8_list_push(ctx->arena, &ctx->list, string);
return((char*)buf);
}
void platform_log_entry(log_output* output,
log_level level,
str8 function,
str8 file,
int line,
const char* fmt,
va_list ap)
{
mem_arena* scratch = mem_scratch();
mem_arena_marker marker = mem_arena_mark(scratch);
orca_log_context ctx = {.arena = scratch,
.list = {0}};
char buf[STB_SPRINTF_MIN];
stbsp_vsprintfcb(log_stbsp_callback, &ctx, buf, fmt, ap);
str8 string = str8_list_join(scratch, ctx.list);
orca_log_entry(level, str8_ip(function), str8_ip(file), line, str8_ip(string));
mem_arena_clear_to(scratch, marker);
}

101
src/platform/orca_strings.c Normal file
View File

@ -0,0 +1,101 @@
/************************************************************//**
*
* @file: orca_strings.c
* @author: Martin Fouilleul
* @date: 18/04/2023
*
*****************************************************************/
#include"platform_strings.h"
#define STB_SPRINTF_IMPLEMENTATION
#include"ext/stb_sprintf.h"
size_t strlen(const char *s)
{
size_t len = 0;
while(s[len] != '\0')
{
len++;
}
return(len);
}
int strcmp(const char *s1, const char *s2)
{
size_t i = 0;
while(s1[i] != '\0' && s1[i] == s2[i])
{
i++;
}
int res = 0;
if(s1[i] != s2[i])
{
if(s1[i] == '\0')
{
res = -1;
}
else if(s2[i] == '\0')
{
res = 1;
}
else
{
res = (unsigned char)s1[i] - (unsigned char)s2[i];
}
}
return(res);
}
int strncmp(const char *s1, const char *s2, size_t n)
{
size_t i = 0;
while(i < n && s1[i] != '\0' && s1[i] == s2[i])
{
i++;
}
int res = 0;
if(s1[i] != s2[i])
{
if(s1[i] == '\0')
{
res = -1;
}
else if(s2[i] == '\0')
{
res = 1;
}
else
{
res = (unsigned char)s1[i] - (unsigned char)s2[i];
}
}
return(res);
}
char* strcpy(char *restrict s1, const char *restrict s2)
{
size_t i = 0;
while(s2[i] != '\0')
{
s1[i] = s2[i];
i++;
}
s1[i] = '\0';
return(s1);
}
char* strncpy(char *restrict s1, const char *restrict s2, size_t len)
{
size_t i = 0;
while(i < len && s2[i] != '\0')
{
s1[i] = s2[i];
i++;
}
while(i < len)
{
s1[i] = 0;
i++;
}
return(s1);
}

View File

@ -78,7 +78,7 @@ void mp_clock_init()
if(sysctl(mib, 2, &tv, &size, 0, 0) == -1)
{
LOG_ERROR("can't read boot time\n");
log_error("can't read boot time\n");
}
//NOTE(martin): convert boot date to timestamp
__initialTimestamp__ = (((u64)tv.tv_sec + CLK_JAN_1970) << 32)

View File

@ -0,0 +1,56 @@
/************************************************************//**
*
* @file: platform_log.c
* @author: Martin Fouilleul
* @date: 18/04/2023
*
*****************************************************************/
#include"platform_log.h"
typedef struct log_config
{
log_output* output;
log_level level;
} log_config;
//TODO: make default output a compile-time constant to avoid check in log_entry()?
static log_config __logConfig = {0, LOG_LEVEL_INFO};
void log_set_output(log_output* output)
{
__logConfig.output = output;
}
void log_set_level(log_level level)
{
__logConfig.level = level;
}
void platform_log_entry(log_output* output,
log_level level,
str8 function,
str8 file,
int line,
const char* fmt,
va_list ap);
void log_entry(log_level level,
str8 function,
str8 file,
int line,
const char* fmt,
...)
{
if(!__logConfig.output)
{
__logConfig.output = LOG_DEFAULT_OUTPUT;
}
if(level <= __logConfig.level)
{
va_list ap;
va_start(ap, fmt);
platform_log_entry(__logConfig.output, level, function, file, line, fmt, ap);
va_end(ap);
}
}

View File

@ -0,0 +1,58 @@
/************************************************************//**
*
* @file: platform_log.h
* @author: Martin Fouilleul
* @date: 18/04/2023
*
*****************************************************************/
#ifndef __PLATFORM_LOG_H_
#define __PLATFORM_LOG_H_
#include"platform.h"
#include"strings.h"
typedef enum { LOG_LEVEL_ERROR,
LOG_LEVEL_WARNING,
LOG_LEVEL_INFO,
LOG_LEVEL_COUNT } log_level;
typedef struct log_output log_output;
extern log_output* LOG_DEFAULT_OUTPUT;
MP_API void log_set_level(log_level level);
MP_API void log_set_output(log_output* output);
MP_API void log_entry(log_level level,
str8 function,
str8 file,
int line,
const char* fmt,
...);
#define log_generic(level, msg, ...) log_entry(level, STR8(__FUNCTION__), STR8(__FILE__), __LINE__, msg, ##__VA_ARGS__)
#define log_error(msg, ...) log_generic(LOG_LEVEL_ERROR, msg, ##__VA_ARGS__)
#ifndef LOG_COMPILE_WARNING
#define LOG_COMPILE_WARNING 1
#endif
#ifndef LOG_COMPILE_INFO
#define LOG_COMPILE_INFO 1
#endif
#if LOG_COMPILE_WARNING || LOG_COMPILE_INFO
#define log_warning(msg, ...) log_generic(LOG_LEVEL_WARNING, msg, ##__VA_ARGS__)
#if LOG_COMPILE_INFO
#define log_info(msg, ...) log_generic(LOG_LEVEL_INFO, msg, ##__VA_ARGS__ )
#else
#define log_info(msg, ...)
#endif
#else
#define log_warning(msg, ...)
#define log_info(msg, ...)
#endif
#endif //__PLATFORM_LOG_H_

View File

@ -0,0 +1,28 @@
/************************************************************//**
*
* @file: platform_strings.h
* @author: Martin Fouilleul
* @date: 18/04/2023
*
*****************************************************************/
#ifndef __PLATFORM_STRINGS_H_
#define __PLATFORM_STRINGS_H_
#include"platform.h"
#include"platform_varg.h"
#if PLATFORM_ORCA
#include"ext/stb_sprintf.h"
size_t strlen(const char *s);
int strcmp(const char *s1, const char *s2);
char* strcpy(char *restrict s1, const char *restrict s2);
#define vsnprintf stbsp_vsnprintf
#else
#include<string.h>
#include<stdio.h>
#endif
#endif //__PLATFORM_STRINGS_H_

View File

@ -11,6 +11,7 @@
#include"platform.h"
#if PLATFORM_ORCA
#define va_list __builtin_va_list
#define va_start __builtin_va_start
#define va_arg __builtin_va_arg
#define va_copy __builtin_va_copy

View File

@ -17,7 +17,7 @@
#include<stdlib.h> // malloc()/free()
#include"platform_socket.h"
#include"debug_log.h"
#include"platform_log.h"
typedef struct in_addr in_addr;
typedef struct sockaddr_in sockaddr_in;
@ -457,19 +457,19 @@ net_ip SocketGetDefaultExternalIP()
platform_socket* sock = SocketOpen(SOCK_UDP);
if(!sock)
{
LOG_ERROR("can't create socket");
log_error("can't create socket");
return(0);
}
if(SocketConnect(sock, &addr) != 0)
{
LOG_ERROR("can't connect socket: %s\n", SocketGetLastErrorMessage());
LOG_WARNING("try loopback interface\n");
log_error("can't connect socket: %s\n", SocketGetLastErrorMessage());
log_warning("try loopback interface\n");
addr.ip = HostToNetIP(SOCK_IP_LOOPBACK);
if(SocketConnect(sock, &addr) != 0)
{
LOG_ERROR("can't connect socket: %s\n", SocketGetLastErrorMessage());
log_error("can't connect socket: %s\n", SocketGetLastErrorMessage());
SocketClose(sock);
return(0);
}

74
src/platform/std_log.c Normal file
View File

@ -0,0 +1,74 @@
/************************************************************//**
*
* @file: std_log.c
* @author: Martin Fouilleul
* @date: 18/04/2023
*
*****************************************************************/
#include<stdio.h>
#include"platform_log.c"
#if PLATFORM_WIN32 || PLATFORM_WIN64
#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_entry(log_output* output,
log_level level,
str8 function,
str8 file,
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,
str8_ip(function),
str8_ip(file),
line);
}
else
{
fprintf(output->f,
"%s: %.*s() in %.*s:%i: ",
LOG_HEADINGS[level],
str8_ip(function),
str8_ip(file),
line);
}
vfprintf(output->f, fmt, ap);
}

View File

@ -10,7 +10,7 @@
#include<stdio.h>
#include<stdlib.h>
#include"debug_log.h"
#include"platform_log.h"
#include"typedefs.h"
int RandomSeedFromDevice()
@ -18,7 +18,7 @@ int RandomSeedFromDevice()
FILE* urandom = fopen("/dev/urandom", "r");
if(!urandom)
{
LOG_ERROR("can't open /dev/urandom\n");
log_error("can't open /dev/urandom\n");
return(-1);
}
@ -31,7 +31,7 @@ int RandomSeedFromDevice()
int size = fread(seed.buff, 1, 4, urandom);
if(size != 4)
{
LOG_ERROR("couldn't read from /dev/urandom\n");
log_error("couldn't read from /dev/urandom\n");
return(-1);
}

View File

@ -126,7 +126,7 @@ void ui_stack_pop(ui_stack_elt** stack)
}
else
{
LOG_ERROR("ui stack underflow\n");
log_error("ui stack underflow\n");
}
}
@ -470,7 +470,7 @@ ui_box* ui_box_make_str8(str8 string, ui_flags flags)
else
{
//maybe this should be a warning that we're trying to make the box twice in the same frame?
LOG_WARNING("trying to make ui box '%.*s' multiple times in the same frame\n", (int)box->string.len, box->string.ptr);
log_warning("trying to make ui box '%.*s' multiple times in the same frame\n", (int)box->string.len, box->string.ptr);
}
//NOTE: setup per-frame state

View File

@ -1,156 +0,0 @@
/************************************************************//**
*
* @file: debug_log.c
* @author: Martin Fouilleul
* @date: 22/10/2020
* @revision:
*
*****************************************************************/
#include"debug_log.h"
#include"platform_varg.h"
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_config
{
log_output output;
log_level level;
bool enableVTColor;
} log_config;
#if PLATFORM_ORCA
#define LOG_DEFAULT_OUTPUT (log_output){.kind = ORCA_LOG_OUTPUT_CONSOLE}
#define LOG_DEFAULT_ENABLE_VT_COLOR true
#else
#define LOG_DEFAULT_OUTPUT 0
#define LOG_DEFAULT_ENABLE_VT_COLOR true
#endif
static log_config __logConfig = {.output = LOG_DEFAULT_OUTPUT,
.level = LOG_DEFAULT_LEVEL,
.enableVTColor = LOG_DEFAULT_ENABLE_VT_COLOR};
void log_set_output(log_output output)
{
__logConfig.output = output;
}
void log_set_level(log_level level)
{
__logConfig.level = level;
}
void log_enable_vt_color(bool enable)
{
__logConfig.enableVTColor = enable;
}
#if PLATFORM_ORCA
#define STB_SPRINTF_IMPLEMENTATION
#include"ext/stb_sprintf.h"
typedef int orca_log_mode;
enum {ORCA_LOG_BEGIN, ORCA_LOG_APPEND};
extern void orca_log_entry(log_level level,
int fileLen,
const char* file,
int functionLen,
const char* function,
int line);
extern void orca_log_append(int msgLen, const char* msg);
char* log_stbsp_callback(char const* buf, void* user, int len)
{
orca_log_append(len, buf);
return((char*)buf);
}
//TODO: later, move this to orca_strings in milepost
size_t strlen(const char *s)
{
size_t len = 0;
while(s[len] != '\0')
{
len++;
}
return(len);
}
void log_generic(log_level level,
const char* functionName,
const char* fileName,
u32 line,
const char* msg,
...)
{
if(level <= __logConfig.level)
{
orca_log_entry(level, strlen(functionName), functionName, strlen(fileName), fileName, line);
char buf[STB_SPRINTF_MIN];
va_list ap;
va_start(ap, msg);
stbsp_vsprintfcb(log_stbsp_callback, 0, buf, msg, ap);
va_end(ap);
}
}
#else
void log_generic(log_level level,
const char* functionName,
const char* fileName,
u32 line,
const char* msg,
...)
{
if(__logConfig.output == 0)
{
__logConfig.output = stdout;
}
if(level <= __logConfig.level)
{
if(__logConfig.enableVTColor)
{
fprintf(__logConfig.output,
"%s%s:%s %s() in %s:%i: ",
LOG_FORMATS[level],
LOG_HEADINGS[level],
LOG_FORMAT_STOP,
functionName,
fileName,
line);
}
else
{
fprintf(__logConfig.output,
"%s: %s() in %s:%i: ",
LOG_HEADINGS[level],
functionName,
fileName,
line);
}
va_list ap;
va_start(ap, msg);
vfprintf(__logConfig.output, msg, ap);
va_end(ap);
}
}
#endif

View File

@ -1,80 +0,0 @@
/************************************************************//**
*
* @file: debug_log.h
* @author: Martin Fouilleul
* @date: 05/04/2019
* @revision:
*
*****************************************************************/
#ifndef __DEBUG_LOG_H_
#define __DEBUG_LOG_H_
#include"platform.h"
#include"typedefs.h"
#include"macro_helpers.h"
#ifdef __cplusplus
extern "C" {
#endif
//NOTE(martin): the default logging level can be adjusted by defining LOG_DEFAULT_LEVEL.
// It can be adjusted at runtime with log_set_level()
#ifndef LOG_DEFAULT_LEVEL
#define LOG_DEFAULT_LEVEL LOG_LEVEL_WARNING
#endif
typedef enum { LOG_LEVEL_ERROR,
LOG_LEVEL_WARNING,
LOG_LEVEL_INFO,
LOG_LEVEL_COUNT } log_level;
#ifdef PLATFORM_ORCA
typedef enum
{
ORCA_LOG_OUTPUT_CONSOLE,
ORCA_LOG_OUTPUT_FILE
} orca_log_output_kind;
typedef struct log_output
{
orca_log_output_kind kind;
//TODO: file
} log_output;
#else
#include<stdio.h>
typedef FILE* log_output;
#endif
MP_API void log_set_level(log_level level);
MP_API void log_set_output(log_output output);
MP_API void log_enable_vt_color(bool enable);
MP_API void log_generic(log_level level,
const char* functionName,
const char* fileName,
u32 line,
const char* msg,
...);
//NOTE(martin): warnings, messages, and debug info can be enabled in debug mode by defining LOG_COMPILE_XXX, XXX being the max desired log level
// error logging is always compiled
#define LOG_ERROR(msg, ...) log_generic(LOG_LEVEL_ERROR, __FUNCTION__, __FILE__, __LINE__, msg, ##__VA_ARGS__ )
#if defined(LOG_COMPILE_WARNING) || defined(LOG_COMPILE_INFO)
#define LOG_WARNING(msg, ...) log_generic(LOG_LEVEL_WARNING, __FUNCTION__, __FILE__, __LINE__, msg, ##__VA_ARGS__ )
#if defined(LOG_COMPILE_INFO)
#define LOG_INFO(msg, ...) log_generic(LOG_LEVEL_INFO, __FUNCTION__, __FILE__, __LINE__, msg, ##__VA_ARGS__ )
#else
#define LOG_INFO(msg, ...)
#endif
#else
#define LOG_WARNING(msg, ...)
#define LOG_INFO(msg, ...)
#endif
#ifdef __cplusplus
} // extern "C"
#endif
#endif //__DEBUG_LOG_H_

View File

@ -115,6 +115,23 @@ void mem_arena_clear(mem_arena* arena)
arena->currentChunk = list_first_entry(&arena->chunks, mem_arena_chunk, listElt);
}
mem_arena_marker mem_arena_mark(mem_arena* arena)
{
mem_arena_marker marker = {.chunk = arena->currentChunk,
.offset = arena->currentChunk->offset};
#if DEBUG
marker.arena = arena;
#endif
return(marker);
}
void mem_arena_clear_to(mem_arena* arena, mem_arena_marker marker)
{
DEBUG_ASSERT(arena == marker.arena);
arena->currentChunk = marker.chunk;
arena->currentChunk->offset = marker.offset;
}
//--------------------------------------------------------------------------------
//NOTE(martin): memory pool
//--------------------------------------------------------------------------------

View File

@ -38,6 +38,15 @@ typedef struct mem_arena
} mem_arena;
typedef struct mem_arena_marker
{
#if DEBUG
mem_arena* arena;
#endif
mem_arena_chunk* chunk;
u64 offset;
} mem_arena_marker;
typedef struct mem_arena_options
{
mem_base_allocator* base;
@ -50,6 +59,8 @@ MP_API void mem_arena_release(mem_arena* arena);
MP_API void* mem_arena_alloc(mem_arena* arena, u64 size);
MP_API void mem_arena_clear(mem_arena* arena);
MP_API mem_arena_marker mem_arena_mark(mem_arena* arena);
MP_API void mem_arena_clear_to(mem_arena* arena, mem_arena_marker marker);
#define mem_arena_alloc_type(arena, type) ((type*)mem_arena_alloc(arena, sizeof(type)))
#define mem_arena_alloc_array(arena, type, count) ((type*)mem_arena_alloc(arena, sizeof(type)*(count)))

View File

@ -6,8 +6,6 @@
* @revision:
*
*****************************************************************/
#include<string.h> // strlen(), strcpy(), etc.
#include<stdarg.h> // va_list() etc.
#include"macro_helpers.h"
#include"strings.h"

View File

@ -9,10 +9,11 @@
#ifndef __STRINGS_H_
#define __STRINGS_H_
#include<string.h> // strlen
#include"typedefs.h"
#include"lists.h"
#include"memory.h"
#include"platform_strings.h"
#include"platform_varg.h"
#ifdef __cplusplus
extern "C" {