You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.7 KiB
67 lines
1.7 KiB
const path = require('path'); |
|
const fs = require('fs'); |
|
|
|
const getAppSupportFolder = () => { |
|
let folder; |
|
if(process.platform == 'darwin') { |
|
folder = path.resolve(process.env.HOME, 'Library/Application Support/seedpacket') |
|
} else if(process.platform == 'win32') { |
|
folder = path.resolve(process.env.APPDATA, 'seedpacket') |
|
} else { |
|
folder = path.resolve(process.env.HOME, '.local/share/seedpacket') |
|
} |
|
return folder; |
|
}; |
|
|
|
|
|
|
|
let myLogInternal = (message) => { |
|
console.log(message); |
|
}; |
|
|
|
const myLog = (message) => { |
|
myLogInternal(message); |
|
}; |
|
|
|
const startLoggingToFile = (logFileName) => { |
|
const logFilePath = path.join(getAppSupportFolder(), logFileName); |
|
logStream = fs.createWriteStream(logFilePath, {flags:'a', mode: 0o666}); |
|
myLogInternal = (message) => { |
|
logStream.write(`\n${new Date().toISOString()} ${message}`); |
|
console.log(message); |
|
}; |
|
}; |
|
|
|
|
|
function BytesPerSecondGuage() { |
|
this.totalBytes = 0; |
|
this.timeslices = {}; |
|
|
|
this.getBytesPerSecond = () => { |
|
const timeslice = Math.round(new Date().getTime()/100); |
|
let lastSecondBytes = 0; |
|
Object.keys(this.timeslices).forEach(key => { |
|
if(key < timeslice - 10) { |
|
delete this.timeslices[key]; |
|
} else { |
|
lastSecondBytes += this.timeslices[key].reduce((a,b) => a+b, 0); |
|
} |
|
}); |
|
return lastSecondBytes; |
|
}; |
|
|
|
this.recordBytes = (bytes) => { |
|
const timeslice = Math.round(new Date().getTime()/100); |
|
this.timeslices[timeslice] = this.timeslices[timeslice] || []; |
|
this.timeslices[timeslice].push(bytes); |
|
this.totalBytes += bytes; |
|
}; |
|
} |
|
|
|
|
|
module.exports = { |
|
getAppSupportFolder, |
|
myLog, |
|
startLoggingToFile, |
|
BytesPerSecondGuage |
|
}; |