fmtlog.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. // 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. // fmt::format(fg(fmt::color::green), "Hello, {}!\n", _log_time);
  67. // }
  68. #endif /* FMTLOG_H */