goat3dgfx
diff src/logger.cc @ 0:1873dfd13f2d
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 14 Nov 2013 05:27:09 +0200 |
parents | |
children | 18879c956eb1 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/logger.cc Thu Nov 14 05:27:09 2013 +0200 1.3 @@ -0,0 +1,96 @@ 1.4 +#include <stdio.h> 1.5 +#include <stdarg.h> 1.6 +#include "logger.h" 1.7 + 1.8 +#if defined(unix) || defined(__unix__) || defined(__APPLE__) 1.9 +#include <unistd.h> 1.10 +#endif 1.11 + 1.12 +enum { LOG_INFO, LOG_WARNING, LOG_ERROR, LOG_DEBUG }; 1.13 + 1.14 +static int typecolor(int type); 1.15 + 1.16 +static FILE *fp = stdout; 1.17 + 1.18 +static void logmsg(int type, const char *fmt, va_list ap) 1.19 +{ 1.20 +#if defined(unix) || defined(__unix__) || (defined(__APPLE__) && !defined(TARGET_IPHONE)) 1.21 + if(isatty(fileno(fp)) && type != LOG_INFO) { 1.22 + int c = typecolor(type); 1.23 + fprintf(fp, "\033[%dm", c); 1.24 + vfprintf(fp, fmt, ap); 1.25 + fprintf(fp, "\033[0m"); 1.26 + } else 1.27 +#endif 1.28 + { 1.29 + vfprintf(fp, fmt, ap); 1.30 + } 1.31 + if(type == LOG_ERROR || type == LOG_DEBUG) { 1.32 + fflush(fp); 1.33 + } 1.34 +} 1.35 + 1.36 +void info_log(const char *fmt, ...) 1.37 +{ 1.38 + va_list ap; 1.39 + 1.40 + va_start(ap, fmt); 1.41 + logmsg(LOG_INFO, fmt, ap); 1.42 + va_end(ap); 1.43 +} 1.44 + 1.45 +void warning_log(const char *fmt, ...) 1.46 +{ 1.47 + va_list ap; 1.48 + 1.49 + va_start(ap, fmt); 1.50 + logmsg(LOG_WARNING, fmt, ap); 1.51 + va_end(ap); 1.52 +} 1.53 + 1.54 +void error_log(const char *fmt, ...) 1.55 +{ 1.56 + va_list ap; 1.57 + 1.58 + va_start(ap, fmt); 1.59 + logmsg(LOG_ERROR, fmt, ap); 1.60 + va_end(ap); 1.61 +} 1.62 + 1.63 +void debug_log(const char *fmt, ...) 1.64 +{ 1.65 + va_list ap; 1.66 + 1.67 + va_start(ap, fmt); 1.68 + logmsg(LOG_DEBUG, fmt, ap); 1.69 + va_end(ap); 1.70 +} 1.71 + 1.72 +enum { 1.73 + BLACK = 0, 1.74 + RED, 1.75 + GREEN, 1.76 + YELLOW, 1.77 + BLUE, 1.78 + MAGENTA, 1.79 + CYAN, 1.80 + WHITE 1.81 +}; 1.82 + 1.83 +#define ANSI_FGCOLOR(x) (30 + (x)) 1.84 +#define ANSI_BGCOLOR(x) (40 + (x)) 1.85 + 1.86 +static int typecolor(int type) 1.87 +{ 1.88 + switch(type) { 1.89 + case LOG_ERROR: 1.90 + return ANSI_FGCOLOR(RED); 1.91 + case LOG_WARNING: 1.92 + return ANSI_FGCOLOR(YELLOW); 1.93 + case LOG_DEBUG: 1.94 + return ANSI_FGCOLOR(MAGENTA); 1.95 + default: 1.96 + break; 1.97 + } 1.98 + return 37; 1.99 +}