78ddd63e6c584c9409eda48b4b78f43bb2051315
[summerhack] / src / 3dengfx / src / common / err_msg.c
1 /*
2 Copyright (c) 2004, 2005 John Tsiombikas <nuclear@siggraph.org>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 */
18
19 /* Error messages and logging
20  *
21  * Author: John Tsiombikas 2004
22  */
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdarg.h>
27 #include "err_msg.h"
28
29 #ifdef _MSC_VER
30 #define vsnprintf(str, size, format, ap)        vsprintf(str, format, ap)
31 #else
32 int vsnprintf(char *str, size_t size, const char *format, va_list ap);
33 #endif
34
35 /* verbosity setting:
36  * 0 = do not output anything
37  * 1 = output only errors
38  * 2 = output errors and warnings
39  * 3 = output errors, warnings and informative messages
40  */
41
42 static int verbosity = 3;
43 static int log_verbosity = 4;
44
45 static FILE *log_file;
46
47 #define ERR_BUF_SIZE    4096
48 static char err_buf[ERR_BUF_SIZE];
49
50 #define MAX_LOG_FNAME   256
51 static char log_fname[MAX_LOG_FNAME];
52
53 static const char *default_log_fname(void) {
54 #if defined(unix) || defined(__unix__)
55         return "/tmp/3dengfx.log";
56 #elif defined(WIN32) || defined(__WIN32__)
57         return "3dengfx.log";
58 #else
59         return "3dengfx.log";
60 #endif
61 }
62
63 static int open_log_file() {
64         if(log_fname[0] == 0) {
65                 set_log_filename(default_log_fname());
66         }
67         
68         if((log_file = fopen(log_fname, "a"))) {
69                 setbuf(log_file, 0);
70         } else {
71                 int prev_lv = log_verbosity;
72                 log_verbosity = 0;
73                 warning("could not open %s for writing", log_fname);
74                 log_verbosity = prev_lv;
75         }
76
77         return log_file == 0 ? -1 : 0;
78 }
79
80 static void close_log_file() {
81         fclose(log_file);
82 }
83
84 void set_log_filename(const char *fname) {
85         strncpy(log_fname, fname, MAX_LOG_FNAME-1);
86 }
87
88 const char *get_log_filename(void) {
89         if(!*log_fname) {
90                 return default_log_fname();
91         }
92         return log_fname;
93 }
94
95 void set_verbosity(int v) {
96         verbosity = v;
97 }
98
99 void set_log_verbosity(int v) {
100         log_verbosity = v;
101 }
102
103 void error(const char *str, ...) {
104         va_list arg_list;
105
106         strcpy(err_buf, "E: ");
107                 
108         va_start(arg_list, str);
109         vsnprintf(err_buf+3, ERR_BUF_SIZE, str, arg_list);
110         va_end(arg_list);
111         
112         if(verbosity > 0) {
113                 fputs(err_buf, stderr);
114                 fputc('\n', stderr);
115         }
116
117         if(log_verbosity > 0 && open_log_file() != -1) {
118                 fputs(err_buf, log_file);
119                 fputc('\n', log_file);
120                 close_log_file();
121         }
122 }
123
124 void warning(const char *str, ...) {
125         va_list arg_list;
126
127         strcpy(err_buf, "W: ");
128         
129         va_start(arg_list, str);
130         vsnprintf(err_buf+3, ERR_BUF_SIZE, str, arg_list);
131         va_end(arg_list);
132         
133         if(verbosity > 1) {
134                 fputs(err_buf, stderr);
135                 fputc('\n', stderr);
136         }
137
138         if(log_verbosity > 1 && open_log_file() != -1) {
139                 fputs(err_buf, log_file);
140                 fputc('\n', log_file);
141                 close_log_file();
142         }
143 }
144
145 void info(const char *str, ...) {
146         va_list arg_list;
147
148         strcpy(err_buf, "I: ");
149         
150         va_start(arg_list, str);
151         vsnprintf(err_buf+3, ERR_BUF_SIZE, str, arg_list);
152         va_end(arg_list);
153         
154         if(verbosity > 2) {
155                 fputs(err_buf, stdout);
156                 fputc('\n', stdout);
157         }
158
159         if(log_verbosity > 2 && open_log_file() != -1) {
160                 fputs(err_buf, log_file);
161                 fputc('\n', log_file);
162                 close_log_file();
163         }
164 }