ZCLibLog
载入中...
搜索中...
未找到
vformat.hpp
浏览该文件的文档.
1// Copyright 2026 CZF-H
2// Licensed under the Apache License, Version 2.0
3
4//
5// Created by TingIAAI on 2026/4/4.
6//
7
8#ifndef ZCLIBLOG_FORMATTERS_VFORMAT_HPP
9#define ZCLIBLOG_FORMATTERS_VFORMAT_HPP
10
11#include "basic_formatter.hpp"
13#include <format>
14#include <chrono>
15
16// NOLINTNEXTLINE
17namespace ZCLibLog::formatters {
24 template <typename... Args>
25 static std::string do_format(FLogPack pack, const std::string_view fmt, Args&&... args) {
26 std::string f_msg;
27 if (sizeof...(args) == 0) {
28 f_msg = fmt;
29 }
30 else {
31 try {
32 f_msg = std::vformat(fmt, std::make_format_args(std::forward<Args>(args)...));
33 } catch (const std::format_error& e) {
34 return e.what();
35 }
36 }
37
38 const auto tp = std::chrono::system_clock::time_point(std::chrono::milliseconds(pack.time));
39
40 const char* level_str;
41 switch (pack.level) {
42 case LogLevel::TRACE: level_str = "[TRACE]";
43 break;
44 case LogLevel::DEBUG: level_str = "[DEBUG]";
45 break;
46 case LogLevel::INFO: level_str = "[INFO]";
47 break;
48 case LogLevel::WARN: level_str = "[WARN]";
49 break;
50 case LogLevel::ERROR: level_str = "[ERROR]";
51 break;
52 case LogLevel::FATAL: level_str = "[FATAL]";
53 break;
54 default: level_str = "[OUT]";
55 break;
56 }
57
58 return std::format(
59 "{:%Y-%m-%d %H:%M:%S} [{}] {} {}",
60 std::chrono::time_point_cast<std::chrono::milliseconds>(tp),
61 *pack.name,
62 level_str,
63 f_msg
64 );
65 }
66 };
67}
68
69
70#endif // ZCLIBLOG_FORMATTERS_VFORMAT_HPP
预定的一些formatter
Definition android_log.hpp:19
FLogPack FLogPack
简化写法
Definition logger_types.hpp:217
传统格式化API
Definition traditional.hpp:20
基于C++20的"std::vformat"+"std::format"的动态格式化接口
Definition vformat.hpp:23
static std::string do_format(FLogPack pack, const std::string_view fmt, Args &&... args)
Definition vformat.hpp:25