From e4623e2ad5b35ad3eb640f9eba13cadc1215c2d8 Mon Sep 17 00:00:00 2001 From: Jamie Kyle <113370520+jamiebuilds-signal@users.noreply.github.com> Date: Tue, 21 Mar 2023 14:23:27 -0700 Subject: [PATCH] Use circular buffer instead of array for logs --- ACKNOWLEDGMENTS.md | 24 ++++++++++++++++++++++++ package.json | 1 + ts/logging/main_process_logging.ts | 10 ++++------ yarn.lock | 5 +++++ 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/ACKNOWLEDGMENTS.md b/ACKNOWLEDGMENTS.md index adba61bcf40c..d8dfb927a753 100644 --- a/ACKNOWLEDGMENTS.md +++ b/ACKNOWLEDGMENTS.md @@ -387,6 +387,30 @@ Signal Desktop makes use of the following open source projects. License: MIT +## cirbuf + + MIT License + + Copyright (c) 2023 Jamie Kyle + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + ## classnames The MIT License (MIT) diff --git a/package.json b/package.json index c13538622a1e..954adb3a75cc 100644 --- a/package.json +++ b/package.json @@ -92,6 +92,7 @@ "blob-util": "2.0.2", "blueimp-load-image": "5.14.0", "blurhash": "1.1.3", + "cirbuf": "1.0.1", "classnames": "2.2.5", "config": "1.28.1", "copy-text-to-clipboard": "2.1.0", diff --git a/ts/logging/main_process_logging.ts b/ts/logging/main_process_logging.ts index e9966f799435..6126f295ee2a 100644 --- a/ts/logging/main_process_logging.ts +++ b/ts/logging/main_process_logging.ts @@ -22,6 +22,7 @@ import { filter, flatten, map, pick, sortBy } from 'lodash'; import readFirstLine from 'firstline'; import { read as readLastLines } from 'read-last-lines'; import rimraf from 'rimraf'; +import { CircularBuffer } from 'cirbuf'; import type { LoggerType } from '../types/Logging'; import * as Errors from '../types/errors'; @@ -44,7 +45,7 @@ declare global { } } -const MAX_LOG_LINES = 1000000; +const MAX_LOG_LINES = 10_000_000; let globalLogger: undefined | pino.Logger; let shouldRestart = false; @@ -287,7 +288,7 @@ export async function eliminateOldEntries( // Exported for testing only. export async function fetchLog(logFile: string): Promise> { - const results = new Array(); + const results = new CircularBuffer(MAX_LOG_LINES); const rawStream = createReadStream(logFile); const jsonStream = rawStream.pipe( @@ -311,12 +312,9 @@ export async function fetchLog(logFile: string): Promise> { } results.push(result); - if (results.length > MAX_LOG_LINES) { - results.shift(); - } } - return results; + return results.toArray(); } // Exported for testing only. diff --git a/yarn.lock b/yarn.lock index 8ed2ca0add77..3b8791895a1a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6606,6 +6606,11 @@ cipher-base@^1.0.0, cipher-base@^1.0.1, cipher-base@^1.0.3: inherits "^2.0.1" safe-buffer "^5.0.1" +cirbuf@1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/cirbuf/-/cirbuf-1.0.1.tgz#8613dcf424fb4be7c0abf1ce1ed3366971a444ff" + integrity sha512-uk/k9s7aJiiH+Xwq2dQRhsOZE66eoY4TLca3d7zSv6QUfr4xLCT6xxBm4O4ZRkfF136+E5aVmjTbRrGr76Wilw== + class-utils@^0.3.5: version "0.3.6" resolved "https://registry.yarnpkg.com/class-utils/-/class-utils-0.3.6.tgz#f93369ae8b9a7ce02fd41faad0ca83033190c463"