fmtlog.h 5.1 KB

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