From: hgn Date: Mon, 1 Nov 2021 17:52:38 +0000 (+0000) Subject: update logging IO X-Git-Url: https://harrygodden.com/git/?p=fishladder.git;a=commitdiff_plain;h=ed4d77304bd5515253b4048fabbc9a4da81a6f44 update logging IO --- diff --git a/vg/vg.h b/vg/vg.h index 74a8775..90d8f70 100644 --- a/vg/vg.h +++ b/vg/vg.h @@ -48,6 +48,7 @@ float vg_time_delta; #include "vg/vg_input.h" #include "vg/vg_ui.h" #include "vg/vg_debug.h" +#include "vg/vg_console.h" #include "steam/steamworks_thin.h" diff --git a/vg/vg_console.h b/vg/vg_console.h new file mode 100644 index 0000000..e69de29 diff --git a/vg/vg_io.h b/vg/vg_io.h index 8925412..d037dd2 100644 --- a/vg/vg_io.h +++ b/vg/vg_io.h @@ -12,23 +12,43 @@ #define KCYN "\x1B[36m" #define KWHT "\x1B[37m" -#define VG_LOG_WRITE( FILE, PREFIX ) \ - fprintf( FILE, PREFIX ); \ - va_list args; \ - va_start( args, fmt ); \ - vfprintf( FILE, fmt, args ); \ - va_end( args ); \ - fprintf( FILE, KNRM ); +void (*vg_log_callback)( const char *str ) = NULL; -void vg_success( const char *fmt, ... ) { VG_LOG_WRITE( stdout, (KGRN "success" KWHT "| " KGRN) ); } -void vg_info( const char *fmt, ... ) { VG_LOG_WRITE( stdout, (KNRM " info" KWHT "| " KNRM) ); } -void vg_warn( const char *fmt, ... ) { VG_LOG_WRITE( stdout, (KYEL " warn" KWHT "| " KYEL) ); } -void vg_error( const char *fmt, ... ) { VG_LOG_WRITE( stderr, (KRED " error" KWHT "| " KRED) ); } +static void vg_log_write( FILE *file, const char *prefix, const char *fmt, va_list args ) +{ + char buffer[512]; + 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 -2, fmt, args ); + strcpy( buffer + j, KNRM ); + + fputs( buffer, file ); + + if( vg_log_callback ) + vg_log_callback( buffer ); +} + +static void vg_success( const char *fmt, ... ) + { va_list args; va_start( args, fmt ); vg_log_write( stdout, (KGRN "success" KWHT "| " KGRN), fmt, args ); va_end( args ); } +static void vg_info( const char *fmt, ... ) + { va_list args; va_start( args, fmt ); vg_log_write( stdout, (KNRM " info" KWHT "| " KNRM), fmt, args ); va_end( args ); } +static void vg_warn( const char *fmt, ... ) + { va_list args; va_start( args, fmt ); vg_log_write( stdout, (KYEL " warn" KWHT "| " KYEL), fmt, args ); va_end( args ); } +static void vg_error( const char *fmt, ... ) + { va_list args; va_start( args, fmt ); vg_log_write( stderr, (KRED " error" KWHT "| " KRED), fmt, args ); va_end( args ); } // FILE IO // =========================================================================================================== -i64 vg_file_size( FILE *fileptr ) +static i64 vg_file_size( FILE *fileptr ) { fseek( fileptr, 0, SEEK_END ); i64 fsize = ftell( fileptr ); @@ -37,7 +57,7 @@ i64 vg_file_size( FILE *fileptr ) return fsize; } -void *vg_disk_open_read( const char *path, int const reserve_end, i64 *size ) +static void *vg_disk_open_read( const char *path, int const reserve_end, i64 *size ) { FILE *f = fopen( path, "rb" ); if( f ) @@ -66,7 +86,7 @@ void *vg_disk_open_read( const char *path, int const reserve_end, i64 *size ) } } -char *vg_disk_load_text( const char *path, i64 *size ) +static char *vg_disk_load_text( const char *path, i64 *size ) { char *buf; i64 fsize; @@ -82,23 +102,23 @@ char *vg_disk_load_text( const char *path, i64 *size ) return NULL; } -void *vg_asset_read_s( const char *path, i64 *size ) +static void *vg_asset_read_s( const char *path, i64 *size ) { return vg_disk_open_read( path, 0, size ); } -void *vg_asset_read( const char *path ) +static void *vg_asset_read( const char *path ) { i64 size; return vg_disk_open_read( path, 0, &size ); } -char *vg_textasset_read_s( const char *path, i64 *size ) +static char *vg_textasset_read_s( const char *path, i64 *size ) { return vg_disk_load_text( path, size ); } -char *vg_textasset_read( const char *name ) +static char *vg_textasset_read( const char *name ) { i64 size; return vg_disk_load_text( name, &size );