ZCLibLog
载入中...
搜索中...
未找到
tp_tinyformat.hpp
浏览该文件的文档.
1// Copyright 2026 CZF-H
2// Licensed under the Apache License, Version 2.0
3
4//
5// Created by wanjiangzhi on 2026/4/11.
6//
7
8#ifndef ZCLIBLOG_FORMATTERS_TP_TINYFORMAT_HPP
9#define ZCLIBLOG_FORMATTERS_TP_TINYFORMAT_HPP
10
11#include "basic_formatter.hpp"
13#include <tinyformat.h>
14#include <iomanip>
15#include <ctime>
16
17// NOLINTNEXTLINE
18namespace ZCLibLog {
19 namespace formatters {
25 namespace tinyformat {
32 template <typename... Args>
33 static std::string do_format(FLogPack pack, const char* fmt, const Args&... args) {
34 std::string f_msg;
35 try {
36 f_msg = ::tinyformat::format(fmt, args...);
37 } catch (const std::exception& e) {
38 return e.what();
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 ::tinyformat::format("%s [%s] %s %s", t_oss.str(), *pack.name, level_str, f_msg);
76 }
77 };
78 }
79
80
81 }
82}
83
84#endif //ZCLIBLOG_FORMATTERS_TP_TINYFORMAT_HPP
ZCLibLog的命名空间
Definition android_log.hpp:16
FLogPack FLogPack
简化写法
Definition logger_types.hpp:217
传统格式化API
Definition traditional.hpp:20
基于tinyformat::format
Definition tp_tinyformat.hpp:31
static std::string do_format(FLogPack pack, const char *fmt, const Args &... args)
Definition tp_tinyformat.hpp:33