ZCLibLog
载入中...
搜索中...
未找到
tp_fmtlib.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_FMTLIB_HPP
9#define ZCLIBLOG_FORMATTERS_TP_FMTLIB_HPP
10
11#include "basic_formatter.hpp"
14#include <fmt/format.h>
15#include <fmt/chrono.h>
16
17// NOLINTNEXTLINE
18namespace ZCLibLog {
19 namespace formatters {
25 namespace fmtlib {
32 template <typename... Args>
33 static std::string do_format(FLogPack pack, const fmt::format_string<Args...>& fmt, Args&&... args) {
34 std::string f_msg;
35 if (sizeof...(args) == 0) {
36 f_msg = fmt.get().data();
37 } else {
38 try {
39 f_msg = fmt::format(fmt, std::forward<Args>(args)...);
40 } catch (fmt::format_error& e) {
41 return e.what();
42 }
43 }
44 const auto tp = std::chrono::system_clock::time_point(std::chrono::milliseconds(pack.time));
45
46 const char* level_str;
47 switch (pack.level) {
48 case LogLevel::TRACE: level_str = "[TRACE]";
49 break;
50 case LogLevel::DEBUG: level_str = "[DEBUG]";
51 break;
52 case LogLevel::INFO: level_str = "[INFO]";
53 break;
54 case LogLevel::WARN: level_str = "[WARN]";
55 break;
56 case LogLevel::ERROR: level_str = "[ERROR]";
57 break;
58 case LogLevel::FATAL: level_str = "[FATAL]";
59 break;
60 default: level_str = "[OUT]";
61 break;
62 }
63
64 return fmt::format(
65 "{:%Y-%m-%d %H:%M:%S} [{}] {} {}",
66 std::chrono::time_point_cast<std::chrono::milliseconds>(tp),
67 *pack.name,
68 level_str,
69 f_msg
70 );
71 }
72 };
79 template <typename... Args>
80 static std::string do_format(FLogPack pack, const fmt::string_view fmt, Args&&... args) {
81 std::string f_msg;
82 if (sizeof...(args) == 0) {
83 f_msg = fmt.data();
84 } else {
85 try {
86 f_msg = fmt::vformat(fmt, fmt::make_format_args(stdargs...));
87 } catch (fmt::format_error& e) {
88 return e.what();
89 }
90 }
91 const auto tp = std::chrono::system_clock::time_point(std::chrono::milliseconds(pack.time));
92
93 const char* level_str;
94 switch (pack.level) {
95 case LogLevel::TRACE: level_str = "[TRACE]";
96 break;
97 case LogLevel::DEBUG: level_str = "[DEBUG]";
98 break;
99 case LogLevel::INFO: level_str = "[INFO]";
100 break;
101 case LogLevel::WARN: level_str = "[WARN]";
102 break;
103 case LogLevel::ERROR: level_str = "[ERROR]";
104 break;
105 case LogLevel::FATAL: level_str = "[FATAL]";
106 break;
107 default: level_str = "[OUT]";
108 break;
109 }
110
111 return fmt::format(
112 "{:%Y-%m-%d %H:%M:%S} [{}] {} {}",
113 std::chrono::time_point_cast<std::chrono::milliseconds>(tp),
114 *pack.name,
115 level_str,
116 f_msg
117 );
118 }
119 };
120 }
121 }
122}
123
124#endif //ZCLIBLOG_FORMATTERS_TP_FMTLIB_HPP
ZCLibLog的命名空间
Definition android_log.hpp:16
FLogPack FLogPack
简化写法
Definition logger_types.hpp:217
fmtlib API
Definition tp_fmtlib.hpp:22
传统格式化API
Definition traditional.hpp:20
基于第三方的fmtlib
Definition tp_fmtlib.hpp:31
static std::string do_format(FLogPack pack, const fmt::format_string< Args... > &fmt, Args &&... args)
Definition tp_fmtlib.hpp:33
基于第三方的fmtlib
Definition tp_fmtlib.hpp:78
static std::string do_format(FLogPack pack, const fmt::string_view fmt, Args &&... args)
Definition tp_fmtlib.hpp:80