logger update
authorhgn <hgodden00@gmail.com>
Sat, 17 Jun 2023 13:45:54 +0000 (14:45 +0100)
committerhgn <hgodden00@gmail.com>
Sat, 17 Jun 2023 13:45:54 +0000 (14:45 +0100)
submodules/SDL_GameControllerDB
submodules/anyascii
submodules/qoi
submodules/stb
vg.h
vg_console.h
vg_log.c [new file with mode: 0644]
vg_log.h
vg_mem.h
vg_mem_pool.h

index 6ed8d054340ee8a93a684e11360b66cd8a5c168e..c5b4df0e1061175cb11e3ebbf8045178339864a5 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 6ed8d054340ee8a93a684e11360b66cd8a5c168e
+Subproject commit c5b4df0e1061175cb11e3ebbf8045178339864a5
index 44e971c774d9ec67ca6c1f16c5a476724821ab63..eb5332d0b5e48d58397e6f27475a18e058330d23 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 44e971c774d9ec67ca6c1f16c5a476724821ab63
+Subproject commit eb5332d0b5e48d58397e6f27475a18e058330d23
index b8d77df1e80b652a57f0b7270449b179a6b91f40..dfc056e813c98d307238d35f7f041a725d699dfc 160000 (submodule)
@@ -1 +1 @@
-Subproject commit b8d77df1e80b652a57f0b7270449b179a6b91f40
+Subproject commit dfc056e813c98d307238d35f7f041a725d699dfc
index 8b5f1f37b5b75829fc72d38e7b5d4bcbf8a26d55..5736b15f7ea0ffb08dd38af21067c314d6a3aae9 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 8b5f1f37b5b75829fc72d38e7b5d4bcbf8a26d55
+Subproject commit 5736b15f7ea0ffb08dd38af21067c314d6a3aae9
diff --git a/vg.h b/vg.h
index 2a52614b5fc2803c56c0e328edae5e88d580a507..35fb4fdee01b394db40be870a40b72138a7b01e9 100644 (file)
--- a/vg.h
+++ b/vg.h
 
   #include "vg_platform.h"
   #include "vg_mem.h"
-
-  #ifndef _WIN32
-    #include <execinfo.h>
-  #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<size; i++ )
-         vg_info( "%s\n", strings[i] );
-
-      vg_error( "---------------- gnu backtrace -------------\n" );
-   }
-
-   free( strings );
-
-#endif
-}
   
   #ifdef VG_GAME
     #include "dep/glad/glad.h"
@@ -237,6 +208,14 @@ VG_STATIC enum vg_thread_purpose vg_thread_purpose(void)
    }
 }
 
+VG_STATIC void vg_assert_thread( enum vg_thread_purpose required ){
+   enum vg_thread_purpose purpose = vg_thread_purpose();
+
+   if( purpose != required ){
+      vg_fatal_error( "thread_purpose must be %u not %u\n", required, purpose );
+   }
+}
+
 VG_STATIC void _vg_opengl_sync_init(void)
 {
    vg.sem_loader = SDL_CreateSemaphore(1);
@@ -878,7 +857,7 @@ VG_STATIC void vg_fatal_error( const char *fmt, ... )
 {
    va_list args;
    va_start( args, fmt );
-   _vg_log_write( stderr, KRED "  fatal" KWHT "| " KRED, fmt, args );
+   _vg_logx_va( stderr, NULL, "fatal", KRED, fmt, args );
    va_end( args );
 
    vg_print_backtrace();
@@ -903,7 +882,7 @@ VG_STATIC void vg_fatal_error( const char *fmt, ... )
 {
    va_list args;
    va_start( args, fmt );
-   _vg_log_write( stderr, KRED "  fatal" KWHT "| " KRED, fmt, args );
+   _vg_logx_va( stderr, NULL, "fatal", KRED, fmt, args );
    va_end( args );
    exit(0);
 }
@@ -916,4 +895,6 @@ VG_STATIC void vg_fatal_error( const char *fmt, ... )
 u32 NvOptimusEnablement = 0x00000001;
 int AmdPowerXpressRequestHighPerformance = 1;
 
+#include "vg_log.c"
+
 #endif /* VG_HEADER_H */
index d2f8d878edee8763179e2ab57d8d35b3ff4310ce..208b5b392b9245887583b6f7b34ebc32cb35f2b3 100644 (file)
@@ -649,9 +649,9 @@ VG_STATIC void _vg_console_draw(void)
 
    SDL_AtomicLock( &log_print_sl );
 
-       int ptr = vg_log.buffer_line_current;
+       int ptr = vg_log.log_line_current;
    int const fh = 14, log_lines = 32;
-   int console_lines = VG_MIN( log_lines, vg_log.buffer_line_count );
+   int console_lines = VG_MIN( log_lines, vg_log.log_line_count );
 
    ui_rect rect_log   = { 0, 0,                vg.window_x, log_lines*fh },
            rect_input = { 0, log_lines*fh + 1, vg.window_x, fh*2 },
@@ -668,9 +668,9 @@ VG_STATIC void _vg_console_draw(void)
    for( int i=0; i<console_lines; i ++ ){
       ptr --;
 
-      if( ptr < 0 ) ptr = vg_list_size( vg_log.buffer )-1;
+      if( ptr < 0 ) ptr = vg_list_size( vg_log.log )-1;
       
-      ui_text( rect_line, vg_log.buffer[ptr], 1, k_ui_align_left, 0 );
+      ui_text( rect_line, vg_log.log[ptr], 1, k_ui_align_left, 0 );
       rect_line[1] -= fh;
    }
        
diff --git a/vg_log.c b/vg_log.c
new file mode 100644 (file)
index 0000000..638d63f
--- /dev/null
+++ b/vg_log.c
@@ -0,0 +1,136 @@
+#ifndef VG_LOG_C
+#include <stdarg.h>
+#include <string.h>
+#include <malloc.h>
+#include "vg_stdint.h"
+#include "vg_platform.h"
+#include "vg_log.h"
+
+#ifndef _WIN32
+ #include <execinfo.h>
+#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<vg_list_size(buffer); i++ ){
+      char c = buffer[i];
+
+      if( c == '\0' || c == '\n' ){
+         buffer[i] = '\0';
+
+         const char *line_prefix = "",
+                    *line_location = "";
+
+         if( line == buffer ) {
+            line_prefix = prefix;
+            line_location = location;
+         }
+
+         snprintf( logline, 96, "%s%7s" KNRM "|%s %s", 
+                   colour, line_prefix, colour, line );
+         _vg_log_append_line( logline );
+
+         if( location ){
+#ifdef VG_GAME
+            const char *thread_colours[] = {
+               KGRN, KMAG, KCYN, KYEL, KBLU
+            };
+
+            const char *colour = thread_colours[
+               (vg_thread_purpose() % vg_list_size( thread_colours ))];
+
+            fprintf( file, "%s[%u]"KNRM"%.32s", 
+                     colour, vg_thread_purpose(), line_location );
+#else
+            fprintf( file, KNRM "%.32s", line_location );
+#endif
+         }
+
+         fputs( logline, file );
+         fputc( '\n', file );
+         fputs( KNRM, file );
+
+         if( c == '\0' ) break;
+         if( buffer[i+1] == '\0' ) break;
+         line = buffer+i+1;
+      }
+   }
+
+#ifdef VG_GAME
+   SDL_AtomicUnlock( &log_print_sl );
+#endif
+}
+
+static void vg_logx( FILE *file, 
+                     const char *location, const char *prefix, 
+                     const char *colour,
+                     const char *fmt, ... ){
+
+   va_list args;
+   va_start( args, fmt );
+   _vg_logx_va( file, 
+#ifdef VG_LOG_SOURCE_INFO
+         location, 
+#else
+         NULL,
+#endif
+         prefix, colour, fmt, args );
+   va_end( args );
+}
+
+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<size; i++ )
+         vg_info( "%s\n", strings[i] );
+
+      vg_error( "---------------- gnu backtrace -------------\n" );
+   }
+
+   free( strings );
+
+#endif
+}
+
+#endif /* VG_LOG_C */
index b28cefbe1ec8bf671af453872794684d8d861d6f..8d2420a5fa7125d38ef05343d7422ea7f024c2e0 100644 (file)
--- a/vg_log.h
+++ b/vg_log.h
@@ -1,23 +1,22 @@
 #ifndef VG_LOG_H
 #define VG_LOG_H
 
-#include <stdarg.h>
-#include <string.h>
-#include <malloc.h>
-#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]
 #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[0] ); i++ ){
-      if( !str[i] || ( i == vg_list_size(vg_log.buffer[0])-1 ) ){
-         dest[i] = '\0';
-         break;
-      }
+static void vg_print_backtrace(void);
 
-      dest[i] = str[i];
-   }
-   
-   if( vg_log.buffer_line_current >= 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<vg_list_size( buffer ); i ++ ){
-               if( prefix[i] )
-                       buffer[i] = prefix[i];
-               else 
-                       break;
-       }
-       
-       j = i + vsnprintf( buffer + i, vg_list_size( buffer ) - i -12, fmt, args );
-       strcpy( buffer + j, KNRM );
-       
-       fputs( buffer, file );
-   
-   _vg_console_append_to_log( buffer );
-#ifdef VG_GAME
-   SDL_AtomicUnlock( &log_print_sl );
 #endif
-}
-
-#define VG_LOGX( NAME, PIPE, PFX )           \
-VG_STATIC void NAME(const char *fmt, ...)    \
-{                                            \
-   va_list args;                             \
-   va_start( args, fmt );                    \
-   _vg_log_write( PIPE, (PFX), fmt, args );   \
-   va_end( args );                           \
-}
-
-VG_LOGX( vg_success, stdout, (KGRN "success" KWHT "| " KGRN) )
-VG_LOGX( vg_info,    stdout, (KNRM "   info" KWHT "| " KNRM) )
-VG_LOGX( vg_warn,    stdout, (KYEL "   warn" KWHT "| " KYEL) )
-VG_LOGX( vg_error,   stderr, (KRED "  error" KWHT "| " KRED) )
-VG_LOGX( vg_low,     stdout, (KWHT "    log" KWHT "| " KWHT) )
-
-#endif /* VG_LOG_H */
index f8d26d4e7775a4a17d777c537ea61dbd776c8105..12a04f986eecce50d2d1fd917b48dd2e725e8703 100644 (file)
--- a/vg_mem.h
+++ b/vg_mem.h
@@ -4,6 +4,7 @@
 #include "vg.h"
 #include "vg_stdint.h"
 #include "vg_platform.h"
+#include "vg_log.h"
 
 #include <stdlib.h>
 #include <malloc.h>
@@ -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 )
 {
index d5742b48009bf792dba7bfe6f7d187a655f75e83..2326a3f469f70684c38852ffd47adde68053f2e4 100644 (file)
@@ -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 = ic<pool->count? 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;
    }
 }