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