sample_log.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**********************************************************************************
  2. *
  3. * Copyright (c) 2019-2022 Beijing AXera Technology Co., Ltd. All Rights Reserved.
  4. *
  5. * This source file is the property of Beijing AXera Technology Co., Ltd. and
  6. * may not be copied or distributed in any isomorphic form without the prior
  7. * written consent of Beijing AXera Technology Co., Ltd.
  8. *
  9. **********************************************************************************/
  10. #ifndef _SAMPLE_LOG_H_
  11. #define _SAMPLE_LOG_H_
  12. #include <stdio.h>
  13. #include "ax_base_type.h"
  14. typedef enum {
  15. SAMPLE_LOG_MIN = -1,
  16. SAMPLE_LOG_EMERGENCY = 0,
  17. SAMPLE_LOG_ALERT = 1,
  18. SAMPLE_LOG_CRITICAL = 2,
  19. SAMPLE_LOG_ERROR = 3,
  20. SAMPLE_LOG_WARN = 4,
  21. SAMPLE_LOG_NOTICE = 5,
  22. SAMPLE_LOG_INFO = 6,
  23. SAMPLE_LOG_DEBUG = 7,
  24. SAMPLE_LOG_MAX
  25. } SAMPLE_LOG_LEVEL_E;
  26. static SAMPLE_LOG_LEVEL_E log_level = SAMPLE_LOG_NOTICE;
  27. #if 1
  28. #define MACRO_BLACK "\033[1;30;30m"
  29. #define MACRO_RED "\033[1;30;31m"
  30. #define MACRO_GREEN "\033[1;30;32m"
  31. #define MACRO_YELLOW "\033[1;30;33m"
  32. #define MACRO_BLUE "\033[1;30;34m"
  33. #define MACRO_PURPLE "\033[1;30;35m"
  34. #define MACRO_WHITE "\033[1;30;37m"
  35. #define MACRO_END "\033[0m"
  36. #else
  37. #define MACRO_BLACK
  38. #define MACRO_RED
  39. #define MACRO_GREEN
  40. #define MACRO_YELLOW
  41. #define MACRO_BLUE
  42. #define MACRO_PURPLE
  43. #define MACRO_WHITE
  44. #define MACRO_END
  45. #endif
  46. static inline struct timespec sample_log_get(AX_VOID)
  47. {
  48. struct timespec ts;
  49. clock_gettime(CLOCK_MONOTONIC, &ts);
  50. return ts;
  51. }
  52. #define ALOGE(fmt, ...) \
  53. do { \
  54. struct timespec ts = sample_log_get(); \
  55. printf(MACRO_RED "[%ld.%09lld][E][%32s][%4d]: " fmt MACRO_END "\n", ts.tv_sec, (AX_U64)ts.tv_nsec, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
  56. } while (0)
  57. #define ALOGW(fmt, ...) \
  58. do { \
  59. if (log_level >= SAMPLE_LOG_WARN) { \
  60. struct timespec ts = sample_log_get(); \
  61. printf(MACRO_YELLOW "[%ld.%09lld][W][%32s][%4d]: " fmt MACRO_END "\n", ts.tv_sec, (AX_U64)ts.tv_nsec, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
  62. } \
  63. } while (0)
  64. #define ALOGI(fmt, ...) \
  65. do { \
  66. if (log_level >= SAMPLE_LOG_INFO) { \
  67. struct timespec ts = sample_log_get(); \
  68. printf(MACRO_GREEN "[%ld.%09lld][I][%32s][%4d]: " fmt MACRO_END "\n", ts.tv_sec, (AX_U64)ts.tv_nsec, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
  69. } \
  70. } while (0)
  71. #define ALOGD(fmt, ...) \
  72. do { \
  73. if (log_level >= SAMPLE_LOG_DEBUG) { \
  74. struct timespec ts = sample_log_get(); \
  75. printf(MACRO_WHITE "[%ld.%09lld][D][%32s][%4d]: " fmt MACRO_END "\n", ts.tv_sec, (AX_U64)ts.tv_nsec, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
  76. } \
  77. } while (0)
  78. #define ALOGN(fmt, ...) \
  79. do { \
  80. if (log_level >= SAMPLE_LOG_NOTICE) { \
  81. struct timespec ts = sample_log_get(); \
  82. printf(MACRO_PURPLE "[%ld.%09lld][N][%32s][%4d]: " fmt MACRO_END "\n", ts.tv_sec, (AX_U64)ts.tv_nsec, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
  83. } \
  84. } while (0)
  85. #endif /* _SAMPLE_LOG_H_ */