logger_stdout.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. * This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  3. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information.
  4. */
  5. #include <stdio.h>
  6. #include <stdarg.h>
  7. #include "logger_stdout.h"
  8. static void print_time(void) {
  9. UA_DateTime now = UA_DateTime_now();
  10. UA_ByteString str;
  11. UA_DateTime_toString(now, &str);
  12. printf("%.27s", str.data); //a bit hacky way not to display nanoseconds
  13. UA_ByteString_deleteMembers(&str);
  14. }
  15. const char *LogLevelNames[6] = {"trace", "debug", "info", "warning", "error", "fatal"};
  16. const char *LogCategoryNames[4] = {"communication", "server", "client", "userland"};
  17. #if ((__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4 || defined(__clang__))
  18. #pragma GCC diagnostic push
  19. #pragma GCC diagnostic ignored "-Wformat-nonliteral"
  20. #endif
  21. static void Logger_Stdout(UA_LogLevel level, UA_LogCategory category, const char *msg, ...) {
  22. printf("[");
  23. print_time();
  24. va_list ap;
  25. va_start(ap, msg);
  26. printf("] %s/%s\t", LogLevelNames[level], LogCategoryNames[category]);
  27. vprintf(msg, ap);
  28. printf("\n");
  29. va_end(ap);
  30. }
  31. #if ((__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4 || defined(__clang__))
  32. #pragma GCC diagnostic pop
  33. #endif
  34. UA_Logger Logger_Stdout_new(void) {
  35. return Logger_Stdout;
  36. }