conworlds

annotate 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
rev   line source
nuclear@13 1 #include <stdio.h>
nuclear@13 2 #include <stdarg.h>
nuclear@13 3 #include "logger.h"
nuclear@13 4
nuclear@13 5 #if defined(unix) || defined(__unix__) || defined(__APPLE__)
nuclear@13 6 #include <unistd.h>
nuclear@13 7 #elif defined(WIN32)
nuclear@13 8 #include <windows.h>
nuclear@13 9 #endif
nuclear@13 10
nuclear@13 11 enum { LOG_INFO, LOG_WARNING, LOG_ERROR, LOG_FATAL, LOG_DEBUG };
nuclear@13 12
nuclear@13 13 static int typecolor(int type);
nuclear@13 14
nuclear@13 15 static FILE *fp = stdout;
nuclear@13 16
nuclear@13 17 static void logmsg(int type, const char *fmt, va_list ap)
nuclear@13 18 {
nuclear@13 19 #if defined(unix) || defined(__unix__) || (defined(__APPLE__) && !defined(TARGET_IPHONE))
nuclear@13 20 if(isatty(fileno(fp)) && type != LOG_INFO) {
nuclear@13 21 int c = typecolor(type);
nuclear@13 22 fprintf(fp, "\033[%dm", c);
nuclear@13 23 vfprintf(fp, fmt, ap);
nuclear@13 24 fprintf(fp, "\033[0m");
nuclear@13 25 } else
nuclear@13 26 #endif
nuclear@13 27 {
nuclear@13 28 vfprintf(fp, fmt, ap);
nuclear@13 29 }
nuclear@13 30 if(type == LOG_ERROR || type == LOG_FATAL || type == LOG_DEBUG) {
nuclear@13 31 fflush(fp);
nuclear@13 32 }
nuclear@13 33
nuclear@13 34 #ifdef WIN32
nuclear@13 35 if(type == LOG_FATAL) {
nuclear@13 36 static char msgbuf[1024];
nuclear@13 37 vsnprintf(msgbuf, sizeof msgbuf - 1, fmt, ap);
nuclear@13 38 msgbuf[sizeof msgbuf - 1] = 0;
nuclear@13 39 MessageBox(0, msgbuf, "Fatal error", MB_OK | MB_ICONSTOP);
nuclear@13 40 }
nuclear@13 41 #endif
nuclear@13 42 }
nuclear@13 43
nuclear@13 44 void info_log(const char *fmt, ...)
nuclear@13 45 {
nuclear@13 46 va_list ap;
nuclear@13 47
nuclear@13 48 va_start(ap, fmt);
nuclear@13 49 logmsg(LOG_INFO, fmt, ap);
nuclear@13 50 va_end(ap);
nuclear@13 51 }
nuclear@13 52
nuclear@13 53 void warning_log(const char *fmt, ...)
nuclear@13 54 {
nuclear@13 55 va_list ap;
nuclear@13 56
nuclear@13 57 va_start(ap, fmt);
nuclear@13 58 logmsg(LOG_WARNING, fmt, ap);
nuclear@13 59 va_end(ap);
nuclear@13 60 }
nuclear@13 61
nuclear@13 62 void error_log(const char *fmt, ...)
nuclear@13 63 {
nuclear@13 64 va_list ap;
nuclear@13 65
nuclear@13 66 va_start(ap, fmt);
nuclear@13 67 logmsg(LOG_ERROR, fmt, ap);
nuclear@13 68 va_end(ap);
nuclear@13 69 }
nuclear@13 70
nuclear@13 71 void fatal_log(const char *fmt, ...)
nuclear@13 72 {
nuclear@13 73 va_list ap;
nuclear@13 74
nuclear@13 75 va_start(ap, fmt);
nuclear@13 76 logmsg(LOG_FATAL, fmt, ap);
nuclear@13 77 va_end(ap);
nuclear@13 78 }
nuclear@13 79
nuclear@13 80 void debug_log(const char *fmt, ...)
nuclear@13 81 {
nuclear@13 82 va_list ap;
nuclear@13 83
nuclear@13 84 va_start(ap, fmt);
nuclear@13 85 logmsg(LOG_DEBUG, fmt, ap);
nuclear@13 86 va_end(ap);
nuclear@13 87 }
nuclear@13 88
nuclear@13 89 enum {
nuclear@13 90 BLACK = 0,
nuclear@13 91 RED,
nuclear@13 92 GREEN,
nuclear@13 93 YELLOW,
nuclear@13 94 BLUE,
nuclear@13 95 MAGENTA,
nuclear@13 96 CYAN,
nuclear@13 97 WHITE
nuclear@13 98 };
nuclear@13 99
nuclear@13 100 #define ANSI_FGCOLOR(x) (30 + (x))
nuclear@13 101 #define ANSI_BGCOLOR(x) (40 + (x))
nuclear@13 102
nuclear@13 103 static int typecolor(int type)
nuclear@13 104 {
nuclear@13 105 switch(type) {
nuclear@13 106 case LOG_ERROR:
nuclear@13 107 return ANSI_FGCOLOR(RED);
nuclear@13 108 case LOG_FATAL:
nuclear@13 109 return ANSI_FGCOLOR(RED); // TODO differentiate from LOG_ERROR
nuclear@13 110 case LOG_WARNING:
nuclear@13 111 return ANSI_FGCOLOR(YELLOW);
nuclear@13 112 case LOG_DEBUG:
nuclear@13 113 return ANSI_FGCOLOR(MAGENTA);
nuclear@13 114 default:
nuclear@13 115 break;
nuclear@13 116 }
nuclear@13 117 return 37;
nuclear@13 118 }