From: Oliver Kurth Date: Mon, 28 Oct 2019 23:12:39 +0000 (-0700) Subject: Variadic LOG macros and fewer trailing newlines X-Git-Tag: stable-11.1.0~203 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bce9759be2c09dc506b5846ecfde3e337f629313;p=thirdparty%2Fopen-vm-tools.git Variadic LOG macros and fewer trailing newlines 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. --- diff --git a/open-vm-tools/lib/include/loglevel_defs.h b/open-vm-tools/lib/include/loglevel_defs.h index f7934dac3..a06f63ffb 100644 --- a/open-vm-tools/lib/include/loglevel_defs.h +++ b/open-vm-tools/lib/include/loglevel_defs.h @@ -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