chiark / gitweb /
basic/log: add an assert that does not recurse into logging functions
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 23 Feb 2018 12:29:03 +0000 (13:29 +0100)
committerSven Eden <yamakuzure@gmx.net>
Wed, 30 May 2018 05:59:05 +0000 (07:59 +0200)
Then it can be used in the asserts in logging functions without causing
infinite recursion. The error is just printed to stderr, it should be
good enough for the common case.

src/basic/log.c

index f3bb69855086a41392e1d8e77ea53ac0f5b755c3..719702054fbfe1fd12ad463379a907f13f3558f4 100644 (file)
@@ -85,6 +85,16 @@ static bool prohibit_ipc = false;
  * use here. */
 static char *log_abort_msg = NULL;
 
+/* An assert to use in logging functions that does not call recursively
+ * into our logging functions (since that might lead to a loop). */
+#define assert_raw(expr)                                                \
+        do {                                                            \
+                if (_unlikely_(!(expr))) {                              \
+                        fputs(#expr "\n", stderr);                      \
+                        abort();                                        \
+                }                                                       \
+        } while (false)
+
 static void log_close_console(void) {
 
         if (console_fd < 0)
@@ -543,7 +553,7 @@ static int log_do_header(
                      isempty(extra) ? "" : extra,
                      isempty(extra) ? "" : "\n",
                      program_invocation_short_name);
-        assert((size_t) r < size);
+        assert_raw((size_t) r < size);
 
         return 0;
 }
@@ -597,7 +607,7 @@ int log_dispatch_internal(
                 const char *extra,
                 char *buffer) {
 
-        assert(buffer);
+        assert_raw(buffer);
 
         if (error < 0)
                 error = -error;