diff options
author | Bad Diode <bd@badd10de.dev> | 2024-06-14 19:07:41 +0200 |
---|---|---|
committer | Bad Diode <bd@badd10de.dev> | 2024-06-14 19:07:41 +0200 |
commit | f90a2480e2a4198fdc94e4635f6f86dffdbe253a (patch) | |
tree | 14bd6be4fd99929495d6afd8c27793de734f71fe /src/badlib.h | |
parent | 2cad3b5b21fe1644239b03fc8fd3c4329d98e606 (diff) | |
download | bdl-f90a2480e2a4198fdc94e4635f6f86dffdbe253a.tar.gz bdl-f90a2480e2a4198fdc94e4635f6f86dffdbe253a.zip |
Add log_int function
Diffstat (limited to 'src/badlib.h')
-rw-r--r-- | src/badlib.h | 33 |
1 files changed, 29 insertions, 4 deletions
diff --git a/src/badlib.h b/src/badlib.h index 2bc1807..f0f71aa 100644 --- a/src/badlib.h +++ b/src/badlib.h | |||
@@ -289,12 +289,19 @@ buf_pop(Buf *buf, void *dst, sz size) { | |||
289 | // A string or string view. | 289 | // A string or string view. |
290 | typedef Array Str; | 290 | typedef Array Str; |
291 | 291 | ||
292 | // Create a string view from a C literal with these. | 292 | // Create a string object from a C literal. |
293 | #define cstr(s) \ | 293 | #define cstr(s) \ |
294 | (Str) { \ | 294 | (Str) { \ |
295 | (u8 *)s, LEN(s) - 1 \ | 295 | (u8 *)(s), LEN(s) - 1 \ |
296 | } | 296 | } |
297 | 297 | ||
298 | // Create a string object from a char* array. | ||
299 | #define STR(s) \ | ||
300 | (Str){ \ | ||
301 | .mem = (u8 *)(s), \ | ||
302 | .size = strlen(s), \ | ||
303 | }; | ||
304 | |||
298 | bool | 305 | bool |
299 | str_eq(Str a, Str b) { | 306 | str_eq(Str a, Str b) { |
300 | return array_eq(a, b); | 307 | return array_eq(a, b); |
@@ -920,5 +927,23 @@ log_str(Logger *l, Str str) { | |||
920 | } | 927 | } |
921 | } | 928 | } |
922 | 929 | ||
930 | void | ||
931 | log_int(Logger *l, sz num) { | ||
932 | u8 tmp[64]; | ||
933 | u8 *end = tmp + sizeof(tmp); | ||
934 | u8 *beg = tmp; | ||
935 | sz t = num > 0 ? num : -num; | ||
936 | do { | ||
937 | *--beg = '0' + t % 10; | ||
938 | } while (t /= 10); | ||
939 | if (num < 0) { | ||
940 | *--beg = '-'; | ||
941 | } | ||
942 | Str parsed = { | ||
943 | .mem = beg, | ||
944 | .size = end - beg, | ||
945 | }; | ||
946 | log_str(l, parsed); | ||
947 | } | ||
923 | 948 | ||
924 | #endif // BADLIB_H | 949 | #endif // BADLIB_H |