diff options
author | Frediano Ziglio <fziglio@redhat.com> | 2017-07-26 09:20:18 +0100 |
---|---|---|
committer | Frediano Ziglio <fziglio@redhat.com> | 2017-07-26 15:35:01 +0100 |
commit | 6a60c1dcccb95311d4a46a91dbded7e19fec2c95 (patch) | |
tree | 83093b47207bb0e1fc8583d587049250db4a9aa6 | |
parent | 12a4f367d8964fe17ca4ce70a93d04cf9f885517 (diff) |
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@redhat.com>
Acked-by: Christophe Fergeau <cfergeau@redhat.com>
-rw-r--r-- | common/vdlog.cpp | 5 | ||||
-rw-r--r-- | common/vdlog.h | 12 |
2 files changed, 8 insertions, 9 deletions
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) |