diff options
author | minus <minus@mnus.de> | 2015-08-18 23:38:34 +0200 |
---|---|---|
committer | minus <minus@mnus.de> | 2015-08-18 23:38:34 +0200 |
commit | faccaf6112d923533512e1dd868ec4bf0d30e1b5 (patch) | |
tree | 2bbbffaadd3f390e0228e0d687f0a3b6c58bd3ba /sway/log.c | |
parent | Improve README (more) (diff) | |
download | sway-faccaf6112d923533512e1dd868ec4bf0d30e1b5.tar.gz sway-faccaf6112d923533512e1dd868ec4bf0d30e1b5.tar.zst sway-faccaf6112d923533512e1dd868ec4bf0d30e1b5.zip |
added sway_assert function
returns false on a failed assertion in release mode and raises SIGABRT in debug mode
Diffstat (limited to 'sway/log.c')
-rw-r--r-- | sway/log.c | 22 |
1 files changed, 20 insertions, 2 deletions
@@ -4,6 +4,7 @@ | |||
4 | #include <stdlib.h> | 4 | #include <stdlib.h> |
5 | #include <fcntl.h> | 5 | #include <fcntl.h> |
6 | #include <unistd.h> | 6 | #include <unistd.h> |
7 | #include <signal.h> | ||
7 | 8 | ||
8 | int colored = 1; | 9 | int colored = 1; |
9 | int v = 0; | 10 | int v = 0; |
@@ -32,7 +33,7 @@ void sway_log_colors(int mode) { | |||
32 | colored = (mode == 1) ? 1 : 0; | 33 | colored = (mode == 1) ? 1 : 0; |
33 | } | 34 | } |
34 | 35 | ||
35 | void sway_abort(char *format, ...) { | 36 | void sway_abort(const char *format, ...) { |
36 | fprintf(stderr, "ERROR: "); | 37 | fprintf(stderr, "ERROR: "); |
37 | va_list args; | 38 | va_list args; |
38 | va_start(args, format); | 39 | va_start(args, format); |
@@ -42,7 +43,7 @@ void sway_abort(char *format, ...) { | |||
42 | exit(1); | 43 | exit(1); |
43 | } | 44 | } |
44 | 45 | ||
45 | void sway_log(int verbosity, char* format, ...) { | 46 | void sway_log(int verbosity, const char* format, ...) { |
46 | if (verbosity <= v) { | 47 | if (verbosity <= v) { |
47 | int c = verbosity; | 48 | int c = verbosity; |
48 | if (c > sizeof(verbosity_colors) / sizeof(char *)) { | 49 | if (c > sizeof(verbosity_colors) / sizeof(char *)) { |
@@ -64,3 +65,20 @@ void sway_log(int verbosity, char* format, ...) { | |||
64 | fprintf(stderr, "\n"); | 65 | fprintf(stderr, "\n"); |
65 | } | 66 | } |
66 | } | 67 | } |
68 | |||
69 | bool sway_assert(bool condition, const char* format, ...) { | ||
70 | if (condition) { | ||
71 | return true; | ||
72 | } | ||
73 | |||
74 | #ifndef NDEBUG | ||
75 | raise(SIGABRT); | ||
76 | #endif | ||
77 | |||
78 | va_list args; | ||
79 | va_start(args, format); | ||
80 | sway_log(L_ERROR, format, args); | ||
81 | va_end(args); | ||
82 | |||
83 | return false; | ||
84 | } | ||