commit 60ae3ece71f1442f3ef6a7283da72dc98e7195d0
parent 2064b054b1037824f2ad243f9dd86ae6c03f8a14
Author: robert <robertrussell.72001@gmail.com>
Date: Wed, 10 Aug 2022 13:05:13 -0700
Document log and cleanup
Diffstat:
6 files changed, 29 insertions(+), 2 deletions(-)
diff --git a/inc/cext/alloc.h b/inc/cext/alloc.h
@@ -2,6 +2,8 @@
#include "cext/def.h"
+/* TODO: the -f allocators are a bit much; let's remove them */
+
/* A consistently-named set of memory allocators: {,e}{,re}alloc{,n,f}{,z}
* e- => allocation failures are fatal
* re- => realloc-style allocator
diff --git a/inc/cext/def.h b/inc/cext/def.h
@@ -92,6 +92,7 @@ typedef unsigned __int128 u128;
#endif
+/* TODO: change to i32 so sign can be used */
#define RUNE_BAD RUNE_C(0xFFFD)
#define RUNE_MAX RUNE_C(0x10FFFF)
#define RUNE_C U32_C
diff --git a/inc/cext/log.h b/inc/cext/log.h
@@ -7,5 +7,25 @@
#define errorf(...) cext_log(__FILE__, __LINE__, 2, "ERROR", "\x1b[31m", 0, __VA_ARGS__)
#define fatalf(...) cext_log(__FILE__, __LINE__, 3, "FATAL", "\x1b[31m", 1, __VA_ARGS__)
+/* Set global settings for cext_log.
+ * color: <0 force disables color, 0 detects if color should be used,
+ * and >0 force enables color
+ * log_time: enable/disable timestamps in log messages
+ * log_loc: enable/disable source location in log messages
+ * min_level: output log messages iff their level is at least this value
+ * The color detection happens when cext_log_init is called, not on each
+ * subsequent invocation of cext_log.
+ *
+ * cext_log_init can be called multiple times, but it is not thread-safe.
+ * Typically, you should call cext_log_init just once and before starting
+ * multiple threads.
+ *
+ * Calling cext_log_init is optional. By default, color is off, time and source
+ * location are not logged, and the minimum log level is set to 0 (so
+ * everything is logged). */
void cext_log_init(int color, bool log_time, bool log_loc, int min_level);
+
+/* Log a message to stderr. See definition of infof, warnf, etc. for usage.
+ *
+ * cext_log is thread-safe. */
void cext_log(char *file, int line, int level, char *name, char *color, int code, char *fmt, ...);
diff --git a/inc/cext/opt.h b/inc/cext/opt.h
@@ -2,6 +2,9 @@
#include <stdbool.h>
+/* TODO: this isn't very easy to use; a declarative-style option parser would
+ * be better */
+
typedef struct opt_ctx {
char s, *l; /* Short/long opt; l is "" if s != 0 */
bool avail; /* Argument for option is available */
diff --git a/src/alloc.c b/src/alloc.c
@@ -2,8 +2,8 @@
#include <stdlib.h>
#include <string.h>
-#include "cext/cext.h"
#include "cext/alloc.h"
+#include "cext/cext.h"
#include "cext/log.h"
/* If s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW, then s1*s2 <= USIZE_MAX
diff --git a/src/log.c b/src/log.c
@@ -1,4 +1,5 @@
-#define _POSIX_C_SOURCE 199506L
+#define _POSIX_C_SOURCE 199506L /* flockfile, funlockfile */
+
#include <stdio.h>
#include <stdlib.h>
#include <time.h>