Browse Source

use va_list in logging API (necessary for static inline functions)

Julius Pfrommer 7 years ago
parent
commit
aebb8d273e
3 changed files with 19 additions and 16 deletions
  1. 1 1
      include/ua_log.h
  2. 15 14
      plugins/ua_log_stdout.c
  3. 3 1
      plugins/ua_log_stdout.h

+ 1 - 1
include/ua_log.h

@@ -61,7 +61,7 @@ typedef enum {
  * Do not use the logger directly but make use of the following macros that take
  * the minimum log-level defined in ua_config.h into account. */
 typedef void (*UA_Logger)(UA_LogLevel level, UA_LogCategory category,
-                          const char *msg, ...);
+                          const char *msg, va_list args);
 
 static inline void
 UA_LOG_TRACE(UA_Logger logger, UA_LogCategory category, const char *msg, ...) {

+ 15 - 14
plugins/ua_log_stdout.c

@@ -7,25 +7,26 @@
 #include "ua_types_generated.h"
 #include "ua_types_generated_handling.h"
 
+#ifdef UA_ENABLE_MULTITHREADING
+#include <pthread.h>
+static pthread_mutex_t printf_mutex = PTHREAD_MUTEX_INITIALIZER;
+#endif
+
 const char *LogLevelNames[6] = {"trace", "debug", "info", "warning", "error", "fatal"};
 const char *LogCategoryNames[6] = {"network", "channel", "session", "server", "client", "userland"};
 
-#if (defined(__GNUC__) && defined(__GNUC_MINOR__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 6) || defined(__clang__)
-# pragma GCC diagnostic push
-# pragma GCC diagnostic ignored "-Wformat-nonliteral"
-#endif
-
-void UA_Log_Stdout(UA_LogLevel level, UA_LogCategory category, const char *msg, ...) {
+void
+UA_Log_Stdout(UA_LogLevel level, UA_LogCategory category,
+              const char *msg, va_list args) {
     UA_String t = UA_DateTime_toString(UA_DateTime_now());
+#ifdef UA_ENABLE_MULTITHREADING
+    pthread_mutex_lock(&printf_mutex);
+#endif
     printf("[%.23s] %s/%s\t", t.data, LogLevelNames[level], LogCategoryNames[category]);
     UA_ByteString_deleteMembers(&t);
-    va_list ap;
-    va_start(ap, msg);
-    vprintf(msg, ap);
-    va_end(ap);
+    vprintf(msg, args);
     printf("\n");
-}
-
-#if (defined(__GNUC__) && defined(__GNUC_MINOR__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 6) || defined(__clang__)
-# pragma GCC diagnostic pop
+#ifdef UA_ENABLE_MULTITHREADING
+    pthread_mutex_unlock(&printf_mutex);
 #endif
+}

+ 3 - 1
plugins/ua_log_stdout.h

@@ -11,7 +11,9 @@
 extern "C" {
 #endif
 
-UA_EXPORT void UA_Log_Stdout(UA_LogLevel level, UA_LogCategory category, const char *msg, ...);
+UA_EXPORT void
+UA_Log_Stdout(UA_LogLevel level, UA_LogCategory category,
+              const char *msg, va_list args);
 
 #ifdef __cplusplus
 }