ZCLibLog
载入中...
搜索中...
未找到
tp_absl_strformat.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_ABSL_STRFORMAT_HPP
9#define ZCLIBLOG_FORMATTERS_TP_ABSL_STRFORMAT_HPP
10
11#include "basic_formatter.hpp"
13#include <absl/strings/str_format.h>
14#include <iomanip>
15#include <ctime>
16
17// NOLINTNEXTLINE
18namespace ZCLibLog {
19 namespace formatters {
26 template <typename... Args>
27 static std::string do_format(FLogPack pack, const absl::FormatSpec<Args...>& fmt, const Args&... args) {
28 std::string f_msg;
29 try {
30 f_msg = absl::StrFormat(fmt, args...);
31 } catch (const std::exception& e) {
32 return e.what();
33 }
34
35 const auto tp = std::chrono::system_clock::time_point(std::chrono::milliseconds(pack.time));
36
37 const char* level_str;
38 switch (pack.level) {
39 case LogLevel::TRACE: level_str = "[TRACE]";
40 break;
41 case LogLevel::DEBUG: level_str = "[DEBUG]";
42 break;
43 case LogLevel::INFO: level_str = "[INFO]";
44 break;
45 case LogLevel::WARN: level_str = "[WARN]";
46 break;
47 case LogLevel::ERROR: level_str = "[ERROR]";
48 break;
49 case LogLevel::FATAL: level_str = "[FATAL]";
50 break;
51 default: level_str = "[OUT]";
52 break;
53 }
54
55 std::time_t tt = std::chrono::system_clock::to_time_t(tp);
56 std::tm tm{};
57
58 #if defined(_WIN32)
59 localtime_s(&tm, &tt);
60 #elif defined(__linux__) || defined(__APPLE__) || defined(__unix__)
61 localtime_r(&tt, &tm);
62 #else
63 tm = *std::localtime(&tt);
64 #endif
65
66 std::ostringstream t_oss;
67 t_oss << std::put_time(&tm, "%Y-%m-%d %H:%M:%S");
68
69 return absl::StrFormat("%s [%s] %s %s", t_oss.str(), *pack.name, level_str, f_msg);
70 }
71 };
72 }
73}
74
75#endif //ZCLIBLOG_FORMATTERS_TP_ABSL_STRFORMAT_HPP
ZCLibLog的命名空间
Definition android_log.hpp:16
FLogPack FLogPack
简化写法
Definition logger_types.hpp:217
absl format API
Definition tp_absl_strformat.hpp:22
基于absl::StrFormat
Definition tp_absl_strformat.hpp:25
static std::string do_format(FLogPack pack, const absl::FormatSpec< Args... > &fmt, const Args &... args)
Definition tp_absl_strformat.hpp:27