Skip to content

Commit 5ee8a6c

Browse files
committed
Reapply "fix: Revert logging to IndexedDB"
This reverts commit 12ce175.
1 parent 3af8252 commit 5ee8a6c

File tree

3 files changed

+34
-59
lines changed

3 files changed

+34
-59
lines changed

src/lib/Account.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ export default class Account {
200200
Logger.log(
201201
'Resource is locked, trying again soon'
202202
)
203+
await Logger.persist()
203204
return
204205
}
205206
} else {
@@ -348,6 +349,7 @@ export default class Account {
348349
await this.init()
349350
}
350351
}
352+
await Logger.persist()
351353
}
352354

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

src/lib/IndexedDB.ts

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

20+
db.delete().then(() => console.log('Deleted floccus database'))
21+
2022
export { db }
2123
export { LogMessage }
2224

23-
const MAX_STORAGE_SIZE = 50 * 1024 * 1024 // 50MB
24-
2525
export async function freeStorageIfNecessary() {
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-
}
26+
// noop
5427
}

src/lib/Logger.js

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,38 @@ 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'
109

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

1714
// log to console
1815
DEBUG && console.log(util.format.apply(util, logMsg))
19-
db.logs.add({dateTime, message})
20-
.catch(e => {
21-
console.error('Failed to log to IndexedDB: ', e)
22-
console.error(e)
23-
})
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+
)
2430
}
2531

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

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

5357
static async replaceAsync(str, regex, asyncFn) {
@@ -71,23 +75,18 @@ export default class Logger {
7175
if (anonymous) {
7276
logs = await Logger.anonymizeLogs(logs)
7377
}
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], {
78+
let blob = new Blob([logs.join('\n')], {
8079
type: 'text/plain',
8180
endings: 'native'
8281
})
8382
this.download(
8483
'floccus-' +
85-
packageJson.version +
86-
'-' +
87-
new Date().toISOString().slice(0, 10) +
88-
'-' +
89-
(anonymous ? 'redacted' : 'full') +
90-
'.log',
84+
packageJson.version +
85+
'-' +
86+
new Date().toISOString().slice(0, 10) +
87+
'-' +
88+
(anonymous ? 'redacted' : 'full') +
89+
'.log',
9190
blob
9291
)
9392
}
@@ -122,3 +121,4 @@ export default class Logger {
122121
}
123122
}
124123
}
124+
Logger.messages = []

0 commit comments

Comments
 (0)