Repository navigation
Expand file tree
/
Copy pathLogChunks.h
More file actions
144 lines (122 loc) · 5 KB
/
Copy pathLogChunks.h
File metadata and controls
144 lines (122 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#ifndef LogChunks_h
#define LogChunks_h
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <string>
#include <string_view>
#include <vector>
namespace tns {
// os_log replaces whatever exceeds its buffer with "<…>". The buffer also holds
// a few bytes of bookkeeping per argument, which left 996 bytes of text with two
// arguments on iPadOS 26.7, so stay well below that.
constexpr size_t kMaxLogChunkBytes = 900;
// How a chunk connects to the one after it.
enum class LogChunkBreak { kNone, kJoin, kLine };
// Calls emit with consecutive pieces of message, each at most maxBytes long and
// never ending inside a UTF-8 sequence. A piece ends at the last line break that
// fits, so that lines stay intact whenever they can; that line break is dropped,
// since every piece is logged as a line of its own.
template <typename Emit>
void ForEachLogChunk(std::string_view message, size_t maxBytes, Emit&& emit) {
while (message.size() > maxBytes) {
size_t end = message.rfind('\n', maxBytes);
size_t next = end + 1;
LogChunkBreak chunkBreak = LogChunkBreak::kLine;
if (end == std::string_view::npos || end == 0) {
end = maxBytes;
while (end > 0 && (static_cast<unsigned char>(message[end]) & 0xC0) == 0x80) {
end--;
}
// Not UTF-8 at all: any cut is as good as another.
if (end == 0) {
end = maxBytes;
}
next = end;
chunkBreak = LogChunkBreak::kJoin;
}
emit(message.substr(0, end), chunkBreak);
message.remove_prefix(next);
}
emit(message, LogChunkBreak::kNone);
}
// The newest value of the logFormat key in the app's package.json that the
// runtime understands. The CLI writes the newest one it understands.
//
// Format 1 logs each piece of a split message as "[ns <id> <index>/<count>] "
// followed by the piece. <id> is unique per message in the process (hex) and
// <index> counts from 1. The pieces are consecutive slices of the message and
// only the first carries the message's prefix, so they join back into the
// message as they are. Messages that fit in one entry are logged without a
// header.
constexpr int kNewestLogFormat = 1;
// Room for the longest header: ids up to 8 hex digits and counts up to 7
// decimal ones (messages of several GB).
constexpr size_t kMaxLogChunkHeaderBytes = 40;
inline std::string_view FormatLogChunkHeader(char (&buffer)[kMaxLogChunkHeaderBytes], uint32_t id,
size_t index, size_t count) {
int length = snprintf(buffer, sizeof(buffer), "[ns %x %zu/%zu] ", id, index, count);
if (length < 0) {
return {};
}
return std::string_view(buffer, static_cast<size_t>(length) < sizeof(buffer)
? static_cast<size_t>(length)
: sizeof(buffer) - 1);
}
inline bool IsLogWhitespace(char c) {
return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\v' || c == '\f';
}
// Calls emit with consecutive slices of text, each at most maxBytes long and
// never ending inside a UTF-8 sequence. The unified log strips whitespace from
// the end of every entry, so a slice never ends in whitespace: it is left for
// the start of the next slice, behind that slice's header. Only a run of
// whitespace longer than maxBytes still loses what it ends with. A slice ends
// before the last line break that fits, so that lines stay intact whenever they
// can.
template <typename Emit>
void ForEachLogSlice(std::string_view text, size_t maxBytes, Emit&& emit) {
auto backOverWhitespace = [&](size_t end) {
while (end > 0 && IsLogWhitespace(text[end - 1])) {
end--;
}
return end;
};
while (text.size() > maxBytes) {
size_t lineBreak = text.rfind('\n', maxBytes);
size_t end = lineBreak == std::string_view::npos ? 0 : backOverWhitespace(lineBreak);
if (end == 0) {
end = maxBytes;
while (end > 0 && (static_cast<unsigned char>(text[end]) & 0xC0) == 0x80) {
end--;
}
// Not UTF-8 at all: any cut is as good as another.
if (end == 0) {
end = maxBytes;
}
if (size_t beforeWhitespace = backOverWhitespace(end)) {
end = beforeWhitespace;
}
}
emit(text.substr(0, end));
text.remove_prefix(end);
}
emit(text);
}
// Calls emit with the header and text of each piece of prefix + message, split
// in format 1 as the message with the given id.
template <typename Emit>
void ForEachFramedLogChunk(std::string_view prefix, std::string_view message, uint32_t id,
Emit&& emit) {
std::string text;
text.reserve(prefix.size() + message.size());
text.append(prefix).append(message);
std::vector<std::string_view> pieces;
ForEachLogSlice(text, kMaxLogChunkBytes - kMaxLogChunkHeaderBytes,
[&](std::string_view piece) { pieces.push_back(piece); });
for (size_t i = 0; i < pieces.size(); i++) {
char buffer[kMaxLogChunkHeaderBytes];
emit(FormatLogChunkHeader(buffer, id, i + 1, pieces.size()), pieces[i]);
}
}
} // namespace tns
#endif /* LogChunks_h */