]> git.lizzy.rs Git - nothing.git/blob - src/error.c
fc600d04d8ce0777e54e63588c5f94bfd4eaf256
[nothing.git] / src / error.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4
5 #include <SDL2/SDL.h>
6
7 #include "./error.h"
8
9 static error_type_t current_error_type = ERROR_TYPE_OK;
10
11 error_type_t current_error(void)
12 {
13     return current_error_type;
14 }
15
16 void throw_error(error_type_t error_type)
17 {
18     assert(0 <= error_type && error_type < ERROR_TYPE_N);
19     current_error_type = error_type;
20 }
21
22 void reset_error(void)
23 {
24     current_error_type = ERROR_TYPE_OK;
25 }
26
27 void print_current_error_msg(const char *user_prefix)
28 {
29     print_error_msg(current_error_type, user_prefix);
30     current_error_type = ERROR_TYPE_OK;
31 }
32
33 void print_error_msg(error_type_t error_type, const char *user_prefix)
34 {
35     switch (error_type) {
36     case ERROR_TYPE_LIBC:
37         perror(user_prefix);
38         break;
39
40     case ERROR_TYPE_SDL2:
41         fprintf(stderr, "%s: %s", user_prefix, SDL_GetError());
42         break;
43
44     default: {}
45     }
46 }