| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 | #ifndef FMTLOG_H#define FMTLOG_H#include <chrono>// #include "fmt/format.h"// #include "fmt/chrono.h"// #include "fmt/color.h"#include "fmt/bundled/format.h"#include "chrono.h"#include "fmt/bundled/color.h"#include <regex>/** * 说明:使用方式和使用fmt的print方式一样 *      输出格式如下 *      [2024-09-01 23:10:50][widget.cpp:52] 你好 *//* 取出文件名的正则表达式 */#if defined (_WIN32)static const std::regex _reg_file(R"(.*\\([\S]*[\.][\S]*)$)");#elif defined(__linux__)static const std::regex _reg_file(R"(.*/([\S]*[\.][\S]*)$)");#endif/* 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, "DEBUG", _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, "INFO", _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, "WARN", _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, "ERROR", _log_file, __LINE__, _log_str); \    }while(0)/******************** 不输出换行符 *********************//* Debug输出 */#define FMTLOG_DEBUG_NON(...) \    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), "[{}][{}][{}:{}] {}", _log_time, "DEBUG", _log_file , __LINE__ , _log_str); \    }while(0)/* 正常输出 */#define FMTLOG_INFO_NON(...) \    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), "[{}][{}][{}:{}] {}", _log_time, "INFO", _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");//     fmt::format(fg(fmt::color::green), "Hello, {}!\n", _log_time);// }/** * @brief 采用单例模式 *  */// class FmtLog// {// private://     FmtLog();//     FmtLog(const FmtLog &fl) = delete;//     FmtLog &operator=(const FmtLog &fl) = delete;// public://     ~FmtLog();//     static FmtLog& getInstance()//     {//         static FmtLog fl;//         return fl;//     }//     /*  */// private://     /* 子线程函数,处理字符串 *///     void threadProcessStr();// private:// };#endif /* FMTLOG_H */
 |