goat3dgfx

annotate src/logger.cc @ 5:18879c956eb1

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