|
@@ -0,0 +1,79 @@
|
|
|
+#ifndef FMTLOG_H
|
|
|
+#define FMTLOG_H
|
|
|
+
|
|
|
+#include <chrono>
|
|
|
+
|
|
|
+#include "fmt/core.h"
|
|
|
+#include "fmt/chrono.h"
|
|
|
+#include "fmt/color.h"
|
|
|
+#include <regex>
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * 说明:使用方式和使用fmt的print方式一样
|
|
|
+ * 输出格式如下
|
|
|
+ * [2024-09-01 23:10:50][widget.cpp:52] 你好
|
|
|
+ */
|
|
|
+
|
|
|
+
|
|
|
+/* 取出文件名的正则表达式 */
|
|
|
+const std::regex _reg_file(R"(.*/([\S]*[\.][\S]*)$)");
|
|
|
+
|
|
|
+/* Debug输出 */
|
|
|
+#define FMTLOG_DEBUG(...) \
|
|
|
+ do{ \
|
|
|
+ auto _now = std::chrono::system_clock::now(); \
|
|
|
+ std::time_t _now_c = std::chrono::system_clock::to_time_t(_now); \
|
|
|
+ std::string _log_time = fmt::format("{:%Y-%m-%d %H:%M:%S}", fmt::localtime(_now_c)); \
|
|
|
+ std::string _log_file_src = __FILE__; \
|
|
|
+ auto _log_file = std::regex_replace(_log_file_src, _reg_file, "$1"); \
|
|
|
+ std::string _log_str = fmt::format(__VA_ARGS__); \
|
|
|
+ fmt::print(fg(fmt::color::blue), "[{}][{}:{}] {}\n",_log_time,_log_file ,__LINE__ ,_log_str); \
|
|
|
+ }while(0)
|
|
|
+
|
|
|
+/* 正常输出 */
|
|
|
+#define FMTLOG_INFO(...) \
|
|
|
+ do{ \
|
|
|
+ auto _now = std::chrono::system_clock::now(); \
|
|
|
+ std::time_t _now_c = std::chrono::system_clock::to_time_t(_now); \
|
|
|
+ std::string _log_time = fmt::format("{:%Y-%m-%d %H:%M:%S}", fmt::localtime(_now_c)); \
|
|
|
+ std::string _log_file_src = __FILE__; \
|
|
|
+ auto _log_file = std::regex_replace(_log_file_src, _reg_file, "$1"); \
|
|
|
+ std::string _log_str = fmt::format(__VA_ARGS__); \
|
|
|
+ fmt::print(fg(fmt::color::green), "[{}][{}:{}] {}\n",_log_time,_log_file ,__LINE__ ,_log_str); \
|
|
|
+ }while(0)
|
|
|
+
|
|
|
+/* Warn输出 */
|
|
|
+#define FMTLOG_WARN(...) \
|
|
|
+ do{ \
|
|
|
+ auto _now = std::chrono::system_clock::now(); \
|
|
|
+ std::time_t _now_c = std::chrono::system_clock::to_time_t(_now); \
|
|
|
+ std::string _log_time = fmt::format("{:%Y-%m-%d %H:%M:%S}", fmt::localtime(_now_c)); \
|
|
|
+ std::string _log_file_src = __FILE__; \
|
|
|
+ auto _log_file = std::regex_replace(_log_file_src, _reg_file, "$1"); \
|
|
|
+ std::string _log_str = fmt::format(__VA_ARGS__); \
|
|
|
+ fmt::print(fg(fmt::color::yellow), "[{}][{}:{}] {}\n",_log_time,_log_file ,__LINE__ ,_log_str); \
|
|
|
+ }while(0)
|
|
|
+
|
|
|
+/* 错误输出 */
|
|
|
+#define FMTLOG_ERROR(...) \
|
|
|
+ do{ \
|
|
|
+ auto _now = std::chrono::system_clock::now(); \
|
|
|
+ std::time_t _now_c = std::chrono::system_clock::to_time_t(_now); \
|
|
|
+ std::string _log_time = fmt::format("{:%Y-%m-%d %H:%M:%S}", fmt::localtime(_now_c)); \
|
|
|
+ std::string _log_file_src = __FILE__; \
|
|
|
+ auto _log_file = std::regex_replace(_log_file_src, _reg_file, "$1"); \
|
|
|
+ std::string _log_str = fmt::format(__VA_ARGS__); \
|
|
|
+ fmt::print(fg(fmt::color::red), "[{}][{}:{}] {}\n",_log_time,_log_file ,__LINE__ ,_log_str); \
|
|
|
+ }while(0)
|
|
|
+
|
|
|
+// void hello()
|
|
|
+// {
|
|
|
+// auto now = std::chrono::system_clock::now();
|
|
|
+// std::time_t now_c = std::chrono::system_clock::to_time_t(now);
|
|
|
+// std::string _log_time = fmt::format("{:%Y-%m-%d %H:%M:%S}", fmt::localtime(now_c));
|
|
|
+// fmt::print(fg(fmt::color::green), "Hello, {}!\n", _log_time);
|
|
|
+// std::regex_replace("hello", _reg_file, "$1");
|
|
|
+// }
|
|
|
+
|
|
|
+#endif /* FMTLOG_H */
|