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