goat3dgfx

annotate src/logger.cc @ 15:7d6b667821cf

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