7 #if defined(unix) || defined(__unix__) || defined(__APPLE__)
13 #define UI_MSG_TIMEOUT 4000
15 enum { LOG_INFO, LOG_WARNING, LOG_ERROR, LOG_FATAL, LOG_DEBUG };
17 static int typecolor(int type);
18 static const char *typeprefix(int type);
23 static void logmsg(int type, const char *fmt, va_list ap)
31 #if defined(unix) || defined(__unix__) || (defined(__APPLE__) && !defined(TARGET_IPHONE))
32 if(isatty(fileno(fp)) && type != LOG_INFO) {
33 int c = typecolor(type);
34 fprintf(fp, "\033[%dm", c);
35 vfprintf(fp, fmt, ap);
36 fprintf(fp, "\033[0m");
40 fprintf(fp, "%s", typeprefix(type));
41 vfprintf(fp, fmt, ap);
43 if(type == LOG_ERROR || type == LOG_FATAL || type == LOG_DEBUG) {
50 fprintf(logfile, "%s", typeprefix(type));
51 vfprintf(logfile, fmt, ap);
55 if(type == LOG_FATAL) {
56 static char msgbuf[1024];
57 vsnprintf(msgbuf, sizeof msgbuf - 1, fmt, ap_orig);
58 msgbuf[sizeof msgbuf - 1] = 0;
59 MessageBox(0, msgbuf, "Fatal error", MB_OK | MB_ICONSTOP);
66 static void close_logfile(void)
68 if(logfile) fclose(logfile);
71 void set_log_file(const char *fname)
76 logfile = fopen(fname, "w");
79 atexit(close_logfile);
84 void info_log(const char *fmt, ...)
89 logmsg(LOG_INFO, fmt, ap);
93 void warning_log(const char *fmt, ...)
98 logmsg(LOG_WARNING, fmt, ap);
102 show_messagev(UI_MSG_TIMEOUT, 1.0, 0.8, 0.1, fmt, ap);
106 void error_log(const char *fmt, ...)
111 logmsg(LOG_ERROR, fmt, ap);
115 show_messagev(UI_MSG_TIMEOUT, 1.0, 0.1, 0.1, fmt, ap);
119 void fatal_log(const char *fmt, ...)
124 logmsg(LOG_FATAL, fmt, ap);
128 void debug_log(const char *fmt, ...)
133 logmsg(LOG_DEBUG, fmt, ap);
148 #define ANSI_FGCOLOR(x) (30 + (x))
149 #define ANSI_BGCOLOR(x) (40 + (x))
151 static int typecolor(int type)
155 return ANSI_FGCOLOR(RED);
157 return ANSI_FGCOLOR(RED); // TODO differentiate from LOG_ERROR
159 return ANSI_FGCOLOR(YELLOW);
161 return ANSI_FGCOLOR(MAGENTA);
168 static const char *typeprefix(int type)