]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
Variadic LOG macros and fewer trailing newlines
authorOliver Kurth <okurth@vmware.com>
Mon, 28 Oct 2019 23:12:39 +0000 (16:12 -0700)
committerOliver Kurth <okurth@vmware.com>
Mon, 28 Oct 2019 23:12:39 +0000 (16:12 -0700)
It's (long past) time we started using variadic LOG macros
and stopped requiring a newline at the end of every format
string.  A previous removed the newline requirement recently.

-   LOG(N, ("format string\n", arg1, arg2))
+   LOG(N, "format string", arg1, arg2)

The important parts of this change are buried in macro madness.
The key bit is the LOG_BYNAME macro, which now can be written
to be variadic.

To support both styles simultaneously, this change adds a macro
LOGLEVEL_VARIADIC which switches the definition of LOG_BYNAME to
variadic (e.g. remove extra parens).

Following this change, we can convert files to the variadic version
and set LOGLEVEL_VARIADIC.

open-vm-tools/lib/include/loglevel_defs.h

index f7934dac37d50f76db30c4719e435f46314258ea..a06f63ffbebfe73a52f33f4c1a22cb8c8bbad9c6 100644 (file)
@@ -87,8 +87,25 @@ int LogLevel_Set(const char *extension, const char *module, int val);
 #define DOLOG_BYNAME(_mod, _min) \
         UNLIKELY(LOGLEVEL_BYNAME(_mod) >= (_min))
 
+#ifdef LOGLEVEL_VARIADIC
+/*
+ * Variadic macro wrinkle: C99 says "one or more arguments"; some compilers
+ * (gcc+clang+msvc) support zero arguments, but differ in tolerating a trailing
+ * comma. Solution: include format string in variadic arguments.
+ *
+ * C++2a introduces __VA_OPT__, which would allow this definition instead:
+ * define LOG_BYNAME(_mod, _min, _fmt, ...) \
+ *        (DOLOG_BYNAME(_mod, _min) ? Log(_fmt __VA_OPT__(,) __VA_ARGS__) \
+ *                                  : (void) 0)
+ * MSVC has always ignored a spurious trailing comma, so does not need this.
+ */
+#define LOG_BYNAME(_mod, _min, ...) \
+        (DOLOG_BYNAME(_mod, _min) ? Log(__VA_ARGS__) : (void) 0)
+
+#else
 #define LOG_BYNAME(_mod, _min, _log) \
         (DOLOG_BYNAME(_mod, _min) ? (Log _log) : (void) 0)
+#endif
 
 /*
  * Default
@@ -113,7 +130,11 @@ int LogLevel_Set(const char *extension, const char *module, int val);
 
 
 #ifdef VMX86_DEVEL
+   #ifdef LOGLEVEL_VARIADIC
+   #define LOG_DEVEL(...) Log(__VA_ARGS__)
+   #else
    #define LOG_DEVEL(_x) (Log _x)
+   #endif
 #else
    #define LOG_DEVEL(...)
 #endif