stf_log.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2021 StarFive Technology Co., Ltd.
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <stdarg.h>
  9. #include "stf_log.h"
  10. #define ANSI_COLOR_ERR "\x1b[31m" // RED
  11. #define ANSI_COLOR_TRACE "\x1b[32m" // GREEN
  12. #define ANSI_COLOR_WARN "\x1b[33m" // YELLOW
  13. #define ANSI_COLOR_BLUE "\x1b[34m" // BLUE
  14. #define ANSI_COLOR_INFO ""
  15. // For future
  16. #define ANSI_COLOR_MAGENTA "\x1b[35m" // MAGENTA
  17. #define ANSI_COLOR_CYAN "\x1b[36m" // CYAN
  18. #define ANSI_COLOR_RESET "\x1b[0m" // RESET
  19. #define MAX_PRINT_LENGTH 512
  20. #define STF_LOG_FILE_PATH "./STF_ErrorLog.txt"
  21. #define STF_LOG_ENV_VARIABLE "V4L2_DEBUG"
  22. // static unsigned int log_decor = LOG_HAS_TIME | LOG_HAS_FILE | LOG_HAS_COLOR;
  23. static unsigned int log_decor = LOG_HAS_TIME | LOG_HAS_COLOR;
  24. static FILE *fpLog = NULL;
  25. static int max_log_level = STF_LEVEL_ERR;
  26. int init_log()
  27. {
  28. char *strDebug_level = NULL;
  29. int level;
  30. if ((log_decor & LOG_HAS_FILE) && !fpLog) {
  31. fpLog = fopen(STF_LOG_FILE_PATH, "w");
  32. }
  33. strDebug_level = getenv(STF_LOG_ENV_VARIABLE);
  34. if (strDebug_level) {
  35. level = atoi(strDebug_level);
  36. if (level >=0) {
  37. max_log_level = level;
  38. }
  39. }
  40. return 0;
  41. }
  42. void deinit_log()
  43. {
  44. if (fpLog) {
  45. fclose(fpLog);
  46. fpLog = NULL;
  47. }
  48. }
  49. void set_maxLogLevel(int level)
  50. {
  51. max_log_level = level;
  52. }
  53. int get_maxLogLevel()
  54. {
  55. return max_log_level;
  56. }
  57. void logmsg(int level, const char *format, ...)
  58. {
  59. va_list ptr;
  60. char logBuf[MAX_PRINT_LENGTH] = {0};
  61. char* prefix = "";
  62. char* postfix= "";
  63. if (level > max_log_level)
  64. return;
  65. if ((log_decor & LOG_HAS_COLOR)) {
  66. postfix = ANSI_COLOR_RESET;
  67. switch (level) {
  68. case STF_LEVEL_ERR: prefix = ANSI_COLOR_ERR "[ERROR]"; break;
  69. case STF_LEVEL_WARN: prefix = ANSI_COLOR_WARN"[WARN ]"; break;
  70. case STF_LEVEL_INFO: prefix = ANSI_COLOR_INFO"[INFO ]"; break;
  71. case STF_LEVEL_DEBUG: prefix = ANSI_COLOR_INFO"[DEBUG]"; break;
  72. case STF_LEVEL_LOG: prefix = ANSI_COLOR_INFO"[LOG ]"; break;
  73. case STF_LEVEL_TRACE: prefix = ANSI_COLOR_TRACE"[TRACE]"; break;
  74. default: prefix = ""; break;
  75. }
  76. }
  77. va_start(ptr, format);
  78. vsnprintf(logBuf, MAX_PRINT_LENGTH, format, ptr);
  79. va_end(ptr);
  80. fputs(prefix, stderr);
  81. fputs(logBuf, stderr);
  82. fputs(postfix, stderr);
  83. if ((log_decor & LOG_HAS_FILE) && fpLog) {
  84. fwrite(logBuf, strlen(logBuf), 1,fpLog);
  85. fflush(fpLog);
  86. }
  87. }