ZCLibLog
载入中...
搜索中...
未找到
tp_boost-format.hpp
浏览该文件的文档.
1// Copyright 2026 CZF-H
2// Licensed under the Apache License, Version 2.0
3
4//
5// Created by wanjiangzhi on 2026/4/10.
6//
7
8#ifndef ZCLIBLOG_FORMATTERS_TP_BOOST_FORMAT_HPP
9#define ZCLIBLOG_FORMATTERS_TP_BOOST_FORMAT_HPP
10
12#include <boost/format.hpp>
13#include <iomanip>
14#include <ctime>
15#include <sstream>
16
17namespace ZCLibLog {
18 namespace formatters {
25 template <typename... Args>
26 static std::string do_format(FLogPack pack, const std::string& fmt, Args&&... args) {
27 std::string f_msg;
28 if (sizeof...(args) == 0) {
29 f_msg = fmt;
30 }
31 else {
32 boost::format f(fmt);
33 try {
34 apply(f, std::forward<Args>(args)...);
35 } catch (const std::exception& e) {
36 return e.what();
37 }
38 f_msg = f.str();
39 }
40
41 const auto tp = std::chrono::system_clock::time_point(std::chrono::milliseconds(pack.time));
42
43 const char* level_str;
44 switch (pack.level) {
45 case LogLevel::TRACE: level_str = "[TRACE]";
46 break;
47 case LogLevel::DEBUG: level_str = "[DEBUG]";
48 break;
49 case LogLevel::INFO: level_str = "[INFO]";
50 break;
51 case LogLevel::WARN: level_str = "[WARN]";
52 break;
53 case LogLevel::ERROR: level_str = "[ERROR]";
54 break;
55 case LogLevel::FATAL: level_str = "[FATAL]";
56 break;
57 default: level_str = "[OUT]";
58 break;
59 }
60
61 std::time_t tt = std::chrono::system_clock::to_time_t(tp);
62 std::tm tm{};
63
64 #if defined(_WIN32)
65 localtime_s(&tm, &tt);
66 #elif defined(__linux__) || defined(__APPLE__) || defined(__unix__)
67 localtime_r(&tt, &tm);
68 #else
69 tm = *std::localtime(&tt);
70 #endif
71
72 std::ostringstream t_oss;
73 t_oss << std::put_time(&tm, "%Y-%m-%d %H:%M:%S");
74
75 return (boost::format("%1% [%2%] %3% %4%")
76 % t_oss.str()
77 % *pack.name
78 % level_str
79 % f_msg
80 ).str();
81 }
82 private:
83 template <typename T>
84 static void apply(boost::format& f, T&& t) {
85 f % std::forward<T>(t);
86 }
87
88 template <typename T, typename... Args>
89 static void apply(boost::format& f, T&& t, Args&&... args) {
90 f % std::forward<T>(t);
91 apply(f, std::forward<Args>(args)...);
92 }
93 };
94 }
95}
96
97#endif //ZCLIBLOG_FORMATTERS_TP_BOOST_FORMAT_HPP
ZCLibLog的命名空间
Definition android_log.hpp:16
FLogPack FLogPack
简化写法
Definition logger_types.hpp:217
传统格式化API
Definition traditional.hpp:20
基于boost::format
Definition tp_boost-format.hpp:24
static std::string do_format(FLogPack pack, const std::string &fmt, Args &&... args)
Definition tp_boost-format.hpp:26