]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: compiler: add a TOSTR() macro to turn a value into a string
authorWilly Tarreau <w@1wt.eu>
Mon, 20 Feb 2023 18:18:47 +0000 (19:18 +0100)
committerWilly Tarreau <w@1wt.eu>
Wed, 22 Feb 2023 08:10:53 +0000 (09:10 +0100)
Pretty often we have to emit a value (setting, limit etc) in an error
message, and this value is known at compile-time, and just doing this
forces to use a printf format such as "%d". Let's have a simple macro
to turn any other macro or value into a string that can be concatenated
with the rest of the string around. This simplifies error messages
production on the CLI for example.

include/haproxy/compiler.h

index 2f23c7456024281e7a34a7fad493a2ba03f5f9eb..5b9c668bb44ec8a33a4faef1c01cfcbac9493190 100644 (file)
  */
 #define ONLY_ONCE() ({ static char __cnt; !_HA_ATOMIC_XCHG(&__cnt, 1); })
 
+/* makes a string from a constant (number or macro), avoids the need for
+ * printf("%d") format just to dump a setting limit or value in an error
+ * message. We use two levels so that macros are resolved.
+ */
+#define _TOSTR(x) #x
+#define TOSTR(x) _TOSTR(x)
+
 /*
  * Gcc >= 3 provides the ability for the program to give hints to the
  * compiler about what branch of an if is most likely to be taken. This