goat3dgfx

annotate 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
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@0 7 #endif
nuclear@0 8
nuclear@0 9 enum { LOG_INFO, LOG_WARNING, LOG_ERROR, LOG_DEBUG };
nuclear@0 10
nuclear@0 11 static int typecolor(int type);
nuclear@0 12
nuclear@0 13 static FILE *fp = stdout;
nuclear@0 14
nuclear@0 15 static void logmsg(int type, const char *fmt, va_list ap)
nuclear@0 16 {
nuclear@0 17 #if defined(unix) || defined(__unix__) || (defined(__APPLE__) && !defined(TARGET_IPHONE))
nuclear@0 18 if(isatty(fileno(fp)) && type != LOG_INFO) {
nuclear@0 19 int c = typecolor(type);
nuclear@0 20 fprintf(fp, "\033[%dm", c);
nuclear@0 21 vfprintf(fp, fmt, ap);
nuclear@0 22 fprintf(fp, "\033[0m");
nuclear@0 23 } else
nuclear@0 24 #endif
nuclear@0 25 {
nuclear@0 26 vfprintf(fp, fmt, ap);
nuclear@0 27 }
nuclear@0 28 if(type == LOG_ERROR || type == LOG_DEBUG) {
nuclear@0 29 fflush(fp);
nuclear@0 30 }
nuclear@0 31 }
nuclear@0 32
nuclear@0 33 void info_log(const char *fmt, ...)
nuclear@0 34 {
nuclear@0 35 va_list ap;
nuclear@0 36
nuclear@0 37 va_start(ap, fmt);
nuclear@0 38 logmsg(LOG_INFO, fmt, ap);
nuclear@0 39 va_end(ap);
nuclear@0 40 }
nuclear@0 41
nuclear@0 42 void warning_log(const char *fmt, ...)
nuclear@0 43 {
nuclear@0 44 va_list ap;
nuclear@0 45
nuclear@0 46 va_start(ap, fmt);
nuclear@0 47 logmsg(LOG_WARNING, fmt, ap);
nuclear@0 48 va_end(ap);
nuclear@0 49 }
nuclear@0 50
nuclear@0 51 void error_log(const char *fmt, ...)
nuclear@0 52 {
nuclear@0 53 va_list ap;
nuclear@0 54
nuclear@0 55 va_start(ap, fmt);
nuclear@0 56 logmsg(LOG_ERROR, fmt, ap);
nuclear@0 57 va_end(ap);
nuclear@0 58 }
nuclear@0 59
nuclear@0 60 void debug_log(const char *fmt, ...)
nuclear@0 61 {
nuclear@0 62 va_list ap;
nuclear@0 63
nuclear@0 64 va_start(ap, fmt);
nuclear@0 65 logmsg(LOG_DEBUG, fmt, ap);
nuclear@0 66 va_end(ap);
nuclear@0 67 }
nuclear@0 68
nuclear@0 69 enum {
nuclear@0 70 BLACK = 0,
nuclear@0 71 RED,
nuclear@0 72 GREEN,
nuclear@0 73 YELLOW,
nuclear@0 74 BLUE,
nuclear@0 75 MAGENTA,
nuclear@0 76 CYAN,
nuclear@0 77 WHITE
nuclear@0 78 };
nuclear@0 79
nuclear@0 80 #define ANSI_FGCOLOR(x) (30 + (x))
nuclear@0 81 #define ANSI_BGCOLOR(x) (40 + (x))
nuclear@0 82
nuclear@0 83 static int typecolor(int type)
nuclear@0 84 {
nuclear@0 85 switch(type) {
nuclear@0 86 case LOG_ERROR:
nuclear@0 87 return ANSI_FGCOLOR(RED);
nuclear@0 88 case LOG_WARNING:
nuclear@0 89 return ANSI_FGCOLOR(YELLOW);
nuclear@0 90 case LOG_DEBUG:
nuclear@0 91 return ANSI_FGCOLOR(MAGENTA);
nuclear@0 92 default:
nuclear@0 93 break;
nuclear@0 94 }
nuclear@0 95 return 37;
nuclear@0 96 }