fmtlog.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. const std::regex _reg_file(R"(.*/([\S]*[\.][\S]*)$)");
  15. /* Debug输出 */
  16. #define FMTLOG_DEBUG(...) \
  17. do{ \
  18. auto _now = std::chrono::system_clock::now(); \
  19. std::time_t _now_c = std::chrono::system_clock::to_time_t(_now); \
  20. std::string _log_time = fmt::format("{:%Y-%m-%d %H:%M:%S}", fmt::localtime(_now_c)); \
  21. std::string _log_file_src = __FILE__; \
  22. auto _log_file = std::regex_replace(_log_file_src, _reg_file, "$1"); \
  23. std::string _log_str = fmt::format(__VA_ARGS__); \
  24. fmt::print(fg(fmt::color::blue), "[{}][{}][{}:{}] {}\n", _log_time, "DEBUG", _log_file , __LINE__ , _log_str); \
  25. }while(0)
  26. /* 正常输出 */
  27. #define FMTLOG_INFO(...) \
  28. do{ \
  29. auto _now = std::chrono::system_clock::now(); \
  30. std::time_t _now_c = std::chrono::system_clock::to_time_t(_now); \
  31. std::string _log_time = fmt::format("{:%Y-%m-%d %H:%M:%S}", fmt::localtime(_now_c)); \
  32. std::string _log_file_src = __FILE__; \
  33. auto _log_file = std::regex_replace(_log_file_src, _reg_file, "$1"); \
  34. std::string _log_str = fmt::format(__VA_ARGS__); \
  35. fmt::print(fg(fmt::color::green), "[{}][{}][{}:{}] {}\n", _log_time, "INFO", _log_file, __LINE__, _log_str); \
  36. }while(0)
  37. /* Warn输出 */
  38. #define FMTLOG_WARN(...) \
  39. do{ \
  40. auto _now = std::chrono::system_clock::now(); \
  41. std::time_t _now_c = std::chrono::system_clock::to_time_t(_now); \
  42. std::string _log_time = fmt::format("{:%Y-%m-%d %H:%M:%S}", fmt::localtime(_now_c)); \
  43. std::string _log_file_src = __FILE__; \
  44. auto _log_file = std::regex_replace(_log_file_src, _reg_file, "$1"); \
  45. std::string _log_str = fmt::format(__VA_ARGS__); \
  46. fmt::print(fg(fmt::color::yellow), "[{}][{}][{}:{}] {}\n", _log_time, "WARN", _log_file, __LINE__, _log_str); \
  47. }while(0)
  48. /* 错误输出 */
  49. #define FMTLOG_ERROR(...) \
  50. do{ \
  51. auto _now = std::chrono::system_clock::now(); \
  52. std::time_t _now_c = std::chrono::system_clock::to_time_t(_now); \
  53. std::string _log_time = fmt::format("{:%Y-%m-%d %H:%M:%S}", fmt::localtime(_now_c)); \
  54. std::string _log_file_src = __FILE__; \
  55. auto _log_file = std::regex_replace(_log_file_src, _reg_file, "$1"); \
  56. std::string _log_str = fmt::format(__VA_ARGS__); \
  57. fmt::print(fg(fmt::color::red), "[{}][{}][{}:{}] {}\n", _log_time, "ERROR", _log_file, __LINE__, _log_str); \
  58. }while(0)
  59. /******************** 不输出换行符 *********************/
  60. /* Debug输出 */
  61. #define FMTLOG_DEBUG_NON(...) \
  62. do{ \
  63. auto _now = std::chrono::system_clock::now(); \
  64. std::time_t _now_c = std::chrono::system_clock::to_time_t(_now); \
  65. std::string _log_time = fmt::format("{:%Y-%m-%d %H:%M:%S}", fmt::localtime(_now_c)); \
  66. std::string _log_file_src = __FILE__; \
  67. auto _log_file = std::regex_replace(_log_file_src, _reg_file, "$1"); \
  68. std::string _log_str = fmt::format(__VA_ARGS__); \
  69. fmt::print(fg(fmt::color::blue), "[{}][{}][{}:{}] {}", _log_time, "DEBUG", _log_file , __LINE__ , _log_str); \
  70. }while(0)
  71. /* 正常输出 */
  72. #define FMTLOG_INFO_NON(...) \
  73. do{ \
  74. auto _now = std::chrono::system_clock::now(); \
  75. std::time_t _now_c = std::chrono::system_clock::to_time_t(_now); \
  76. std::string _log_time = fmt::format("{:%Y-%m-%d %H:%M:%S}", fmt::localtime(_now_c)); \
  77. std::string _log_file_src = __FILE__; \
  78. auto _log_file = std::regex_replace(_log_file_src, _reg_file, "$1"); \
  79. std::string _log_str = fmt::format(__VA_ARGS__); \
  80. fmt::print(fg(fmt::color::green), "[{}][{}][{}:{}] {}", _log_time, "INFO", _log_file, __LINE__, _log_str); \
  81. }while(0)
  82. // void hello()
  83. // {
  84. // auto now = std::chrono::system_clock::now();
  85. // std::time_t now_c = std::chrono::system_clock::to_time_t(now);
  86. // std::string _log_time = fmt::format("{:%Y-%m-%d %H:%M:%S}", fmt::localtime(now_c));
  87. // fmt::print(fg(fmt::color::green), "Hello, {}!\n", _log_time);
  88. // std::regex_replace("hello", _reg_file, "$1");
  89. // fmt::format(fg(fmt::color::green), "Hello, {}!\n", _log_time);
  90. // }
  91. #endif /* FMTLOG_H */