logger_stdout.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. #define LOG_FUNCTION(LEVEL) \
  16. static void log_##LEVEL(UA_LoggerCategory category, const char *msg, ...) { \
  17. printf("["); \
  18. print_time(); \
  19. va_list ap; \
  20. va_start(ap, msg); \
  21. printf("] " #LEVEL "/%s\t", UA_LoggerCategoryNames[category]); \
  22. vprintf(msg, ap); \
  23. printf("\n"); \
  24. va_end(ap); \
  25. }
  26. #if defined(__GNUC__) || defined(__clang__)
  27. #pragma GCC diagnostic push
  28. #pragma GCC diagnostic ignored "-Wformat-nonliteral"
  29. #endif
  30. LOG_FUNCTION(trace)
  31. LOG_FUNCTION(debug)
  32. LOG_FUNCTION(info)
  33. LOG_FUNCTION(warning)
  34. LOG_FUNCTION(error)
  35. LOG_FUNCTION(fatal)
  36. #if defined(__GNUC__) || defined(__clang__)
  37. #pragma GCC diagnostic pop
  38. #endif
  39. UA_Logger Logger_Stdout_new(void) {
  40. return (UA_Logger){
  41. .log_trace = log_trace,
  42. .log_debug = log_debug,
  43. .log_info = log_info,
  44. .log_warning = log_warning,
  45. .log_error = log_error,
  46. .log_fatal = log_fatal
  47. };
  48. }