fmtlog.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #ifndef FMTLOG_H
  2. #define FMTLOG_H
  3. #include <chrono>
  4. #include "fmt/core.h"
  5. #include "fmt/chrono.h"
  6. #include "fmt/color.h"
  7. #include <regex>
  8. /**
  9. * 说明:使用方式和使用fmt的print方式一样
  10. * 输出格式如下
  11. * [2024-09-01 23:10:50][widget.cpp:52] 你好
  12. */
  13. /* 取出文件名的正则表达式 */
  14. #if defined (_WIN32)
  15. const std::regex _reg_file(R"(.*\\([\S]*[\.][\S]*)$)");
  16. #elif defined(__linux__)
  17. const std::regex _reg_file(R"(.*/([\S]*[\.][\S]*)$)");
  18. #endif
  19. /* Debug输出 */
  20. #define FMTLOG_DEBUG(...) \
  21. do{ \
  22. auto _now = std::chrono::system_clock::now(); \
  23. std::time_t _now_c = std::chrono::system_clock::to_time_t(_now); \
  24. std::string _log_time = fmt::format("{:%Y-%m-%d %H:%M:%S}", fmt::localtime(_now_c)); \
  25. std::string _log_file_src = __FILE__; \
  26. auto _log_file = std::regex_replace(_log_file_src, _reg_file, "$1"); \
  27. std::string _log_str = fmt::format(__VA_ARGS__); \
  28. fmt::print(fg(fmt::color::blue), "[{}][{}][{}:{}] {}\n", _log_time, "DEBUG", _log_file , __LINE__ , _log_str); \
  29. }while(0)
  30. /* 正常输出 */
  31. #define FMTLOG_INFO(...) \
  32. do{ \
  33. auto _now = std::chrono::system_clock::now(); \
  34. std::time_t _now_c = std::chrono::system_clock::to_time_t(_now); \
  35. std::string _log_time = fmt::format("{:%Y-%m-%d %H:%M:%S}", fmt::localtime(_now_c)); \
  36. std::string _log_file_src = __FILE__; \
  37. auto _log_file = std::regex_replace(_log_file_src, _reg_file, "$1"); \
  38. std::string _log_str = fmt::format(__VA_ARGS__); \
  39. fmt::print(fg(fmt::color::green), "[{}][{}][{}:{}] {}\n", _log_time, "INFO", _log_file, __LINE__, _log_str); \
  40. }while(0)
  41. /* Warn输出 */
  42. #define FMTLOG_WARN(...) \
  43. do{ \
  44. auto _now = std::chrono::system_clock::now(); \
  45. std::time_t _now_c = std::chrono::system_clock::to_time_t(_now); \
  46. std::string _log_time = fmt::format("{:%Y-%m-%d %H:%M:%S}", fmt::localtime(_now_c)); \
  47. std::string _log_file_src = __FILE__; \
  48. auto _log_file = std::regex_replace(_log_file_src, _reg_file, "$1"); \
  49. std::string _log_str = fmt::format(__VA_ARGS__); \
  50. fmt::print(fg(fmt::color::yellow), "[{}][{}][{}:{}] {}\n", _log_time, "WARN", _log_file, __LINE__, _log_str); \
  51. }while(0)
  52. /* 错误输出 */
  53. #define FMTLOG_ERROR(...) \
  54. do{ \
  55. auto _now = std::chrono::system_clock::now(); \
  56. std::time_t _now_c = std::chrono::system_clock::to_time_t(_now); \
  57. std::string _log_time = fmt::format("{:%Y-%m-%d %H:%M:%S}", fmt::localtime(_now_c)); \
  58. std::string _log_file_src = __FILE__; \
  59. auto _log_file = std::regex_replace(_log_file_src, _reg_file, "$1"); \
  60. std::string _log_str = fmt::format(__VA_ARGS__); \
  61. fmt::print(fg(fmt::color::red), "[{}][{}][{}:{}] {}\n", _log_time, "ERROR", _log_file, __LINE__, _log_str); \
  62. }while(0)
  63. /******************** 不输出换行符 *********************/
  64. /* Debug输出 */
  65. #define FMTLOG_DEBUG_NON(...) \
  66. do{ \
  67. auto _now = std::chrono::system_clock::now(); \
  68. std::time_t _now_c = std::chrono::system_clock::to_time_t(_now); \
  69. std::string _log_time = fmt::format("{:%Y-%m-%d %H:%M:%S}", fmt::localtime(_now_c)); \
  70. std::string _log_file_src = __FILE__; \
  71. auto _log_file = std::regex_replace(_log_file_src, _reg_file, "$1"); \
  72. std::string _log_str = fmt::format(__VA_ARGS__); \
  73. fmt::print(fg(fmt::color::blue), "[{}][{}][{}:{}] {}", _log_time, "DEBUG", _log_file , __LINE__ , _log_str); \
  74. }while(0)
  75. /* 正常输出 */
  76. #define FMTLOG_INFO_NON(...) \
  77. do{ \
  78. auto _now = std::chrono::system_clock::now(); \
  79. std::time_t _now_c = std::chrono::system_clock::to_time_t(_now); \
  80. std::string _log_time = fmt::format("{:%Y-%m-%d %H:%M:%S}", fmt::localtime(_now_c)); \
  81. std::string _log_file_src = __FILE__; \
  82. auto _log_file = std::regex_replace(_log_file_src, _reg_file, "$1"); \
  83. std::string _log_str = fmt::format(__VA_ARGS__); \
  84. fmt::print(fg(fmt::color::green), "[{}][{}][{}:{}] {}", _log_time, "INFO", _log_file, __LINE__, _log_str); \
  85. }while(0)
  86. // void hello()
  87. // {
  88. // auto now = std::chrono::system_clock::now();
  89. // std::time_t now_c = std::chrono::system_clock::to_time_t(now);
  90. // std::string _log_time = fmt::format("{:%Y-%m-%d %H:%M:%S}", fmt::localtime(now_c));
  91. // fmt::print(fg(fmt::color::green), "Hello, {}!\n", _log_time);
  92. // std::regex_replace("hello", _reg_file, "$1");
  93. // fmt::format(fg(fmt::color::green), "Hello, {}!\n", _log_time);
  94. // }
  95. /**
  96. * @brief 采用单例模式
  97. *
  98. */
  99. // class FmtLog
  100. // {
  101. // private:
  102. // FmtLog();
  103. // FmtLog(const FmtLog &fl) = delete;
  104. // FmtLog &operator=(const FmtLog &fl) = delete;
  105. // public:
  106. // ~FmtLog();
  107. // static FmtLog& getInstance()
  108. // {
  109. // static FmtLog fl;
  110. // return fl;
  111. // }
  112. // /* */
  113. // private:
  114. // /* 子线程函数,处理字符串 */
  115. // void threadProcessStr();
  116. // private:
  117. // };
  118. #endif /* FMTLOG_H */