X-Git-Url: https://harrygodden.com/git/?p=csRadar.git;a=blobdiff_plain;f=csrLog.h;h=eea60ceec15ede3d57c8bc6b9bc3d18766cb8124;hp=f0735ad84e356a2a29eafe3cd7c025285a4191c7;hb=HEAD;hpb=d7339f0f28ca5b35ad60a393ca270cbae1a154c6 diff --git a/csrLog.h b/csrLog.h index f0735ad..eea60ce 100644 --- a/csrLog.h +++ b/csrLog.h @@ -1,6 +1,15 @@ +// Copyright (C) 2021 Harry Godden (hgn) + // Logging with colour // ====================================================================================== +void log_success( const char *fmt, ... ); +void log_info( const char *fmt, ... ); +void log_warn( const char *fmt, ... ); +void log_error( const char *fmt, ... ); + +#ifdef CSR_EXECUTABLE + #define KNRM "\x1B[0m" #define KRED "\x1B[31m" #define KGRN "\x1B[32m" @@ -10,48 +19,52 @@ #define KCYN "\x1B[36m" #define KWHT "\x1B[37m" -#define LOG_TYPE_DEV 0 -#define LOG_TYPE_INFO 1 -#define LOG_TYPE_ALLOC 2 -#define LOG_TYPE_FREE 3 -#define LOG_TYPE_SUCCESS 4 -#define LOG_TYPE_ERROR 5 -#define LOG_TYPE_WARN 6 +#define CSR_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(*CSR_LOG_REDIR_STDOUT)(int, const char*) = NULL; -int CSR_LOG_ENABLE_STDOUT = 1; +void log_success( const char *fmt, ... ) { CSR_LOG_WRITE( stdout, (KGRN "success" KWHT "| " KGRN) ); } +void log_info( const char *fmt, ... ) { CSR_LOG_WRITE( stdout, (KNRM " info" KWHT "| " KNRM) ); } +void log_warn( const char *fmt, ... ) { CSR_LOG_WRITE( stdout, (KYEL " warn" KWHT "| " KYEL) ); } +void log_error( const char *fmt, ... ) { CSR_LOG_WRITE( stderr, (KRED " error" KWHT "| " KRED) ); } + +// TODO: Remove this? +const char *csr_prog_msg; +void csr_prog_begin( const char *msg ) +{ + csr_prog_msg = msg; +} -void csr_log_out( FILE *f, int type, const char* prefix, const char* fmt, ... ) +void csr_prog_update( float const percent ) { - if( CSR_LOG_ENABLE_STDOUT ) - { - fprintf( f, "%s", prefix ); - va_list args; - va_start( args, fmt ); - vfprintf( f, fmt, args ); - va_end( args ); - fprintf( f, KNRM ); // reset - } + int const k_track_length = 40; + + printf( "\r %s %d%% |", csr_prog_msg, (int)(percent*100.f) ); + + // Calculate how many steps travelled + int cur_pos = ((float)k_track_length * (1.f-percent)); + + for( int i = 0; i < cur_pos; i ++ ) + printf( " " ); + + printf( "🚂🚃🚃🚃🚃" ); - // Send to a seperate logging function - if( CSR_LOG_REDIR_STDOUT ) - { - char buffer[ 512 ]; - va_list args; - va_start( args, fmt ); - vsnprintf( buffer, 512, fmt, args ); - va_end( args ); - CSR_LOG_REDIR_STDOUT( type, buffer ); - } + for( int i = 0; i < k_track_length-cur_pos; i ++ ) + printf( " " ); + + printf( "|" ); + + fflush( stdout ); } -#define log_dev(FMT, ...) csr_log_out( stdout, LOG_TYPE_DEV, (KNRM " dev" KWHT "| " KNRM), FMT, ##__VA_ARGS__ ) -#define log_info(FMT, ...) csr_log_out( stdout, LOG_TYPE_INFO, (KNRM " info" KWHT "| " KNRM), FMT, ##__VA_ARGS__ ) -#define log_alloc(FMT, ...) csr_log_out( stdout, LOG_TYPE_ALLOC, (KCYN " alloc" KWHT "| " KNRM), FMT, ##__VA_ARGS__ ) -#define log_free(FMT, ...) csr_log_out( stdout, LOG_TYPE_FREE, (KMAG " free" KWHT "| " KNRM), FMT, ##__VA_ARGS__ ) -#define log_success(FMT, ...) csr_log_out( stdout, LOG_TYPE_SUCCESS, (KGRN "success" KWHT "| " KGRN), FMT, ##__VA_ARGS__ ) -#define log_error(FMT, ...) csr_log_out( stderr, LOG_TYPE_ERROR, (KRED " error" KWHT "| " KRED), FMT, ##__VA_ARGS__ ) -#define log_warn(FMT, ...) csr_log_out( stdout, LOG_TYPE_WARN, (KYEL " warn" KWHT "| " KYEL), FMT, ##__VA_ARGS__ ) +void csr_prog_end(void) +{ + csr_prog_update( 1.f ); + printf( "\n" ); +} -#define log_init log_alloc -#define log_dealloc log_free +#endif