[Spice-commits] 3 commits - Makefile.am common/test-log.cpp common/vdlog.cpp common/vdlog.h test-log

Frediano Ziglio fziglio at kemper.freedesktop.org
Wed Jul 26 15:17:02 UTC 2017


 Makefile.am         |   13 +++++++++++++
 common/test-log.cpp |   52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 common/vdlog.cpp    |    5 +++--
 common/vdlog.h      |   19 ++++++++-----------
 test-log            |    3 +++
 5 files changed, 79 insertions(+), 13 deletions(-)

New commits:
commit d827d0ab51495e88631a1b296f36424fc50ed194
Author: Frediano Ziglio <fziglio at redhat.com>
Date:   Wed Jul 26 10:36:09 2017 +0100

    Add a test for logging functions
    
    Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
    Acked-by: Christophe Fergeau <cfergeau at redhat.com>

diff --git a/Makefile.am b/Makefile.am
index 38c8711..7fafb8b 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -87,6 +87,19 @@ imagetest_SOURCES =			\
 TESTS = test-png
 EXTRA_DIST += test-png
 
+check_PROGRAMS += test-log-win
+TESTS += test-log
+EXTRA_DIST += test-log
+
+test_log_win_LDFLAGS = $(AM_LDFLAGS) -Wl,--subsystem,console
+test_log_win_SOURCES =			\
+	common/vdcommon.cpp             \
+	common/vdcommon.h		\
+	common/vdlog.cpp		\
+	common/vdlog.h			\
+	common/test-log.cpp		\
+	$(NULL)
+
 deps.txt:
 	$(AM_V_GEN)rpm -qa | grep $(host_os) | sort | unix2dos > $@
 
diff --git a/common/test-log.cpp b/common/test-log.cpp
new file mode 100644
index 0000000..341b605
--- /dev/null
+++ b/common/test-log.cpp
@@ -0,0 +1,52 @@
+/*
+   Copyright (C) 2017 Red Hat, Inc.
+
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2 of
+   the License, or (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+#undef NDEBUG
+#include <assert.h>
+#include "vdlog.h"
+
+using namespace std;
+
+int main(int argc, char **argv)
+{
+    TCHAR temp_path[MAX_PATH];
+    assert(GetTempPath(ARRAYSIZE(temp_path), temp_path) != 0);
+
+    TCHAR path[MAX_PATH];
+    assert(GetTempFileName(temp_path, TEXT("tst"), 0, path) != 0);
+
+    VDLog *log = VDLog::get(path);
+    assert(log);
+
+    log_version();
+    vd_printf("Log something");
+    log->printf("A number %d", 123456);
+    delete log;
+
+    FILE *f = _wfopen(path, L"r");
+    assert(f);
+    char line[1024];
+    assert(fgets(line, sizeof(line), f) != NULL);
+    assert(strstr(line, "log_version") != NULL);
+    assert(fgets(line, sizeof(line), f) != NULL);
+    assert(strstr(line, "Log something") != NULL);
+    assert(fgets(line, sizeof(line), f) != NULL);
+    assert(strstr(line, "A number 123456") != NULL);
+    fclose(f);
+
+    DeleteFile(path);
+    return 0;
+}
diff --git a/test-log b/test-log
new file mode 100755
index 0000000..4ba2efe
--- /dev/null
+++ b/test-log
@@ -0,0 +1,3 @@
+#!/bin/bash
+
+exec wine test-log-win.exe
commit cc1534a3a0f38f55bc912992d067e310c9712981
Author: Frediano Ziglio <fziglio at redhat.com>
Date:   Wed Jul 26 09:14:07 2017 +0100

    Expand PRINT_LINE macros
    
    This complicated macro was used as an helper for LOG macro
    which expand PRINT_LINE macro twice. Now that LOG expand PRINT_LINE
    only once there's no need to have this macro which make logging code
    more complicated.
    
    Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
    Acked-by: Christophe Fergeau <cfergeau at redhat.com>

diff --git a/common/vdlog.h b/common/vdlog.h
index b1be391..9f08fc4 100644
--- a/common/vdlog.h
+++ b/common/vdlog.h
@@ -58,10 +58,6 @@ static const VDLogLevel log_level = LOG_DEBUG;
 static const VDLogLevel log_level = LOG_INFO;
 #endif
 
-#define PRINT_LINE(type, format, datetime, ms, ...)                     \
-    printf("%lu::%s::%s,%.3d::%s::" format "\n", GetCurrentThreadId(), type, datetime, ms, \
-           __FUNCTION__, ## __VA_ARGS__);
-
 #define LOG(type, format, ...) do {                                     \
     if (type >= log_level && type <= LOG_FATAL) {                       \
         const char *type_as_char[] = { "DEBUG", "INFO", "WARN", "ERROR", "FATAL" }; \
@@ -71,7 +67,10 @@ 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);        \
-        VDLog::PRINT_LINE(type_as_char[type], format, datetime_str, now.millitm, ## __VA_ARGS__); \
+        VDLog::printf("%lu::%s::%s,%.3d::%s::" format "\n",             \
+                      GetCurrentThreadId(), type_as_char[type],         \
+                      datetime_str, now.millitm,                        \
+                      __FUNCTION__, ## __VA_ARGS__);                    \
     }                                                                   \
 } while(0)
 
commit 6a60c1dcccb95311d4a46a91dbded7e19fec2c95
Author: Frediano Ziglio <fziglio at redhat.com>
Date:   Wed Jul 26 09:20:18 2017 +0100

    Make VDLog::printf static
    
    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 really need to deal with it, VDLog::printf can
    do that instead.
    This simplifies LOG macros by 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>
    Acked-by: Christophe Fergeau <cfergeau at redhat.com>

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)
 


More information about the Spice-commits mailing list