107
body = stringifyAhpLogEntryTruncated(entry, MAX_LOGGED_STRING_LENGTH);
108
}
110
>
this._pending.push(VSBuffer.fromString(line));
111
>
this._scheduleDrain();
112
>
}
113
114
async flush(): Promise<void> {
115
>
// Pending entries always have a drain scheduled (see _scheduleDrain), so
ahpJsonlLogger.ts
116
>
// awaiting the queue is sufficient to flush everything submitted before
117
>
// this call.
118
>
await this._queue;
119
>
}
120
121
private _scheduleDrain(): void {
123
>
return;
124
>
}
125
>
this._drainScheduled = true;
126
>
this._queue = this._queue.then(() => this._drainPending()).catch(error => {
127
this._logService.error('[AHPLog] Failed to write transport log', error);
129
>
}
130
131
private async _drainPending(): Promise<void> {
132
>
// Clear the scheduled flag before snapshotting _pending so that any log()
ahpJsonlLogger.ts
133
>
// calls happening during the awaits below will schedule a fresh drain.
134
>
this._drainScheduled = false;
135
>
if (this._pending.length === 0) {
136
return;
137
}
139
>
this._pending = [];
140
>
141
>
// Create folder once and memoize to avoid repeated filesystem calls
142
>
if (!this._folderCreated) {
143
>
this._folderCreated = this._fileService.createFolder(this._directory);
144
>
}
145
>
await this._folderCreated;
146
>
if (this._currentSize === 0) {
147
>
this._currentSize = await this._getFileSize(this._currentFile);
148
>
}
149
>
150
>
// Coalesce buffers into chunks, respecting both file-rotation size and the
151
>
// per-write batch cap. Rotation is checked per-entry to preserve the
152
>
// invariant that we don't exceed maxFileSizeBytes once a file has data.
153
>
let chunk: VSBuffer[] = [];
154
>
let chunkSize = 0;
155
>
const flushChunk = async () => {
156
>
if (chunk.length === 0) {
157
return;
158
}
159
>
const combined = chunk.length === 1 ? chunk[0] : VSBuffer.concat(chunk, chunkSize);
ahpJsonlLogger.ts
160
>
await this._fileService.writeFile(this._currentFile, combined, { append: true });
161
>
this._currentSize += combined.byteLength;
162
>
chunk = [];
163
>
chunkSize = 0;
164
>
};
165
>
166
>
for (const buffer of buffers) {
167
>
const totalInFile = this._currentSize + chunkSize;
168
>
if (totalInFile > 0 && totalInFile + buffer.byteLength > this._maxFileSizeBytes) {
169
await flushChunk();
170
await this._rotate();
171
>
} else if (chunkSize > 0 && chunkSize + buffer.byteLength > MAX_BATCH_BYTES) {
ahpJsonlLogger.ts
172
// Same file but the batch is getting too large; flush early to
173
// avoid creating an oversized concatenated VSBuffer.
174
await flushChunk();
175
}
177
>
chunkSize += buffer.byteLength;
178
>
}
179
>
await flushChunk();
180
>
}
181
182
private async _rotate(): Promise<void> {