syslog.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* Copyright 2018 Canaan Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #ifndef _SYSLOG_H
  16. #define _SYSLOG_H
  17. #include <stdint.h>
  18. #include <stdio.h>
  19. #include "encoding.h"
  20. #include "printf.h"
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. /* clang-format off */
  25. typedef enum _kendryte_log_level
  26. {
  27. LOG_NONE, /*!< No log output */
  28. LOG_ERROR, /*!< Critical errors, software module can not recover on its own */
  29. LOG_WARN, /*!< Error conditions from which recovery measures have been taken */
  30. LOG_INFO, /*!< Information messages which describe normal flow of events */
  31. LOG_DEBUG, /*!< Extra information which is not necessary for normal use (values, pointers, sizes, etc). */
  32. LOG_VERBOSE /*!< Bigger chunks of debugging information, or frequent messages which can potentially flood the output. */
  33. } kendryte_log_level_t ;
  34. /* clang-format on */
  35. /* clang-format off */
  36. #if CONFIG_LOG_COLORS
  37. #define LOG_COLOR_BLACK "30"
  38. #define LOG_COLOR_RED "31"
  39. #define LOG_COLOR_GREEN "32"
  40. #define LOG_COLOR_BROWN "33"
  41. #define LOG_COLOR_BLUE "34"
  42. #define LOG_COLOR_PURPLE "35"
  43. #define LOG_COLOR_CYAN "36"
  44. #define LOG_COLOR(COLOR) "\033[0;" COLOR "m"
  45. #define LOG_BOLD(COLOR) "\033[1;" COLOR "m"
  46. #define LOG_RESET_COLOR "\033[0m"
  47. #define LOG_COLOR_E LOG_COLOR(LOG_COLOR_RED)
  48. #define LOG_COLOR_W LOG_COLOR(LOG_COLOR_BROWN)
  49. #define LOG_COLOR_I LOG_COLOR(LOG_COLOR_GREEN)
  50. #define LOG_COLOR_D
  51. #define LOG_COLOR_V
  52. #else /* CONFIG_LOG_COLORS */
  53. #define LOG_COLOR_E
  54. #define LOG_COLOR_W
  55. #define LOG_COLOR_I
  56. #define LOG_COLOR_D
  57. #define LOG_COLOR_V
  58. #define LOG_RESET_COLOR
  59. #endif /* CONFIG_LOG_COLORS */
  60. /* clang-format on */
  61. #define LOG_FORMAT(letter, format) LOG_COLOR_##letter #letter " (%lu) %s: " format LOG_RESET_COLOR "\n"
  62. #ifdef LOG_LEVEL
  63. #undef CONFIG_LOG_LEVEL
  64. #define CONFIG_LOG_LEVEL LOG_LEVEL
  65. #endif
  66. #ifdef LOG_KERNEL
  67. #define LOG_PRINTF printk
  68. #else
  69. #define LOG_PRINTF printf
  70. #endif
  71. #ifdef CONFIG_LOG_ENABLE
  72. #define LOGE(tag, format, ...) \
  73. do \
  74. { \
  75. if(CONFIG_LOG_LEVEL >= LOG_ERROR) \
  76. LOG_PRINTF(LOG_FORMAT(E, format), read_cycle(), tag, ##__VA_ARGS__); \
  77. } while(0)
  78. #define LOGW(tag, format, ...) \
  79. do \
  80. { \
  81. if(CONFIG_LOG_LEVEL >= LOG_WARN) \
  82. LOG_PRINTF(LOG_FORMAT(W, format), read_cycle(), tag, ##__VA_ARGS__); \
  83. } while(0)
  84. #define LOGI(tag, format, ...) \
  85. do \
  86. { \
  87. if(CONFIG_LOG_LEVEL >= LOG_INFO) \
  88. LOG_PRINTF(LOG_FORMAT(I, format), read_cycle(), tag, ##__VA_ARGS__); \
  89. } while(0)
  90. #define LOGD(tag, format, ...) \
  91. do \
  92. { \
  93. if(CONFIG_LOG_LEVEL >= LOG_DEBUG) \
  94. LOG_PRINTF(LOG_FORMAT(D, format), read_cycle(), tag, ##__VA_ARGS__); \
  95. } while(0)
  96. #define LOGV(tag, format, ...) \
  97. do \
  98. { \
  99. if(CONFIG_LOG_LEVEL >= LOG_VERBOSE) \
  100. LOG_PRINTF(LOG_FORMAT(V, format), read_cycle(), tag, ##__VA_ARGS__); \
  101. } while(0)
  102. #else
  103. #define LOGE(tag, format, ...)
  104. #define LOGW(tag, format, ...)
  105. #define LOGI(tag, format, ...)
  106. #define LOGD(tag, format, ...)
  107. #define LOGV(tag, format, ...)
  108. #endif /* LOG_ENABLE */
  109. #ifdef __cplusplus
  110. }
  111. #endif
  112. #endif /* _SYSLOG_H */