6 #if defined(unix) || defined(__unix__) || defined(__APPLE__)
12 #define UI_MSG_TIMEOUT 4000
14 enum { LOG_INFO, LOG_WARNING, LOG_ERROR, LOG_FATAL, LOG_DEBUG };
16 static int typecolor(int type);
17 static const char *typeprefix(int type);
19 static FILE *fp = stdout;
22 static void logmsg(int type, const char *fmt, va_list ap)
28 #if defined(unix) || defined(__unix__) || (defined(__APPLE__) && !defined(TARGET_IPHONE))
29 if(isatty(fileno(fp)) && type != LOG_INFO) {
30 int c = typecolor(type);
31 fprintf(fp, "\033[%dm", c);
32 vfprintf(fp, fmt, ap);
33 fprintf(fp, "\033[0m");
37 fprintf(fp, "%s", typeprefix(type));
38 vfprintf(fp, fmt, ap);
40 if(type == LOG_ERROR || type == LOG_FATAL || type == LOG_DEBUG) {
47 fprintf(logfile, "%s", typeprefix(type));
48 vfprintf(logfile, fmt, ap);
52 if(type == LOG_FATAL) {
53 static char msgbuf[1024];
54 vsnprintf(msgbuf, sizeof msgbuf - 1, fmt, ap_orig);
55 msgbuf[sizeof msgbuf - 1] = 0;
56 MessageBox(0, msgbuf, "Fatal error", MB_OK | MB_ICONSTOP);
63 static void close_logfile(void)
65 if(logfile) fclose(logfile);
68 extern "C" void set_log_file(const char *fname)
73 logfile = fopen(fname, "w");
76 atexit(close_logfile);
81 extern "C" void info_log(const char *fmt, ...)
86 logmsg(LOG_INFO, fmt, ap);
90 extern "C" void warning_log(const char *fmt, ...)
95 logmsg(LOG_WARNING, fmt, ap);
99 show_messagev(UI_MSG_TIMEOUT, Vec3(1.0, 0.8, 0.1), fmt, ap);
103 extern "C" void error_log(const char *fmt, ...)
108 logmsg(LOG_ERROR, fmt, ap);
112 show_messagev(UI_MSG_TIMEOUT, Vec3(1.0, 0.1, 0.1), fmt, ap);
116 extern "C" void fatal_log(const char *fmt, ...)
121 logmsg(LOG_FATAL, fmt, ap);
125 extern "C" void debug_log(const char *fmt, ...)
130 logmsg(LOG_DEBUG, fmt, ap);
145 #define ANSI_FGCOLOR(x) (30 + (x))
146 #define ANSI_BGCOLOR(x) (40 + (x))
148 static int typecolor(int type)
152 return ANSI_FGCOLOR(RED);
154 return ANSI_FGCOLOR(RED); // TODO differentiate from LOG_ERROR
156 return ANSI_FGCOLOR(YELLOW);
158 return ANSI_FGCOLOR(MAGENTA);
165 static const char *typeprefix(int type)