finish up plugin architecture
[csRadar.git] / csrLog.h
index 42b16722aab0ec86279b85119647711c69c2e079..eea60ceec15ede3d57c8bc6b9bc3d18766cb8124 100644 (file)
--- 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"
 #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
-
-void(*CSR_LOG_REDIR_STDOUT)(int, const char*) = NULL;
-int CSR_LOG_ENABLE_STDOUT = 1;
-
-void csr_log_out( FILE *f, int type, const char* prefix, const char* fmt, ... )
-{
-       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
-       }
-       
-       // 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 );
-       }
-}
+#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 );
 
-#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__ )
-
-#define log_init log_alloc
-#define log_dealloc log_free
+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 )
 {
@@ -89,3 +66,5 @@ void csr_prog_end(void)
        csr_prog_update( 1.f );
        printf( "\n" );
 }
+
+#endif