f0735ad84e356a2a29eafe3cd7c025285a4191c7
[csRadar.git] / csrLog.h
1 // Logging with colour
2 // ======================================================================================
3
4 #define KNRM "\x1B[0m"
5 #define KRED "\x1B[31m"
6 #define KGRN "\x1B[32m"
7 #define KYEL "\x1B[33m"
8 #define KBLU "\x1B[34m"
9 #define KMAG "\x1B[35m"
10 #define KCYN "\x1B[36m"
11 #define KWHT "\x1B[37m"
12
13 #define LOG_TYPE_DEV 0
14 #define LOG_TYPE_INFO 1
15 #define LOG_TYPE_ALLOC 2
16 #define LOG_TYPE_FREE 3
17 #define LOG_TYPE_SUCCESS 4
18 #define LOG_TYPE_ERROR 5
19 #define LOG_TYPE_WARN 6
20
21 void(*CSR_LOG_REDIR_STDOUT)(int, const char*) = NULL;
22 int CSR_LOG_ENABLE_STDOUT = 1;
23
24 void csr_log_out( FILE *f, int type, const char* prefix, const char* fmt, ... )
25 {
26 if( CSR_LOG_ENABLE_STDOUT )
27 {
28 fprintf( f, "%s", prefix );
29 va_list args;
30 va_start( args, fmt );
31 vfprintf( f, fmt, args );
32 va_end( args );
33 fprintf( f, KNRM ); // reset
34 }
35
36 // Send to a seperate logging function
37 if( CSR_LOG_REDIR_STDOUT )
38 {
39 char buffer[ 512 ];
40 va_list args;
41 va_start( args, fmt );
42 vsnprintf( buffer, 512, fmt, args );
43 va_end( args );
44 CSR_LOG_REDIR_STDOUT( type, buffer );
45 }
46 }
47
48 #define log_dev(FMT, ...) csr_log_out( stdout, LOG_TYPE_DEV, (KNRM " dev" KWHT "| " KNRM), FMT, ##__VA_ARGS__ )
49 #define log_info(FMT, ...) csr_log_out( stdout, LOG_TYPE_INFO, (KNRM " info" KWHT "| " KNRM), FMT, ##__VA_ARGS__ )
50 #define log_alloc(FMT, ...) csr_log_out( stdout, LOG_TYPE_ALLOC, (KCYN " alloc" KWHT "| " KNRM), FMT, ##__VA_ARGS__ )
51 #define log_free(FMT, ...) csr_log_out( stdout, LOG_TYPE_FREE, (KMAG " free" KWHT "| " KNRM), FMT, ##__VA_ARGS__ )
52 #define log_success(FMT, ...) csr_log_out( stdout, LOG_TYPE_SUCCESS, (KGRN "success" KWHT "| " KGRN), FMT, ##__VA_ARGS__ )
53 #define log_error(FMT, ...) csr_log_out( stderr, LOG_TYPE_ERROR, (KRED " error" KWHT "| " KRED), FMT, ##__VA_ARGS__ )
54 #define log_warn(FMT, ...) csr_log_out( stdout, LOG_TYPE_WARN, (KYEL " warn" KWHT "| " KYEL), FMT, ##__VA_ARGS__ )
55
56 #define log_init log_alloc
57 #define log_dealloc log_free