ua_log_stdout.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. #ifdef UA_ENABLE_MULTITHREADING
  30. pthread_mutex_unlock(&printf_mutex);
  31. #endif
  32. UA_ByteString_deleteMembers(&t);
  33. }
  34. #if (defined(__GNUC__) && defined(__GNUC_MINOR__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 6) || \
  35. defined(__clang__)
  36. # pragma GCC diagnostic pop
  37. #endif