Skip to content

Commit

Permalink
feat(log): using tagslog
Browse files Browse the repository at this point in the history
  • Loading branch information
DEgITx committed Aug 1, 2021
1 parent 1ab96fb commit 750dbfd
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 63 deletions.
26 changes: 24 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
"react-minimal-pie-chart": "^8.2.0",
"simple-encryptor": "github:DEgITx/node-simple-encryptor",
"stun": "1.1.0",
"tagslog": "^1.1.2",
"webtorrent": "github:DEgITx/webtorrent"
},
"devDependencies": {
Expand Down
37 changes: 5 additions & 32 deletions src/background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,39 +75,12 @@ const util = require('util');
if (!fs.existsSync(app.getPath("userData"))){
fs.mkdirSync(app.getPath("userData"));
}
const logFile = fs.createWriteStream(app.getPath("userData") + '/rats.log', {flags : 'w'});
const logStdout = process.stdout;

const colors = require('ansi-256-colors');
const stringHashCode = (str) => {
let hash = 0, i, chr;
if (str.length === 0)
return hash;
for (i = 0; i < str.length; i++) {
chr = str.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};

console.log = (...d) => {
const date = (new Date).toLocaleTimeString()
logFile.write(`[${date}] ` + util.format(...d) + '\n');
logStdout.write(util.format(...d) + '\n');
};

global.logT = (type, ...d) => {
const date = (new Date).toLocaleTimeString()
logFile.write(`[${date}] [${type}] ` + util.format(...d) + '\n');
logStdout.write(colors.fg.codes[Math.abs(stringHashCode(type)) % 256] + `[${type}]` + colors.reset + ' ' + util.format(...d) + '\n');
}

global.logTE = (type, ...d) => {
const date = (new Date).toLocaleTimeString()
logFile.write(`\n[${date}] [ERROR] [${type}] ` + util.format(...d) + '\n\n');
logStdout.write(colors.fg.codes[Math.abs(stringHashCode(type)) % 256] + `[${type}]` + colors.reset + ' ' + colors.fg.codes[9] + util.format(...d) + colors.reset + '\n');
}
require('tagslog')({
logFile: app.getPath("userData") + '/rats.log',
stdout: (log) => process.stdout.write(log + '\n'),
overrideConsole: true
});

// print os info
logT('system', 'Rats', app.getVersion())
Expand Down
30 changes: 1 addition & 29 deletions src/background/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,10 @@ const io = require('socket.io')(server);
const fs = require('fs');
const path = require('path')
const os = require('os')
require('tagslog')({logFile: 'rats.log'});

const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'));

const util = require('util');
const colors = require('ansi-256-colors');
const stringHashCode = (str) => {
let hash = 0, i, chr;
if (str.length === 0)
return hash;
for (i = 0; i < str.length; i++) {
chr = str.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};

const logFile = fs.createWriteStream('rats.log', {flags : 'w'});

global.logT = (type, ...d) => {
const date = (new Date).toLocaleTimeString()
console.log(colors.fg.codes[Math.abs(stringHashCode(type)) % 256] + `[${type}]` + colors.reset + ' ' + util.format(...d));
logFile.write(`[${date}] ` + util.format(...d) + '\n');
}

global.logTE = (type, ...d) => {
const date = (new Date).toLocaleTimeString()
console.log(colors.fg.codes[Math.abs(stringHashCode(type)) % 256] + `[${type}]` + colors.reset + ' ' + colors.fg.codes[9] + util.format(...d) + colors.reset + '\n');
logFile.write(`[${date}] ` + util.format(...d) + '\n');
}


server.listen(appConfig.httpPort);
logT('system', 'Rats v' + packageJson.version)
logT('system', 'Listening web server on', appConfig.httpPort, 'port')
Expand Down

0 comments on commit 750dbfd

Please sign in to comment.