Skip to content

Commit 9d9a173

Browse files
committed
fix(Logger): Trim logs regularly to avoid memory leak
Signed-off-by: Marcel Klehr <[email protected]>
1 parent ead7dc3 commit 9d9a173

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/lib/Logger.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@ import packageJson from '../../package.json'
55
import Crypto from './Crypto'
66
import { Share } from '@capacitor/share'
77
import { Filesystem, Directory, Encoding } from '@capacitor/filesystem'
8+
import { throttle } from 'throttle-debounce'
89

910
export default class Logger {
1011
static log() {
1112
const logMsg = [new Date().toISOString(), ...arguments]
1213

1314
// log to console
1415
DEBUG && console.log(util.format.apply(util, logMsg))
15-
this.messages.push(util.format.apply(util, logMsg)) // TODO: Use a linked list here to get O(n)
16+
this.messages.push(util.format.apply(util, logMsg))
17+
throttledTrimLogs()
18+
}
19+
20+
static trimLogs() {
21+
this.messages = this.messages.slice(-1000)
1622
}
1723

1824
static async persist() {
@@ -120,4 +126,7 @@ export default class Logger {
120126
}
121127
}
122128
}
129+
130+
const throttledTrimLogs = throttle(20000, Logger.trimLogs)
131+
123132
Logger.messages = []

0 commit comments

Comments
 (0)