2 Copyright (c) 2004, 2005 John Tsiombikas <nuclear@siggraph.org>
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.
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.
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
19 /* Error messages and logging
21 * Author: John Tsiombikas 2004
30 #define vsnprintf(str, size, format, ap) vsprintf(str, format, ap)
32 int vsnprintf(char *str, size_t size, const char *format, va_list ap);
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
42 static int verbosity = 3;
43 static int log_verbosity = 4;
45 static FILE *log_file;
47 #define ERR_BUF_SIZE 4096
48 static char err_buf[ERR_BUF_SIZE];
50 #define MAX_LOG_FNAME 256
51 static char log_fname[MAX_LOG_FNAME];
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__)
63 static int open_log_file() {
64 if(log_fname[0] == 0) {
65 set_log_filename(default_log_fname());
68 if((log_file = fopen(log_fname, "a"))) {
71 int prev_lv = log_verbosity;
73 warning("could not open %s for writing", log_fname);
74 log_verbosity = prev_lv;
77 return log_file == 0 ? -1 : 0;
80 static void close_log_file() {
84 void set_log_filename(const char *fname) {
85 strncpy(log_fname, fname, MAX_LOG_FNAME-1);
88 const char *get_log_filename(void) {
90 return default_log_fname();
95 void set_verbosity(int v) {
99 void set_log_verbosity(int v) {
103 void error(const char *str, ...) {
106 strcpy(err_buf, "E: ");
108 va_start(arg_list, str);
109 vsnprintf(err_buf+3, ERR_BUF_SIZE, str, arg_list);
113 fputs(err_buf, stderr);
117 if(log_verbosity > 0 && open_log_file() != -1) {
118 fputs(err_buf, log_file);
119 fputc('\n', log_file);
124 void warning(const char *str, ...) {
127 strcpy(err_buf, "W: ");
129 va_start(arg_list, str);
130 vsnprintf(err_buf+3, ERR_BUF_SIZE, str, arg_list);
134 fputs(err_buf, stderr);
138 if(log_verbosity > 1 && open_log_file() != -1) {
139 fputs(err_buf, log_file);
140 fputc('\n', log_file);
145 void info(const char *str, ...) {
148 strcpy(err_buf, "I: ");
150 va_start(arg_list, str);
151 vsnprintf(err_buf+3, ERR_BUF_SIZE, str, arg_list);
155 fputs(err_buf, stdout);
159 if(log_verbosity > 2 && open_log_file() != -1) {
160 fputs(err_buf, log_file);
161 fputc('\n', log_file);