hlog.h 3.2 KB

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