[Spice-devel] [vdagent-win PATCH v2 1/3] Make VDLog::printf static

Frediano Ziglio fziglio at redhat.com
Wed Jul 26 11:49:15 UTC 2017


VDLog::printf is called only using a VDLog pointer.
Since this class is a singleton, there can only be one instance of it,
so LOG() does not need really need to deal with it, VDLog::printf can
do that instead.
This simplify LOG macros not having to choose between printf and
VDLog::printf.
The manual GNU __attribute__ is used as the format to use is
not the standard __printf__ format used in SPICE_GNUC_PRINTF
but you need to use the gnu_printf format. For instance the "%llu"
syntax is accepted by gnu_print format but not by __printf__ one.
MinGW C++ by default uses gnu_printf format for all formatting
functions.

Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
 common/vdlog.cpp |  5 +++--
 common/vdlog.h   | 12 +++++-------
 2 files changed, 8 insertions(+), 9 deletions(-)

Changes since v1:
- improve comment

diff --git a/common/vdlog.cpp b/common/vdlog.cpp
index b9ae93f..8af6dcc 100644
--- a/common/vdlog.cpp
+++ b/common/vdlog.cpp
@@ -70,12 +70,13 @@ VDLog* VDLog::get(TCHAR* path)
 
 void VDLog::printf(const char* format, ...)
 {
+    FILE *fh = _log ? _log->_handle : stdout;
     va_list args;
 
     va_start(args, format);
-    vfprintf(_handle, format, args);
+    vfprintf(fh, format, args);
     va_end(args);
-    fflush(_handle);
+    fflush(fh);
 }
 
 void log_version()
diff --git a/common/vdlog.h b/common/vdlog.h
index e645409..b1be391 100644
--- a/common/vdlog.h
+++ b/common/vdlog.h
@@ -31,7 +31,10 @@ class VDLog {
 public:
     ~VDLog();
     static VDLog* get(TCHAR* path = NULL);
-    void printf(const char* format, ...);
+#ifdef __GNUC__
+    __attribute__((__format__ (gnu_printf, 1, 2)))
+#endif
+    static void printf(const char* format, ...);
 
 private:
     VDLog(FILE* handle);
@@ -61,7 +64,6 @@ static const VDLogLevel log_level = LOG_INFO;
 
 #define LOG(type, format, ...) do {                                     \
     if (type >= log_level && type <= LOG_FATAL) {                       \
-        VDLog* log = VDLog::get();                                      \
         const char *type_as_char[] = { "DEBUG", "INFO", "WARN", "ERROR", "FATAL" }; \
         struct _timeb now;                                              \
         struct tm today;                                                \
@@ -69,11 +71,7 @@ static const VDLogLevel log_level = LOG_INFO;
         _ftime_s(&now);                                                 \
         localtime_s(&today, &now.time);                                 \
         strftime(datetime_str, 20, "%Y-%m-%d %H:%M:%S", &today);        \
-        if (log) {                                                      \
-            log->PRINT_LINE(type_as_char[type], format, datetime_str, now.millitm, ## __VA_ARGS__); \
-        } else {                                                        \
-            PRINT_LINE(type_as_char[type], format, datetime_str, now.millitm, ## __VA_ARGS__); \
-        }                                                               \
+        VDLog::PRINT_LINE(type_as_char[type], format, datetime_str, now.millitm, ## __VA_ARGS__); \
     }                                                                   \
 } while(0)
 
-- 
2.13.3



More information about the Spice-devel mailing list