ua_log_stdout.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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", "warn", "error", "fatal"};
  13. const char *logCategoryNames[7] = {"network", "channel", "session", "server",
  14. "client", "userland", "securitypolicy"};
  15. #ifdef __clang__
  16. __attribute__((__format__(__printf__, 3 , 0)))
  17. #endif
  18. void
  19. UA_Log_Stdout(UA_LogLevel level, UA_LogCategory category,
  20. const char *msg, va_list args) {
  21. UA_String t = UA_DateTime_toString(UA_DateTime_now());
  22. #ifdef UA_ENABLE_MULTITHREADING
  23. pthread_mutex_lock(&printf_mutex);
  24. #endif
  25. printf("[%.23s] %s/%s\t", t.data, logLevelNames[level], logCategoryNames[category]);
  26. vprintf(msg, args);
  27. printf("\n");
  28. fflush(stdout);
  29. #ifdef UA_ENABLE_MULTITHREADING
  30. pthread_mutex_unlock(&printf_mutex);
  31. #endif
  32. UA_ByteString_deleteMembers(&t);
  33. }