2019-09-03 21:30:18 +02:00
import Debug from '../conf/Debug' ;
2019-09-03 00:48:18 +02:00
import currentBrowser from '../conf/BrowserDetect' ;
2019-07-16 20:59:12 +02:00
class Logger {
constructor ( conf ) {
2019-09-03 00:48:18 +02:00
this . initLogger ( ) ;
if ( conf ) {
this . conf = conf ;
}
2019-07-16 20:59:12 +02:00
this . history = [ ] ;
this . startTime = performance . now ( ) ;
2019-07-18 21:25:58 +02:00
this . temp _disable = false ;
2020-01-06 21:37:40 +01:00
this . stopTime = confTimeout ? performance . now ( ) + ( conf . timeout * 1000 ) : undefined ;
2019-07-16 20:59:12 +02:00
}
2019-09-03 00:48:18 +02:00
initLogger ( ) {
const ths = this ;
const br = currentBrowser . firefox ? browser : chrome ;
br . storage . onChanged . addListener ( ( changes , area ) => {
if ( Debug . debug && Debug . debugStorage ) {
console . log ( "[Logger::<storage/on change>] Settings have been changed outside of here. Updating active settings. Changes:" , changes , "storage area:" , area ) ;
if ( changes [ 'uwLogger' ] && changes [ 'uwLogger' ] . newValue ) {
console . log ( "[Logger::<storage/on change>] new settings object:" , JSON . parse ( changes . uwLogger . newValue ) ) ;
}
}
if ( changes [ 'uwLogger' ] && changes [ 'uwLogger' ] . newValue ) {
ths . conf = JSON . parse ( changes . uwLogger . newValue ) ;
}
} ) ;
}
async init ( ) {
2019-09-17 22:18:02 +02:00
if ( ! this . conf ) {
this . conf = await this . getSaved ( ) ;
}
2019-09-03 00:48:18 +02:00
}
2019-07-16 20:59:12 +02:00
clear ( ) {
this . log = [ ] ;
this . startTime = performance . now ( ) ;
2020-01-06 21:37:40 +01:00
this . stopTime = this . conf . timeout ? performance . now ( ) + ( this . conf . timeout * 1000 ) : undefined ;
2019-07-16 20:59:12 +02:00
}
setConf ( conf ) {
2019-09-03 00:48:18 +02:00
this . conf = conf ; // effective immediately
// also persist settings:
if ( currentBrowser . firefox || currentBrowser . edge ) {
return browser . storage . local . set ( { 'uwLogger' : JSON . stringify ( this . conf ) } ) ;
} else if ( currentBrowser . chrome ) {
return chrome . storage . local . set ( { 'uwLogger' : JSON . stringify ( this . logger ) } ) ;
}
2019-07-16 20:59:12 +02:00
}
2019-09-03 00:48:18 +02:00
async getSaved ( ) {
let ret ;
if ( currentBrowser . firefox ) {
ret = await browser . storage . local . get ( 'uwLogger' ) ;
} else if ( currentBrowser . chrome ) {
ret = await new Promise ( ( resolve , reject ) => {
chrome . storage . local . get ( 'uwLogger' , ( res ) => resolve ( res ) ) ;
} ) ;
} else if ( currentBrowser . edge ) {
ret = await new Promise ( ( resolve , reject ) => {
browser . storage . local . get ( 'uwLogger' , ( res ) => resolve ( res ) ) ;
} ) ;
}
if ( Debug . debug && Debug . debugStorage ) {
try {
console . log ( "[Logger::getSaved] Got settings:" , JSON . parse ( ret . uwLogger ) ) ;
} catch ( e ) {
console . log ( "[Logger::getSaved] No settings." )
}
}
try {
return JSON . parse ( ret . uwLogger ) ;
} catch ( e ) {
return { logToFile : false , logToConsole : false , consoleOptions : { } , fileOptions : { } } ;
}
}
2019-07-16 20:59:12 +02:00
// allow syncing of start times between bg and page scripts.
// may result in negative times in the log file, but that doesn't
// really matter
getStartTime ( ) {
return this . startTime ;
}
setStartTime ( startTime ) {
if ( startTime ) {
this . startTime = startTime ;
} else {
this . startTime = performance . now ( ) ;
}
}
2020-01-06 21:37:40 +01:00
// getLogFileString() {
// let logfileStr = '';
// let logTs = ''; // number of seconds since extension started on a given page¸
// for (let i = 0; i < this.history.length; i++) {
// logTs = ((this.history[i].ts - Math.floor(this.performance.now)) / 3).toFixed(3);
// logfileStr = `${logfileStr}[@${logTs}] -- ${this.history[i].message}\n`
// }
// return logfileStr;
// }
getFileLogJSONString ( ) {
return {
site : window && window . location ,
log : JSON . toString ( this . history ) ,
2019-07-16 20:59:12 +02:00
}
}
2019-07-18 21:25:58 +02:00
pause ( ) {
this . temp _disable = true ;
}
resume ( ) {
this . temp _disable = false ;
}
2019-07-16 22:46:16 +02:00
canLog ( component ) {
2019-07-18 21:25:58 +02:00
return this . canLogFile ( component ) || this . canLogConsole ( component ) ;
}
canLogFile ( component ) {
if ( ! this . conf . fileOptions . enabled || this . temp _disable ) {
return false ;
}
2020-01-06 21:37:40 +01:00
if ( performance . now ( ) > this . stopTime ) {
return false ;
}
2019-11-04 22:14:41 +01:00
if ( Array . isArray ( component ) && component . length ) {
for ( const c of component ) {
if ( this . conf . fileOptions [ c ] ) {
return this . conf . fileOptions [ c ] ;
2019-07-18 21:25:58 +02:00
}
}
2019-11-04 22:14:41 +01:00
} else {
return this . conf . fileOptions [ component ] ;
}
2019-07-18 21:25:58 +02:00
}
canLogConsole ( component ) {
if ( ! this . conf . consoleOptions . enabled || this . temp _disable ) {
return false ;
}
2020-01-06 21:37:40 +01:00
if ( performance . now ( ) > this . stopTime ) {
return false ;
}
2019-08-25 22:00:57 +02:00
if ( Array . isArray ( component ) && component . length ) {
2019-11-04 22:14:41 +01:00
for ( const c of component ) {
if ( this . conf . consoleOptions [ c ] ) {
return this . conf . consoleOptions [ c ] ;
2019-07-16 22:46:16 +02:00
}
}
2019-11-04 22:14:41 +01:00
} else {
return this . conf . consoleOptions [ component ] ;
}
2019-07-16 22:46:16 +02:00
}
2019-07-16 20:59:12 +02:00
// level is unused as of now, but this may change in the future
2019-07-16 22:46:16 +02:00
// levels: 'info', 'warn', 'error'
2019-07-16 20:59:12 +02:00
log ( level , component , ... message ) {
2019-07-16 22:46:16 +02:00
if ( ! this . conf ) {
return ;
}
2020-01-06 21:37:40 +01:00
const error = new Error ( ) ;
2019-07-16 20:59:12 +02:00
if ( this . conf . logToFile ) {
2019-07-18 21:25:58 +02:00
if ( this . canLogFile ( component ) ) {
2019-07-16 20:59:12 +02:00
let ts = performance . now ( ) ;
if ( ts <= this . history [ this . history . length - 1 ] ) {
ts = this . history [ this . history . length - 1 ] + 0.00001 ;
}
this . history . push ( {
ts : ts ,
message : JSON . stringify ( message ) ,
2020-01-06 21:37:40 +01:00
stack : error . stack ,
2019-07-16 20:59:12 +02:00
} )
}
}
if ( this . conf . logToConsole ) {
2019-07-18 21:25:58 +02:00
if ( this . canLogConsole ( component ) ) {
2020-01-06 21:37:40 +01:00
console . log ( ... message , error . stack ) ;
2019-07-16 20:59:12 +02:00
}
}
}
// leaves a noticeable mark in the file log at the time it got triggered, as well as
// at the intervals of 1s and .5s before the trigger moment
cahen ( ) {
if ( this . conf . logToFile ) {
let ts = performance . now ( ) ;
let secondMark = ts - 1000 ;
let halfSecondMark = ts - 500 ;
let i = this . history . length ( ) ;
// correct ts _after_ secondMark and halfSecondMark were determined
if ( ts <= this . history [ this . history . length - 1 ] ) {
ts = this . history [ this . history . length - 1 ] + 0.00001 ;
}
this . history . push ( {
ts : ts ,
message : "-------------------------------------- CAHEN --------------------------------------"
} ) ;
// find the spot for the half second mark. In this case, we don't really particularly care whether timestamps
// are duped due to cahen warnings
while ( this . history [ i -- ] . ts > halfSecondMark ) { }
this . history . push ( {
ts : halfSecondMark ,
message : "-------------------------------------- 0.5s to CAHEN --------------------------------------"
} ) ;
// repeat for one second warning
while ( this . history [ i -- ] . ts > secondMark ) { }
this . history . push ( {
ts : secondMark ,
message : "-------------------------------------- 1s to CAHEN --------------------------------------"
} ) ;
}
}
}
export default Logger ;