ZCLibLog
载入中...
搜索中...
未找到
android_log.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_ANDROID_LOG_HPP
9#define ZCLIBLOG_FORMATTERS_ANDROID_LOG_HPP
10
11#include "basic_formatter.hpp"
13#include <ctime>
14#include <cstdio>
15#include <array>
16
17// NOLINTNEXTLINE
18namespace ZCLibLog {
19 namespace formatters {
27 template<typename... Args>
28 static std::string do_format(FLogPack pack, const char* fmt, Args&&... args) {
29 thread_local std::array<char, 4096> buffer;
30 int len{};
31
32 std::string f_msg;
33 if (sizeof...(args) == 0) {
34 f_msg = fmt;
35 }
36 else {
37 len = std::snprintf(buffer.data(), buffer.size(), fmt, std::forward<Args>(args)...);
38 if (len < 0) return {};
39 if (len >= static_cast<int>(buffer.size())) len = buffer.size() - 1;
40 f_msg = std::string(buffer.data(), len);
41 }
42 auto t = static_cast<std::time_t>(pack.time / 1000);
43 const auto ms = static_cast<short>(pack.time % 1000);
44 std::tm tm{};
45
46 #if defined(_WIN32)
47 localtime_s(&tm, &t);
48 #elif defined(__linux__) || defined(__APPLE__) || defined(__unix__)
49 localtime_r(&t, &tm);
50 #else
51 tm = *std::localtime(&t);
52 #endif
53
54 thread_local std::array<char, 64> time;
55 std::strftime(time.data(), time.size(), "%Y-%m-%d %H:%M:%S", &tm);
56 thread_local std::array<char, 80> ms_time;
57 std::snprintf(ms_time.data(), ms_time.size(), "%s.%03d", time.data(), ms);
58
59 len = std::snprintf(buffer.data(), buffer.size(),
60 "%s [%s] %s\n",
61 ms_time.data(), pack.name->c_str(), f_msg.c_str());
62 if (len < 0) return {};
63 if (len >= static_cast<int>(buffer.size())) len = buffer.size() - 1;
64 return {buffer.data(), buffer.data() + len};
65 }
66 };
67 }
68}
69
70#endif // ZCLIBLOG_FORMATTERS_ANDROID_LOG_HPP
ZCLibLog的命名空间
Definition android_log.hpp:16
FLogPack FLogPack
简化写法
Definition logger_types.hpp:217
传统格式化API
Definition traditional.hpp:20
为"__android_log_write"设计的基于C语言"snprintf"的格式化接口
Definition android_log.hpp:26
static std::string do_format(FLogPack pack, const char *fmt, Args &&... args)
Definition android_log.hpp:28