From dd402a7717847a2d74c3b98e3f3970cde4874a73 Mon Sep 17 00:00:00 2001 From: hgn Date: Sat, 17 Jun 2023 14:45:54 +0100 Subject: [PATCH] logger update --- submodules/SDL_GameControllerDB | 2 +- submodules/anyascii | 2 +- submodules/qoi | 2 +- submodules/stb | 2 +- vg.h | 43 +++------- vg_console.h | 8 +- vg_log.c | 136 ++++++++++++++++++++++++++++++++ vg_log.h | 110 +++++++------------------- vg_mem.h | 4 + vg_mem_pool.h | 105 +++++++++++++----------- 10 files changed, 247 insertions(+), 167 deletions(-) create mode 100644 vg_log.c diff --git a/submodules/SDL_GameControllerDB b/submodules/SDL_GameControllerDB index 6ed8d05..c5b4df0 160000 --- a/submodules/SDL_GameControllerDB +++ b/submodules/SDL_GameControllerDB @@ -1 +1 @@ -Subproject commit 6ed8d054340ee8a93a684e11360b66cd8a5c168e +Subproject commit c5b4df0e1061175cb11e3ebbf8045178339864a5 diff --git a/submodules/anyascii b/submodules/anyascii index 44e971c..eb5332d 160000 --- a/submodules/anyascii +++ b/submodules/anyascii @@ -1 +1 @@ -Subproject commit 44e971c774d9ec67ca6c1f16c5a476724821ab63 +Subproject commit eb5332d0b5e48d58397e6f27475a18e058330d23 diff --git a/submodules/qoi b/submodules/qoi index b8d77df..dfc056e 160000 --- a/submodules/qoi +++ b/submodules/qoi @@ -1 +1 @@ -Subproject commit b8d77df1e80b652a57f0b7270449b179a6b91f40 +Subproject commit dfc056e813c98d307238d35f7f041a725d699dfc diff --git a/submodules/stb b/submodules/stb index 8b5f1f3..5736b15 160000 --- a/submodules/stb +++ b/submodules/stb @@ -1 +1 @@ -Subproject commit 8b5f1f37b5b75829fc72d38e7b5d4bcbf8a26d55 +Subproject commit 5736b15f7ea0ffb08dd38af21067c314d6a3aae9 diff --git a/vg.h b/vg.h index 2a52614..35fb4fd 100644 --- a/vg.h +++ b/vg.h @@ -56,35 +56,6 @@ #include "vg_platform.h" #include "vg_mem.h" - - #ifndef _WIN32 - #include - #endif - -VG_STATIC void vg_print_backtrace(void) -{ -#ifndef _WIN32 - - void *array[20]; - char **strings; - int size, i; - - size = backtrace( array, 20 ); - strings = backtrace_symbols( array, size ); - - if( strings != NULL ){ - vg_error( "---------------- gnu backtrace -------------\n" ); - - for( int i=0; i +#include +#include +#include "vg_stdint.h" +#include "vg_platform.h" +#include "vg_log.h" + +#ifndef _WIN32 + #include +#endif + +static void _vg_log_append_line( const char *str ){ + if( vg_log.log_line_count < vg_list_size( vg_log.log ) ) + vg_log.log_line_count ++; + + char *dest = vg_log.log[ vg_log.log_line_current ++ ]; + vg_strncpy( str, dest, vg_list_size(vg_log.log[0]), k_strncpy_allow_cutoff ); + + if( vg_log.log_line_current >= vg_list_size( vg_log.log ) ) + vg_log.log_line_current = 0; +} + +static void _vg_logx_va( FILE *file, + const char *location, const char *prefix, + const char *colour, + const char *fmt, va_list args ) +{ + + /* @model.h:2000 info| dwahjdiawdjaiwdwadwa djaidjwa\n + * | djwaidwaj waodawh a\n + * | dwajdkiawjdiw + */ + +#ifdef VG_GAME + SDL_AtomicLock( &log_print_sl ); +#endif + + char buffer[4096]; + + vsnprintf( buffer, vg_list_size(buffer), fmt, args ); + + const char *line = buffer; + char logline[96]; + + for( u32 i=0; i -#include -#include -#include "vg_stdint.h" -#include "vg_platform.h" +#define VG_LOG_MCSTR(S) VG_LOG_MCSTR2(S) +#define VG_LOG_MCSTR2(S) #S +#define VG_LOG_WHERE "@"__FILE__":"VG_LOG_MCSTR(__LINE__)\ + " " + +#define vg_success( ... ) \ + vg_logx(stdout,VG_LOG_WHERE,"success",KGRN,__VA_ARGS__) +#define vg_info( ... ) \ + vg_logx(stdout,VG_LOG_WHERE,"info",KWHT,__VA_ARGS__) +#define vg_warn( ... ) \ + vg_logx(stdout,VG_LOG_WHERE,"warn",KYEL,__VA_ARGS__ ) +#define vg_error( ... ) \ + vg_logx(stdout,VG_LOG_WHERE,"error",KRED,__VA_ARGS__) +#define vg_low( ... ) \ + vg_logx(stdout,VG_LOG_WHERE,"log",KWHT,__VA_ARGS__) -#ifdef _WIN32 -#define KNRM "" -#define KBLK "" -#define KRED "" -#define KGRN "" -#define KYEL "" -#define KBLU "" -#define KMAG "" -#define KCYN "" -#define KWHT "" -#else #define KNRM "\x1B[0m" #define KBLK "\x1B[30m" #define KRED "\x1B[31m" @@ -27,7 +26,6 @@ #define KMAG "\x1B[35m" #define KCYN "\x1B[36m" #define KWHT "\x1B[37m" -#endif #define PRINTF_v2f( V2 ) "%.4f %.4f\n", V2[0], V2[1] #define PRINTF_v3f( V3 ) "%.4f %.4f %.4f\n", V3[0], V3[1], V3[2] @@ -40,77 +38,27 @@ #endif #ifdef VG_GAME -static SDL_SpinLock log_print_sl; + #include "dep/sdl/include/SDL.h" + static SDL_SpinLock log_print_sl; #endif -struct vg_log -{ - char buffer[64][96]; - u32 buffer_line_count, buffer_line_current; +struct vg_log{ + char log[64][96]; + u32 log_line_count, log_line_current; } static vg_log; -void _vg_console_append_to_log( const char *str ) -{ - if( vg_log.buffer_line_count < vg_list_size( vg_log.buffer ) ) - vg_log.buffer_line_count ++; +static void vg_logx( FILE *file, + const char *location, const char *prefix, + const char *colour, + const char *fmt, ... ); - char *dest = vg_log.buffer[ vg_log.buffer_line_current ++ ]; +static void _vg_logx_va( FILE *file, + const char *location, const char *prefix, + const char *colour, + const char *fmt, va_list args ); - for( int i=0; i= vg_list_size( vg_log.buffer ) ) - vg_log.buffer_line_current = 0; -} - -VG_STATIC void _vg_log_write( FILE *file, const char *prefix, - const char *fmt, va_list args ) -{ -#ifdef VG_GAME - SDL_AtomicLock( &log_print_sl ); -#endif - char buffer[ 4096 ]; - int i, j; - - for( i=0; i #include @@ -119,8 +120,11 @@ VG_STATIC void vg_mem_log( void *lin_alloc, int depth, const char *name ); */ VG_STATIC void vg_fatal_error( const char *fmt, ... ); + +#if 0 VG_STATIC void vg_error(const char *fmt, ...); VG_STATIC void vg_info(const char *fmt, ...); +#endif VG_STATIC u32 vg_align8( u32 s ) { diff --git a/vg_mem_pool.h b/vg_mem_pool.h index d5742b4..2326a3f 100644 --- a/vg_mem_pool.h +++ b/vg_mem_pool.h @@ -1,8 +1,9 @@ #ifndef VG_MEM_POOL_H #define VG_MEM_POOL_H -/* pool for loaded addons */ -#define VG_POOL_NIL 0xffff +#include "vg/vg_mem.h" +#include "stddef.h" +#include "vg/vg_stdint.h" typedef struct vg_pool vg_pool; typedef struct vg_pool_node vg_pool_node; @@ -18,62 +19,75 @@ struct vg_pool { size_t stride, offset; }; -static vg_pool_node *vg_pool_nodeptr ( vg_pool *pool, void *item ); -static void *vg_pool_item ( vg_pool *pool, u16 index ); +static vg_pool_node *vg_pool_nodeptr ( vg_pool *pool, u16 id ); +static void *vg_pool_item ( vg_pool *pool, u16 id ); static void vg_pool_init ( vg_pool *pool ); -static u16 addon_item_index ( vg_pool *pool, void *item ); -static void *vg_pool_lru ( vg_pool *pool ); -static void vg_pool_watch ( vg_pool *pool, void *item ); -static void vg_pool_unwatch ( vg_pool *pool, void *item ); +static u16 vg_pool_id ( vg_pool *pool, void *item ); +static u16 vg_pool_lru ( vg_pool *pool ); +static void vg_pool_watch ( vg_pool *pool, u16 id ); +static void vg_pool_unwatch ( vg_pool *pool, u16 id ); /* implementation * -------------------------------------------------------------------------- */ -static vg_pool_node *vg_pool_nodeptr( vg_pool *pool, void *item ){ - if( !item ) return NULL; - else return item + pool->offset; +static vg_pool_node *vg_pool_nodeptr( vg_pool *pool, u16 id ){ + if( !id ) return NULL; + else { + return pool->buffer + (pool->stride*(id-1)) + pool->offset; + } } -static void *vg_pool_item( vg_pool *pool, u16 index ){ - if( index == VG_POOL_NIL ) return NULL; - else return pool->buffer + pool->stride*(size_t)index; +static void *vg_pool_item( vg_pool *pool, u16 id ){ + if( !id ) return NULL; + else return pool->buffer + pool->stride*(size_t)(id-1); } static void vg_pool_init( vg_pool *pool ){ - pool->head = 0; - pool->tail = pool->count -1; - for( i32 ib=0; ib < pool->count; ib++ ){ - void *vb = vg_pool_item( pool, ib ); - vg_pool_node *nb = vg_pool_nodeptr( pool, vb ); - - i32 ia = ib-1, ic = ib+1; - nb->l = ia>=0? ia: VG_POOL_NIL, - nb->r = iccount? ic: VG_POOL_NIL; + pool->head = 1; + pool->tail = pool->count; + for( u16 ib=1; ib <= pool->count; ib++ ){ + vg_pool_node *nb = vg_pool_nodeptr( pool, ib ); + + u16 ia = ib-1, ic = ib+1; + nb->l = ia; + nb->r = ic<=pool->count? ic: 0; nb->ref_count = 0; } } -static u16 addon_item_index( vg_pool *pool, void *item ){ - return (item - pool->buffer) / pool->stride; +static u16 vg_pool_id( vg_pool *pool, void *item ){ + return ((item - pool->buffer) / pool->stride) + 1; } -static void *vg_pool_lru( vg_pool *pool ){ - u16 head = pool->head; - if( head == VG_POOL_NIL ) return NULL; +static void vg_pool_unlink( vg_pool *pool, u16 id ){ + vg_pool_node *node = vg_pool_nodeptr( pool, id ); + vg_pool_node *l = vg_pool_nodeptr( pool, node->l ), + *r = vg_pool_nodeptr( pool, node->r ); - void *item = vg_pool_item( pool, head ); - vg_pool_node *node = vg_pool_nodeptr( pool, item ); + if( pool->head == id ) pool->head = node->r; + if( pool->tail == id ) pool->tail = node->l; + + if( l ) l->r = node->r; + if( r ) r->l = node->l; + + node->r = 0; + node->l = 0; +} - if( pool->head == pool->tail ) pool->tail = VG_POOL_NIL; - pool->head = node->r; +static u16 vg_pool_lru( vg_pool *pool ){ + u16 head = pool->head; + if( !head ) return 0; - node->l = VG_POOL_NIL; - node->r = VG_POOL_NIL; - return item; + vg_pool_unlink( pool, head ); + return head; } -static void vg_pool_watch( vg_pool *pool, void *item ){ - vg_pool_node *node = vg_pool_nodeptr( pool, item ); +static void vg_pool_watch( vg_pool *pool, u16 id ){ + vg_pool_node *node = vg_pool_nodeptr( pool, id ); + + if( !node->ref_count ){ + vg_pool_unlink( pool, id ); + } if( node->ref_count >= 128 ) vg_fatal_error( "pool watch missmatch (limit is 128)\n" ); @@ -82,24 +96,21 @@ static void vg_pool_watch( vg_pool *pool, void *item ){ } /* if after this no more watches, places back into the volatile list */ -static void vg_pool_unwatch( vg_pool *pool, void *item ){ - vg_pool_node *node = vg_pool_nodeptr( pool, item ); +static void vg_pool_unwatch( vg_pool *pool, u16 id ){ + vg_pool_node *node = vg_pool_nodeptr( pool, id ); if( node->ref_count == 0 ) vg_fatal_error( "pool unwatch missmatch (no watchers)\n" ); node->ref_count --; if( !node->ref_count ){ - void *item_head = vg_pool_item( pool, pool->head ), - *item_tail = vg_pool_item( pool, pool->tail ); - vg_pool_node *head = vg_pool_nodeptr( pool, item_head ), - *tail = vg_pool_nodeptr( pool, item_tail ); + vg_pool_node *head = vg_pool_nodeptr( pool, pool->head ), + *tail = vg_pool_nodeptr( pool, pool->tail ); - u16 index = addon_item_index( pool, item ); - if( tail ) tail->r = index; + if( tail ) tail->r = id; node->l = pool->tail; - pool->tail = index; - if( !head ) pool->head = index; + pool->tail = id; + if( !head ) pool->head = id; } } -- 2.25.1