2 Error handling functions.
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) {
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");
33 va_start(arg
, format
);
34 vfprintf(stderr
, format
, arg
);
38 va_start(arg
, format
);
39 vfprintf(log
, format
, arg
);
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
);
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) {
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
);
92 fputs("\n SDL: ", stream
);
93 fputs(SDL_GetError(), stream
);
99 void write_string(const char *data
, int sdlerror
, const char *file
,
102 FILE *log
= fopen(LOG_FILE
, "at");
104 write_message_stream(0, data
, sdlerror
, stderr
, file
, line
);
107 write_message_stream(0, data
, sdlerror
, log
, file
, line
);
113 void write_message(size_t len
, int sdlerror
, const char *file
, int line
,
114 const char *format
, ...) {
117 char *buffer
= malloc(len
+ 1);
119 va_start(arg
, format
);
120 vsprintf(buffer
, format
, arg
);
123 write_string(buffer
, sdlerror
, file
, line
);
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) {
142 static const char *get_error_type(enum error_type_t type
,
143 enum error_flag_t flag
) {
146 case ERROR_TYPE_WARNING
:
148 case ERROR_TYPE_FATAL
:
149 return "Fatal error";
150 case ERROR_TYPE_SETTING
:
151 return "Settings error";
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
);
179 /*static void log_string_stream(FILE *stream, const char *format, va_list arg) {
182 vfprintf(stream, format, arg);
185 void log_string(const char *format, ...) {
188 va_start(arg, format);
189 log_string_stream(stream, format, 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();
200 va_start(arg
, format
);
201 log_message_stream(stderr
, type
, flag
, file
, line
, date
, format
, arg
);
205 va_start(arg
, format
);
206 log_message_stream(log
, type
, flag
, file
, line
, date
, format
, 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
);