logger.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * logger.cpp
  3. *
  4. * Created on: Feb 3, 2016
  5. * Author: Philipp Hinz
  6. */
  7. #include <stdio.h>
  8. #include <stdarg.h>
  9. #include <time.h>
  10. #include "global.h"
  11. #include "logger.h"
  12. int lastlog = 0;
  13. /**
  14. * Prints a timestamp
  15. * @param stream target stream (stdout, stderr)
  16. */
  17. void logger_print_time(_IO_FILE* stream) {
  18. time_t timer;
  19. char buffer[26];
  20. struct tm* tm_info;
  21. time(&timer);
  22. tm_info = localtime(&timer);
  23. strftime(buffer, 26, "%d-%m-%Y %H:%M:%S", tm_info);
  24. fprintf(stream, "[%s] ", buffer);
  25. }
  26. /**
  27. * Outputs the printf like formatted message to stderr and colorizes it
  28. * @param format printf formatted string
  29. */
  30. void logger_error(const char* format, ...) {
  31. pthread_mutex_lock(&mutex_logger);
  32. va_list args;
  33. fprintf( stderr, KRED);
  34. if (optDate)
  35. logger_print_time(stderr);
  36. fprintf( stderr, "Error: ");
  37. va_start(args, format);
  38. vfprintf( stderr, format, args);
  39. va_end(args);
  40. fprintf( stderr, KNRM);
  41. lastlog = LOG_ERROR;
  42. pthread_mutex_unlock(&mutex_logger);
  43. }
  44. /**
  45. * Resets the logger to prevent the previous line being overdrawn.
  46. * Call after custom printf
  47. */
  48. void logger_reset() {
  49. lastlog = 0;
  50. }
  51. /**
  52. * prints the message to stdout and formats it
  53. * @param verboselevel minimum verbose level for output
  54. * @param logtype type of logmessage for coloring
  55. * @param format printf formatted string
  56. */
  57. void logger(logger_verbose_t verboselevel, logger_type_t logtype,
  58. const char* format, ...) {
  59. if (verbose < verboselevel)
  60. return;
  61. pthread_mutex_lock(&mutex_logger);
  62. switch (logtype) {
  63. case LOG_CAN:
  64. printf(KBOLD);
  65. break;
  66. case LOG_OK:
  67. printf(KGRN);
  68. break;
  69. case LOG_WARN:
  70. printf(KYEL);
  71. break;
  72. case LOG_ERROR:
  73. printf(KRED);
  74. break;
  75. case LOG_ERRORC:
  76. if (lastlog == LOG_ERRORC)
  77. printf(CLEARLINE);
  78. break;
  79. case LOG_INFO:
  80. default:
  81. break;
  82. }
  83. if (optDate)
  84. logger_print_time(stdout);
  85. va_list args;
  86. va_start(args, format);
  87. vprintf(format, args);
  88. va_end(args);
  89. if (logtype > 0)
  90. printf(KNRM);
  91. lastlog = logtype;
  92. pthread_mutex_unlock(&mutex_logger);
  93. }
  94. /**
  95. * prints the message to stdout
  96. * @param verboselevel minimum verbose level for output
  97. * @param format printf formatted string
  98. */
  99. void logger(logger_verbose_t verboselevel, const char* format, ...) {
  100. if (verbose < verboselevel)
  101. return;
  102. pthread_mutex_lock(&mutex_logger);
  103. if (optDate)
  104. logger_print_time(stdout);
  105. va_list args;
  106. va_start(args, format);
  107. vprintf(format, args);
  108. va_end(args);
  109. lastlog = 0;
  110. pthread_mutex_unlock(&mutex_logger);
  111. }