Moved the error handling functions from xuni.c into the new file error.c, and re...
[xuni.git] / src / error.c
blob0664e0968e3e653ef32c553053350142d3dfcd27
1 /*! \file error.h
2 Error handling functions.
3 */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdarg.h>
8 #include <time.h>
10 #include "graphics.h"
11 #include "error.h"
13 #if !1
14 static const char *get_ctime(void);
15 static void log_printf(const char *format, ...);
16 static void print_message(const char *type, const char *message,
17 const char *file, int line);
19 static const char *get_ctime(void) {
20 time_t tt = time(0);
22 return ctime(&tt);
25 /*! Calls vfprintf() for stderr and the file LOG_FILE with the same data.
26 @param format Format string, including optional format specifiers
27 @param ... Variables matching format specifiers
29 static void log_printf(const char *format, ...) {
30 FILE *log = fopen(LOG_FILE, "at");
31 va_list arg;
33 va_start(arg, format);
34 vfprintf(stderr, format, arg);
35 va_end(arg);
37 if(log) {
38 va_start(arg, format);
39 vfprintf(log, format, arg);
40 va_end(arg);
42 fclose(log);
46 static void print_message(const char *type, const char *message,
47 const char *file, int line) {
49 const char *sdlerror = SDL_GetError(), *t = get_ctime();
51 log_printf("xuni: %s from %s:%i at %s %s\n SDL: %s\n",
52 type, file, line, t, message, sdlerror);
55 void print_warning(const char *message, const char *file, int line) {
56 print_message("Warning", message, file, line);
59 void settings_error(const char *message, const char *sfile, int sline) {
60 const char *t = get_ctime();
62 log_printf("xuni: Error in settings file %s:%i at %s %s\n",
63 sfile, sline, t, message);
66 void fatal_error(const char *message, const char *file, int line) {
67 print_message("Fatal error", message, file, line);
69 quit_sdl();
70 exit(1);
72 #elif 0
74 static const char *get_ctime(void);
75 static void write_message_stream(const char *type, const char *data,
76 int sdlerror, FILE *stream, const char *file, int line);
78 static const char *get_ctime(void) {
79 time_t tt = time(0);
81 return ctime(&tt);
84 static void write_message_stream(const char *type, const char *data,
85 int sdlerror, FILE *stream, const char *file, int line) {
87 fputs("xuni: %s from %s line %i, at %s ",
88 type ? type : "Log message", file, line, stream);
89 fputs(data, stream);
91 if(sdlerror) {
92 fputs("\n SDL: ", stream);
93 fputs(SDL_GetError(), stream);
96 putc('\n', stream);
99 void write_string(const char *data, int sdlerror, const char *file,
100 int line) {
102 FILE *log = fopen(LOG_FILE, "at");
104 write_message_stream(0, data, sdlerror, stderr, file, line);
106 if(log) {
107 write_message_stream(0, data, sdlerror, log, file, line);
109 fclose(log);
113 void write_message(size_t len, int sdlerror, const char *file, int line,
114 const char *format, ...) {
116 va_list arg;
117 char *buffer = malloc(len + 1);
119 va_start(arg, format);
120 vsprintf(buffer, format, arg);
121 va_end(arg);
123 write_string(buffer, sdlerror, file, line);
125 free(buffer);
127 #endif
129 static const char *get_ctime(void);
130 static const char *get_error_type(enum error_type_t type,
131 enum error_flag_t flag);
132 static void log_message_stream(FILE *stream, enum error_type_t type,
133 enum error_flag_t flag, const char *file, int line, const char *date,
134 const char *format, va_list arg);
136 static const char *get_ctime(void) {
137 time_t tt = time(0);
139 return ctime(&tt);
142 static const char *get_error_type(enum error_type_t type,
143 enum error_flag_t flag) {
145 switch(type) {
146 case ERROR_TYPE_WARNING:
147 return "Warning";
148 case ERROR_TYPE_FATAL:
149 return "Fatal error";
150 case ERROR_TYPE_SETTING:
151 return "Settings error";
152 default:
153 break;
156 return "Log message";
159 static void log_message_stream(FILE *stream, enum error_type_t type,
160 enum error_flag_t flag, const char *file, int line, const char *date,
161 const char *format, va_list arg) {
163 if(!(flag & ERROR_FLAG_CONTINUED)) {
164 fprintf(stream, "xuni: %s from %s line %i, at %s ",
165 get_error_type(type, flag), file, line, date);
167 else fputs(" [continued] ", stream);
169 vfprintf(stream, format, arg);
171 if(flag & ERROR_FLAG_SDL) {
172 fputs("\n SDL: ", stream);
173 fputs(SDL_GetError(), stream);
176 putc('\n', stream);
179 /*static void log_string_stream(FILE *stream, const char *format, va_list arg) {
180 fputs(" ", stderr);
182 vfprintf(stream, format, arg);
185 void log_string(const char *format, ...) {
186 va_list arg;
188 va_start(arg, format);
189 log_string_stream(stream, format, arg);
190 va_end(arg);
193 void log_message(enum error_type_t type, enum error_flag_t flag,
194 const char *file, int line, const char *format, ...) {
196 FILE *log = fopen(LOG_FILE, "at");
197 const char *date = get_ctime();
198 va_list arg;
200 va_start(arg, format);
201 log_message_stream(stderr, type, flag, file, line, date, format, arg);
202 va_end(arg);
204 if(log) {
205 va_start(arg, format);
206 log_message_stream(log, type, flag, file, line, date, format, arg);
207 va_end(arg);
211 void settings_error(const char *message, const char *file, int line) {
212 log_message(ERROR_TYPE_SETTING, ERROR_TYPE_SETTING, __FILE__, __LINE__,
213 "Settings error in %s line %i:", file, line);
214 log_message(ERROR_TYPE_SETTING, ERROR_FLAG_CONTINUED,
215 __FILE__, __LINE__, message);