fmtlog.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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,_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,_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,_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,_log_file ,__LINE__ ,_log_str); \
  58. }while(0)
  59. // void hello()
  60. // {
  61. // auto now = std::chrono::system_clock::now();
  62. // std::time_t now_c = std::chrono::system_clock::to_time_t(now);
  63. // std::string _log_time = fmt::format("{:%Y-%m-%d %H:%M:%S}", fmt::localtime(now_c));
  64. // fmt::print(fg(fmt::color::green), "Hello, {}!\n", _log_time);
  65. // std::regex_replace("hello", _reg_file, "$1");
  66. // }
  67. #endif /* FMTLOG_H */