Skip to content

Commit 12ce175

Browse files
committed
Revert "fix: Revert logging to IndexedDB"
This reverts commit ccc6859.
1 parent 9d1ad09 commit 12ce175

File tree

3 files changed

+59
-34
lines changed

3 files changed

+59
-34
lines changed

src/lib/Account.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,6 @@ export default class Account {
199199
Logger.log(
200200
'Resource is locked, trying again soon'
201201
)
202-
await Logger.persist()
203202
return
204203
}
205204
} else {
@@ -348,7 +347,6 @@ export default class Account {
348347
await this.init()
349348
}
350349
}
351-
await Logger.persist()
352350
}
353351

354352
static async stringifyError(er:any):Promise<string> {

src/lib/IndexedDB.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,38 @@ db.version(1).stores({
1717
logs: '++id, dateTime, message'
1818
})
1919

20-
db.delete().then(() => console.log('Deleted floccus database'))
21-
2220
export { db }
2321
export { LogMessage }
2422

23+
const MAX_STORAGE_SIZE = 50 * 1024 * 1024 // 50MB
24+
2525
export async function freeStorageIfNecessary() {
26-
// noop
26+
if (navigator.storage && navigator.storage.estimate) {
27+
let {usage, quota} = await navigator.storage.estimate()
28+
if (usage / quota > 0.9 || usage > MAX_STORAGE_SIZE) {
29+
const oneWeekAgo = Date.now() - 60 * 60 * 1000 * 24 * 7
30+
31+
await db.logs
32+
.where('dateTime').below(oneWeekAgo)
33+
.delete()
34+
}
35+
36+
({usage, quota} = await navigator.storage.estimate())
37+
if (usage / quota > 0.6 || usage > MAX_STORAGE_SIZE) {
38+
const oneDayAgo = Date.now() - 60 * 60 * 1000 * 24
39+
40+
await db.logs
41+
.where('dateTime').below(oneDayAgo)
42+
.delete()
43+
}
44+
45+
({usage, quota} = await navigator.storage.estimate())
46+
if (usage / quota > 0.6 || usage > MAX_STORAGE_SIZE) {
47+
const lastHour = Date.now() - 60 * 60 * 1000
48+
49+
await db.logs
50+
.where('dateTime').below(lastHour)
51+
.delete()
52+
}
53+
}
2754
}

src/lib/Logger.js

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,31 @@ import Crypto from './Crypto'
66
import { Share } from '@capacitor/share'
77
import { Filesystem, Directory, Encoding } from '@capacitor/filesystem'
88
import { Capacitor } from '@capacitor/core'
9+
import { db } from './IndexedDB'
910

1011
export default class Logger {
1112
static log() {
12-
const logMsg = [new Date().toISOString(), ...arguments]
13+
const dateTime = Date.now()
14+
const logMsg = [...arguments]
15+
const message = util.format.apply(util, logMsg)
1316

1417
// log to console
1518
DEBUG && console.log(util.format.apply(util, logMsg))
16-
this.messages.push(util.format.apply(util, logMsg)) // TODO: Use a linked list here to get O(n)
17-
}
18-
19-
static async persist() {
20-
const Storage = (Capacitor.getPlatform() === 'web') ? await import('./browser/BrowserAccountStorage') : await import('./native/NativeAccountStorage')
21-
await Storage.default.changeEntry(
22-
'logs',
23-
log => {
24-
const messages = this.messages
25-
this.messages = []
26-
return messages // only save the last sync run
27-
},
28-
[]
29-
)
19+
db.logs.add({dateTime, message})
20+
.catch(e => {
21+
console.error('Failed to log to IndexedDB: ', e)
22+
console.error(e)
23+
})
3024
}
3125

3226
static async getLogs() {
33-
const Storage = (Capacitor.getPlatform() === 'web') ? await import('./browser/BrowserAccountStorage') : await import('./native/NativeAccountStorage')
34-
return Storage.default.getEntry('logs', [])
27+
return db.logs.orderBy('dateTime').toArray()
3528
}
3629

3730
static async anonymizeLogs(logs) {
3831
const regex = /\[(.*?)\]\((.*?)\)|\[(.*?)\]/g
39-
const newLogs = await Parallel.map(logs, async(entry) => {
40-
return Logger.replaceAsync(entry, regex, async(match, p1, p2, p3) => {
32+
await Parallel.map(logs, async(logMessage) => {
33+
logMessage.message = await Logger.replaceAsync(logMessage.message, regex, async(match, p1, p2, p3) => {
4134
if (p1 && p2) {
4235
const hash1 = await Crypto.sha256(p1)
4336
const hash2 = await Crypto.sha256(p2)
@@ -50,8 +43,11 @@ export default class Logger {
5043
}, 1)
5144
const regex2 = /url=https?%3A%2F%2F.*$|url=https?%3A%2F%2F[^ ]*/
5245
const regex3 = /https?:\/\/[^ /]*\//
53-
return newLogs
54-
.map(line => line.replace(regex2, '###url###').replace(regex3, '###server###'))
46+
logs
47+
.forEach(logMessage => {
48+
logMessage.message = logMessage.message.replace(regex2, '###url###').replace(regex3, '###server###')
49+
})
50+
return logs
5551
}
5652

5753
static async replaceAsync(str, regex, asyncFn) {
@@ -75,18 +71,23 @@ export default class Logger {
7571
if (anonymous) {
7672
logs = await Logger.anonymizeLogs(logs)
7773
}
78-
let blob = new Blob([logs.join('\n')], {
74+
logs = logs
75+
.map(logMessage => {
76+
return new Date(logMessage.dateTime).toISOString() + ' ' + logMessage.message
77+
})
78+
.join('\n')
79+
let blob = new Blob([logs], {
7980
type: 'text/plain',
8081
endings: 'native'
8182
})
8283
this.download(
8384
'floccus-' +
84-
packageJson.version +
85-
'-' +
86-
new Date().toISOString().slice(0, 10) +
87-
'-' +
88-
(anonymous ? 'redacted' : 'full') +
89-
'.log',
85+
packageJson.version +
86+
'-' +
87+
new Date().toISOString().slice(0, 10) +
88+
'-' +
89+
(anonymous ? 'redacted' : 'full') +
90+
'.log',
9091
blob
9192
)
9293
}
@@ -121,4 +122,3 @@ export default class Logger {
121122
}
122123
}
123124
}
124-
Logger.messages = []

0 commit comments

Comments
 (0)