ZCLibLog
载入中...
搜索中...
未找到
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/5.
6//
7
8#ifndef ZCLIBLOG_FORMATTERS_FORMAT_HPP
9#define ZCLIBLOG_FORMATTERS_FORMAT_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, std::format_string<Args...>&& fmt, Args&&... args) {
26 std::string f_msg;
27 if (sizeof...(args) == 0) {
28 f_msg = fmt.get();
29 }
30 else {
31 f_msg = std::format(fmt, std::forward<Args>(args)...);
32 }
33
34 const auto tp = std::chrono::system_clock::time_point(std::chrono::milliseconds(pack.time));
35
36 const char* level_str;
37 switch (pack.level) {
38 case LogLevel::TRACE: level_str = "[TRACE]";
39 break;
40 case LogLevel::DEBUG: level_str = "[DEBUG]";
41 break;
42 case LogLevel::INFO: level_str = "[INFO]";
43 break;
44 case LogLevel::WARN: level_str = "[WARN]";
45 break;
46 case LogLevel::ERROR: level_str = "[ERROR]";
47 break;
48 case LogLevel::FATAL: level_str = "[FATAL]";
49 break;
50 default: level_str = "[OUT]";
51 break;
52 }
53
54 return std::format(
55 "{:%Y-%m-%d %H:%M:%S} [{}] {} {}",
56 std::chrono::time_point_cast<std::chrono::milliseconds>(tp),
57 *pack.name,
58 level_str,
59 f_msg
60 );
61 }
62 };
63}
64
65
66#endif // ZCLIBLOG_FORMATTERS_FORMAT_HPP
预定的一些formatter
Definition android_log.hpp:19
FLogPack FLogPack
简化写法
Definition logger_types.hpp:217
C++20 format API
Definition stdcxx20format.hpp:22
基于C++20的"std::format"的编译期格式化接口
Definition format.hpp:23
static std::string do_format(FLogPack pack, std::format_string< Args... > &&fmt, Args &&... args)
Definition format.hpp:25