33// BSD-style license that can be found in the LICENSE file.
44
55import 'dart:convert' ;
6+ import 'dart:io' as io;
7+ import 'dart:math' ;
68
79import 'package:clock/clock.dart' ;
810import 'package:file/file.dart' ;
11+ import 'package:meta/meta.dart' ;
912
1013import 'constants.dart' ;
1114import 'event.dart' ;
@@ -154,18 +157,23 @@ class LogFileStats {
154157/// This class is responsible for writing to a log
155158/// file that has been initialized by the [createLogFile] .
156159///
157- /// It will be treated as an append only log and will be limited
158- /// to have has many data records as specified by [kLogFileLength ] .
160+ /// It will be treated as an append only log, trimmed to the newest
161+ /// [kLogFileLength] records when it grows past [kLogFileTrimSize ] .
159162class LogHandler {
160163 final File logFile;
161164
165+ final int _trimSize;
166+
162167 /// Contains instances of [Event.analyticsException] that were encountered
163168 /// during a workflow and will be sent to GA4 for collection.
164169 final Set <Event > errorSet = {};
165170
166171 /// A log handler constructor that will delegate saving
167172 /// logs and retrieving stats from the persisted log.
168- LogHandler ({required this .logFile});
173+ LogHandler ({
174+ required this .logFile,
175+ @visibleForTesting int trimSize = kLogFileTrimSize,
176+ }) : _trimSize = trimSize;
169177
170178 /// Get stats from the persisted log file.
171179 ///
@@ -176,8 +184,9 @@ class LogHandler {
176184 // Parse each line of the log file through [LogItem],
177185 // some returned records may be null if malformed, they will be
178186 // removed later through `whereType<LogItem>`
179- final records = logFile
180- .readAsLinesSync ()
187+ final lines = logFile.readAsLinesSync ();
188+ final records = lines
189+ .skip (max (0 , lines.length - kLogFileLength))
181190 .map ((String e) {
182191 try {
183192 return LogItem .fromRecord (jsonDecode (e) as Map <String , Object ?>);
@@ -273,31 +282,22 @@ class LogHandler {
273282
274283 /// Saves the data passed in as a single line in the log file.
275284 ///
276- /// This will keep the max number of records limited to equal to
277- /// or less than [kLogFileLength] records.
285+ /// This only appends to the file, unless the file has grown past the trim
286+ /// size, see [kLogFileTrimSize] ; then it is first trimmed to the newest
287+ /// [kLogFileLength] records.
278288 void save ({required Map <String , Object ?> data}) {
279289 try {
280- List <String > records;
281290 final stat = logFile.statSync ();
282291 if (stat.size > kMaxLogFileSize) {
283292 logFile.deleteSync ();
284293 logFile.createSync ();
285- records = [];
286- } else {
287- records = logFile.readAsLinesSync ();
288- }
289- final content = '${jsonEncode (data )}\n ' ;
290-
291- // When the record count is less than the max, add as normal;
292- // else drop the oldest records until equal to max
293- if (records.length < kLogFileLength) {
294- logFile.writeAsStringSync (content, mode: FileMode .writeOnlyAppend);
295- } else {
296- records.add (content);
297- records = records.skip (records.length - kLogFileLength).toList ();
298-
299- logFile.writeAsStringSync (records.join ('\n ' ));
294+ } else if (stat.size > _trimSize) {
295+ _trim ();
300296 }
297+ logFile.writeAsStringSync (
298+ '${jsonEncode (data )}\n ' ,
299+ mode: FileMode .writeOnlyAppend,
300+ );
301301 } on Object {
302302 // Logging isn't important enough to warrant raising an
303303 // exception or error that will surprise consumers of this package.
@@ -308,6 +308,47 @@ class LogHandler {
308308 logFile.createSync ();
309309 }
310310 }
311+
312+ /// Rewrites the log file keeping only its newest records.
313+ ///
314+ /// Keeps at most [kLogFileLength] records and at most about half the trim
315+ /// size, so that trims stay rare however large the records are.
316+ ///
317+ /// The new content is written to a temporary file which is then renamed
318+ /// over the log file, so other processes see the old or the new file and
319+ /// never a partial one. A record that another process appends during the
320+ /// trim can be lost; the log is only a local record, so that is accepted.
321+ void _trim () {
322+ File ? tempFile;
323+ try {
324+ final lines = logFile.readAsLinesSync ();
325+ final maxBytes = _trimSize ~ / 2 ;
326+ var start = lines.length;
327+ var bytes = 0 ;
328+ while (start > 0 && lines.length - start < kLogFileLength) {
329+ // UTF-16 length approximates UTF-8 bytes; log records are almost
330+ // entirely ASCII.
331+ bytes += lines[start - 1 ].length + 1 ;
332+ if (bytes > maxBytes) break ;
333+ start-- ;
334+ }
335+ final kept = lines.sublist (start);
336+ // The random part keeps trims in different isolates of one process
337+ // apart.
338+ final suffix = '${io .pid }.${Random ().nextInt (1 << 32 )}' ;
339+ tempFile = logFile.fileSystem.file ('${logFile .path }.$suffix .tmp' );
340+ tempFile.writeAsStringSync (kept.isEmpty ? '' : '${kept .join ('\n ' )}\n ' );
341+ tempFile.renameSync (logFile.path);
342+ } on Object {
343+ // Trimming is best effort; the next save tries again. If it keeps
344+ // failing, the file grows until [kMaxLogFileSize] and is deleted.
345+ try {
346+ if (tempFile != null && tempFile.existsSync ()) tempFile.deleteSync ();
347+ } on Object {
348+ // Nothing more to do.
349+ }
350+ }
351+ }
311352}
312353
313354/// Data class for each record persisted on the client's machine.
0 commit comments