ua_log_stdout.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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[6] = {"network", "channel", "session", "server", "client", "userland"};
  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. void
  20. UA_Log_Stdout(UA_LogLevel level, UA_LogCategory category,
  21. const char *msg, va_list args) {
  22. UA_String t = UA_DateTime_toString(UA_DateTime_now());
  23. #ifdef UA_ENABLE_MULTITHREADING
  24. pthread_mutex_lock(&printf_mutex);
  25. #endif
  26. printf("[%.23s] %s/%s\t", t.data, LogLevelNames[level], LogCategoryNames[category]);
  27. vprintf(msg, args);
  28. printf("\n");
  29. fflush(stdout);
  30. #ifdef UA_ENABLE_MULTITHREADING
  31. pthread_mutex_unlock(&printf_mutex);
  32. #endif
  33. UA_ByteString_deleteMembers(&t);
  34. }
  35. #if (defined(__GNUC__) && defined(__GNUC_MINOR__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 6) || \
  36. defined(__clang__)
  37. # pragma GCC diagnostic pop
  38. #endif