@@ -6,6 +6,7 @@ import Crypto from './Crypto'
66import { Share } from '@capacitor/share'
77import { Filesystem , Directory , Encoding } from '@capacitor/filesystem'
88import { throttle } from 'throttle-debounce'
9+ import asyncThrottle from '@jcoreio/async-throttle'
910
1011export default class Logger {
1112 static log ( ) {
@@ -15,48 +16,71 @@ export default class Logger {
1516 DEBUG && console . log ( util . format . apply ( util , logMsg ) )
1617 this . messages . push ( util . format . apply ( util , logMsg ) )
1718 throttledTrimLogs ( )
19+ throttledIntermittentPersist ( )
1820 }
1921
2022 static trimLogs ( ) {
2123 this . messages = this . messages . slice ( - 1000 )
2224 }
2325
2426 static async persist ( ) {
25- const Storage = ( IS_BROWSER ) ? await import ( './browser/BrowserAccountStorage' ) : await import ( './native/NativeAccountStorage' )
27+ const Storage = IS_BROWSER
28+ ? await import ( './browser/BrowserAccountStorage' )
29+ : await import ( './native/NativeAccountStorage' )
2630 await Storage . default . changeEntry (
2731 'logs' ,
28- log => {
29- const messages = this . messages
32+ ( ) => {
33+ const messages = this . messages . slice ( - 1000 )
3034 this . messages = [ ]
3135 return messages // only save the last sync run
3236 } ,
3337 [ ]
3438 )
3539 }
3640
41+ static async intermittentPersist ( ) {
42+ const Storage = IS_BROWSER
43+ ? await import ( './browser/BrowserAccountStorage' )
44+ : await import ( './native/NativeAccountStorage' )
45+ await Storage . default . changeEntry (
46+ 'logs' ,
47+ ( ) => {
48+ return this . messages . slice ( - 1000 )
49+ } ,
50+ [ ]
51+ )
52+ }
53+
3754 static async getLogs ( ) {
38- const Storage = ( IS_BROWSER ) ? await import ( './browser/BrowserAccountStorage' ) : await import ( './native/NativeAccountStorage' )
55+ const Storage = IS_BROWSER
56+ ? await import ( './browser/BrowserAccountStorage' )
57+ : await import ( './native/NativeAccountStorage' )
3958 return Storage . default . getEntry ( 'logs' , [ ] )
4059 }
4160
4261 static async anonymizeLogs ( logs ) {
4362 const regex = / \[ ( .* ?) \] \( ( .* ?) \) | \[ ( .* ?) \] / g
44- const newLogs = await Parallel . map ( logs , async ( entry ) => {
45- return Logger . replaceAsync ( entry , regex , async ( match , p1 , p2 , p3 ) => {
46- if ( p1 && p2 ) {
47- const hash1 = await Crypto . sha256 ( p1 )
48- const hash2 = await Crypto . sha256 ( p2 )
49- return '[' + hash1 + ']' + '(' + hash2 + ')'
50- } else if ( p3 ) {
51- const hash = await Crypto . sha256 ( p3 )
52- return '[' + hash + ']'
53- }
54- } )
55- } , 1 )
63+ const newLogs = await Parallel . map (
64+ logs ,
65+ async ( entry ) => {
66+ return Logger . replaceAsync ( entry , regex , async ( match , p1 , p2 , p3 ) => {
67+ if ( p1 && p2 ) {
68+ const hash1 = await Crypto . sha256 ( p1 )
69+ const hash2 = await Crypto . sha256 ( p2 )
70+ return '[' + hash1 + ']' + '(' + hash2 + ')'
71+ } else if ( p3 ) {
72+ const hash = await Crypto . sha256 ( p3 )
73+ return '[' + hash + ']'
74+ }
75+ } )
76+ } ,
77+ 1
78+ )
5679 const regex2 = / u r l = h t t p s ? % 3 A % 2 F % 2 F .* $ | u r l = h t t p s ? % 3 A % 2 F % 2 F [ ^ ] * /
5780 const regex3 = / h t t p s ? : \/ \/ [ ^ / ] * \/ /
58- return newLogs
59- . map ( line => line . replace ( regex2 , '###url###' ) . replace ( regex3 , '###server###' ) )
81+ return newLogs . map ( ( line ) =>
82+ line . replace ( regex2 , '###url###' ) . replace ( regex3 , '###server###' )
83+ )
6084 }
6185
6286 static async replaceAsync ( str , regex , asyncFn ) {
@@ -82,16 +106,16 @@ export default class Logger {
82106 }
83107 let blob = new Blob ( [ logs . join ( '\n' ) ] , {
84108 type : 'text/plain' ,
85- endings : 'native'
109+ endings : 'native' ,
86110 } )
87111 this . download (
88112 'floccus-' +
89- packageJson . version +
90- '-' +
91- new Date ( ) . toISOString ( ) . slice ( 0 , 10 ) +
92- '-' +
93- ( anonymous ? 'redacted' : 'full' ) +
94- '.log' ,
113+ packageJson . version +
114+ '-' +
115+ new Date ( ) . toISOString ( ) . slice ( 0 , 10 ) +
116+ '-' +
117+ ( anonymous ? 'redacted' : 'full' ) +
118+ '.log' ,
95119 blob
96120 )
97121 }
@@ -112,12 +136,12 @@ export default class Logger {
112136 URL . revokeObjectURL ( objectUrl )
113137 document . body . removeChild ( element )
114138 } else {
115- const { uri : fileURI } = await Filesystem . writeFile ( {
139+ const { uri : fileURI } = await Filesystem . writeFile ( {
116140 path : 'Downloads/' + filename ,
117141 data : await blob . text ( ) ,
118142 encoding : Encoding . UTF8 ,
119143 directory : Directory . External ,
120- recursive : true
144+ recursive : true ,
121145 } )
122146 await Share . share ( {
123147 title : filename ,
@@ -128,5 +152,6 @@ export default class Logger {
128152}
129153
130154const throttledTrimLogs = throttle ( 20000 , ( ) => Logger . trimLogs ( ) )
155+ const throttledIntermittentPersist = asyncThrottle ( async ( ) => Logger . intermittentPersist ( ) , 3000 )
131156
132157Logger . messages = [ ]
0 commit comments