ua_log_stdout.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  2. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
  3. #include <stdio.h>
  4. #include <stdarg.h>
  5. #include "ua_log_stdout.h"
  6. #include "ua_types_generated.h"
  7. #include "ua_types_generated_handling.h"
  8. #ifdef UA_ENABLE_MULTITHREADING
  9. #include <pthread.h>
  10. static pthread_mutex_t printf_mutex = PTHREAD_MUTEX_INITIALIZER;
  11. #endif
  12. const char *LogLevelNames[6] = {"trace", "debug", "info", "warning", "error", "fatal"};
  13. const char *LogCategoryNames[7] = {"network", "channel", "session", "server", "client", "userland", "securitypolicy"};
  14. #if (defined(__GNUC__) && defined(__GNUC_MINOR__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 6) || \
  15. defined(__clang__)
  16. # pragma GCC diagnostic push
  17. # pragma GCC diagnostic ignored "-Wformat-nonliteral"
  18. #endif
  19. #ifdef __clang__
  20. __attribute__((__format__(__printf__, 3 , 0)))
  21. #endif
  22. void
  23. UA_Log_Stdout(UA_LogLevel level, UA_LogCategory category,
  24. const char *msg, va_list args) {
  25. UA_String t = UA_DateTime_toString(UA_DateTime_now());
  26. #ifdef UA_ENABLE_MULTITHREADING
  27. pthread_mutex_lock(&printf_mutex);
  28. #endif
  29. printf("[%.23s] %s/%s\t", t.data, LogLevelNames[level], LogCategoryNames[category]);
  30. vprintf(msg, args);
  31. printf("\n");
  32. fflush(stdout);
  33. #ifdef UA_ENABLE_MULTITHREADING
  34. pthread_mutex_unlock(&printf_mutex);
  35. #endif
  36. UA_ByteString_deleteMembers(&t);
  37. }
  38. #if (defined(__GNUC__) && defined(__GNUC_MINOR__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 6) || \
  39. defined(__clang__)
  40. # pragma GCC diagnostic pop
  41. #endif