conworlds
diff src/logger.cc @ 13:283cdfa7dda2
added a crapload of code from goat3dgfx
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 24 Aug 2014 09:41:24 +0300 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/logger.cc Sun Aug 24 09:41:24 2014 +0300 1.3 @@ -0,0 +1,118 @@ 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 +#elif defined(WIN32) 1.11 +#include <windows.h> 1.12 +#endif 1.13 + 1.14 +enum { LOG_INFO, LOG_WARNING, LOG_ERROR, LOG_FATAL, LOG_DEBUG }; 1.15 + 1.16 +static int typecolor(int type); 1.17 + 1.18 +static FILE *fp = stdout; 1.19 + 1.20 +static void logmsg(int type, const char *fmt, va_list ap) 1.21 +{ 1.22 +#if defined(unix) || defined(__unix__) || (defined(__APPLE__) && !defined(TARGET_IPHONE)) 1.23 + if(isatty(fileno(fp)) && type != LOG_INFO) { 1.24 + int c = typecolor(type); 1.25 + fprintf(fp, "\033[%dm", c); 1.26 + vfprintf(fp, fmt, ap); 1.27 + fprintf(fp, "\033[0m"); 1.28 + } else 1.29 +#endif 1.30 + { 1.31 + vfprintf(fp, fmt, ap); 1.32 + } 1.33 + if(type == LOG_ERROR || type == LOG_FATAL || type == LOG_DEBUG) { 1.34 + fflush(fp); 1.35 + } 1.36 + 1.37 +#ifdef WIN32 1.38 + if(type == LOG_FATAL) { 1.39 + static char msgbuf[1024]; 1.40 + vsnprintf(msgbuf, sizeof msgbuf - 1, fmt, ap); 1.41 + msgbuf[sizeof msgbuf - 1] = 0; 1.42 + MessageBox(0, msgbuf, "Fatal error", MB_OK | MB_ICONSTOP); 1.43 + } 1.44 +#endif 1.45 +} 1.46 + 1.47 +void info_log(const char *fmt, ...) 1.48 +{ 1.49 + va_list ap; 1.50 + 1.51 + va_start(ap, fmt); 1.52 + logmsg(LOG_INFO, fmt, ap); 1.53 + va_end(ap); 1.54 +} 1.55 + 1.56 +void warning_log(const char *fmt, ...) 1.57 +{ 1.58 + va_list ap; 1.59 + 1.60 + va_start(ap, fmt); 1.61 + logmsg(LOG_WARNING, fmt, ap); 1.62 + va_end(ap); 1.63 +} 1.64 + 1.65 +void error_log(const char *fmt, ...) 1.66 +{ 1.67 + va_list ap; 1.68 + 1.69 + va_start(ap, fmt); 1.70 + logmsg(LOG_ERROR, fmt, ap); 1.71 + va_end(ap); 1.72 +} 1.73 + 1.74 +void fatal_log(const char *fmt, ...) 1.75 +{ 1.76 + va_list ap; 1.77 + 1.78 + va_start(ap, fmt); 1.79 + logmsg(LOG_FATAL, fmt, ap); 1.80 + va_end(ap); 1.81 +} 1.82 + 1.83 +void debug_log(const char *fmt, ...) 1.84 +{ 1.85 + va_list ap; 1.86 + 1.87 + va_start(ap, fmt); 1.88 + logmsg(LOG_DEBUG, fmt, ap); 1.89 + va_end(ap); 1.90 +} 1.91 + 1.92 +enum { 1.93 + BLACK = 0, 1.94 + RED, 1.95 + GREEN, 1.96 + YELLOW, 1.97 + BLUE, 1.98 + MAGENTA, 1.99 + CYAN, 1.100 + WHITE 1.101 +}; 1.102 + 1.103 +#define ANSI_FGCOLOR(x) (30 + (x)) 1.104 +#define ANSI_BGCOLOR(x) (40 + (x)) 1.105 + 1.106 +static int typecolor(int type) 1.107 +{ 1.108 + switch(type) { 1.109 + case LOG_ERROR: 1.110 + return ANSI_FGCOLOR(RED); 1.111 + case LOG_FATAL: 1.112 + return ANSI_FGCOLOR(RED); // TODO differentiate from LOG_ERROR 1.113 + case LOG_WARNING: 1.114 + return ANSI_FGCOLOR(YELLOW); 1.115 + case LOG_DEBUG: 1.116 + return ANSI_FGCOLOR(MAGENTA); 1.117 + default: 1.118 + break; 1.119 + } 1.120 + return 37; 1.121 +}