1
0

hlog.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #ifndef HW_LOG_H_
  2. #define HW_LOG_H_
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. /*
  7. * hlog is thread-safe
  8. */
  9. #define CL_CLR "\033[0m" /* 恢复颜色 */
  10. #define CL_BLACK "\033[30m" /* 黑色字 */
  11. #define CL_RED "\e[1;31m" /* 红色字 */
  12. #define CL_GREEN "\e[1;32m" /* 绿色字 */
  13. #define CL_YELLOW "\e[1;33m" /* 黄色字 */
  14. #define CL_BLUE "\033[34m" /* 蓝色字 */
  15. #define CL_PURPLE "\e[1;35m" /* 紫色字 */
  16. #define CL_SKYBLUE "\e[1;36m" /* 天蓝字 */
  17. #define CL_WHITE "\033[37m" /* 白色字 */
  18. #define CL_BLK_WHT "\033[40;37m" /* 黑底白字 */
  19. #define CL_RED_WHT "\033[41;37m" /* 红底白字 */
  20. #define CL_GRE_WHT "\033[42;37m" /* 绿底白字 */
  21. #define CL_YEW_WHT "\033[43;37m" /* 黄底白字 */
  22. #define CL_BLUE_WHT "\033[44;37m" /* 蓝底白字 */
  23. #define CL_PPL_WHT "\033[45;37m" /* 紫底白字 */
  24. #define CL_SKYB_WHT "\033[46;37m" /* 天蓝底白字 */
  25. #define CL_WHT_BLK "\033[47;30m" /* 白底黑字 */
  26. // F(id, str, clr)
  27. #define FOREACH_LOG(F) \
  28. F(LOG_LEVEL_DEBUG, "DEBUG", CL_WHITE) \
  29. F(LOG_LEVEL_INFO, "INFO ", CL_GREEN) \
  30. F(LOG_LEVEL_WARN, "WARN ", CL_YELLOW) \
  31. F(LOG_LEVEL_ERROR, "ERROR", CL_RED) \
  32. F(LOG_LEVEL_FATAL, "FATAL", CL_RED_WHT)
  33. enum LOG_LEVEL {
  34. LOG_LEVEL_VERBOSE = 0,
  35. #define ENUM_LOG_LEVEL(id, str, clr) id,
  36. FOREACH_LOG(ENUM_LOG_LEVEL)
  37. #undef ENUM_LOG_LEVEL
  38. LOG_LEVEL_SILENT
  39. };
  40. #define DEFAULT_LOG_FILE "default"
  41. #define DEFAULT_LOG_LEVEL LOG_LEVEL_VERBOSE
  42. #define DEFAULT_LOG_REMAIN_DAYS 1
  43. #define LOG_BUFSIZE (1<<13) // 8k
  44. #define MAX_LOG_FILESIZE (1<<23) // 8M
  45. int hlog_set_file(const char* file);
  46. void hlog_set_level(int level);
  47. void hlog_set_remain_days(int days);
  48. void hlog_enable_color(int on);
  49. int hlog_printf(int level, const char* fmt, ...);
  50. // NOTE: fflush cache page => disk, slow
  51. // fflush, default enable
  52. void hlog_set_fflush(int on);
  53. void hlog_fflush();
  54. #define hlogd(fmt, ...) hlog_printf(LOG_LEVEL_DEBUG, fmt " [%s:%d:%s]", ## __VA_ARGS__, __FILE__, __LINE__, __FUNCTION__)
  55. #define hlogi(fmt, ...) hlog_printf(LOG_LEVEL_INFO, fmt " [%s:%d:%s]", ## __VA_ARGS__, __FILE__, __LINE__, __FUNCTION__)
  56. #define hlogw(fmt, ...) hlog_printf(LOG_LEVEL_WARN, fmt " [%s:%d:%s]", ## __VA_ARGS__, __FILE__, __LINE__, __FUNCTION__)
  57. #define hloge(fmt, ...) hlog_printf(LOG_LEVEL_ERROR, fmt " [%s:%d:%s]", ## __VA_ARGS__, __FILE__, __LINE__, __FUNCTION__)
  58. #define hlogf(fmt, ...) hlog_printf(LOG_LEVEL_FATAL, fmt " [%s:%d:%s]", ## __VA_ARGS__, __FILE__, __LINE__, __FUNCTION__)
  59. // below for android
  60. #include "hplatform.h"
  61. #ifdef OS_ANDROID
  62. #include <android/log.h>
  63. #define LOG_TAG "JNI"
  64. #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
  65. #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
  66. #define LOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)
  67. #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
  68. #define LOGF(...) __android_log_print(ANDROID_LOG_FATAL, LOG_TAG, __VA_ARGS__)
  69. #else
  70. #define LOGD hlogd
  71. #define LOGI hlogi
  72. #define LOGW hlogw
  73. #define LOGE hloge
  74. #define LOGF hlogf
  75. #endif
  76. #ifdef __cplusplus
  77. } // extern "C"
  78. #endif
  79. #endif // HW_LOG_H_