goat3dgfx

view src/logger.cc @ 18:6f82b9b6d6c3

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