Skip to content

Commit

Permalink
log optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
luyuhuang committed Sep 25, 2021
1 parent 071d1d9 commit 20bd76a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 35 deletions.
12 changes: 3 additions & 9 deletions src/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const settings = require('./settings');
const { synchronize } = require("./sync");
const { dialog, app, BrowserWindow, shell } = require("electron");
const { readFile, writeFile } = require("fs/promises");
const { currentLog } = require("./log");
const { currentLogPath } = require("./log");


function close(event) {
Expand Down Expand Up @@ -378,14 +378,8 @@ async function resetPlan(_, id) {
where plan_id = ?`, Date.now(), id);
}

async function openLog() {
const path = await currentLog();
if (!path) {
dialog.showMessageBox(getMainWin(), {message: 'No log file found.', type: 'error'});
return;
}

shell.openPath(path);
function openLog() {
shell.openPath(currentLogPath());
}

function openDataDir() {
Expand Down
34 changes: 9 additions & 25 deletions src/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,6 @@ function zeroPad(n, l) {
return n;
}

function dateStr() {
const date = new Date();
const year = date.getFullYear(), month = zeroPad(date.getMonth() + 1, 2), day = zeroPad(date.getDate(), 2);
const hour = date.getHours(), minute = zeroPad(date.getMinutes(), 2), second = zeroPad(date.getSeconds(), 2);
const offset = date.getTimezoneOffset();
const timezone = zeroPad(((o) => Math.floor(o / 60) * 100 + offset % 60)(Math.abs(offset)), 4);
const tzsig = offset < 0 ? '+' : '-';

return `${year}-${month}-${day}T${hour}:${minute}:${second}${tzsig}${timezone}`;
}

async function clearOldLogs() {
const now = Date.now();
const files = await readdir(LOG_DIR);
Expand All @@ -32,35 +21,30 @@ async function clearOldLogs() {
}).map(f => unlink(path.join(LOG_DIR, f))));
}

let logPath;
async function initLog() {
await mkdir(LOG_DIR, {recursive: true});
await clearOldLogs();

const log = createWriteStream(path.join(LOG_DIR, `${dateStr()}.${process.pid}.log`), {flags: 'a'});
const date = new Date();
const year = date.getFullYear(), month = zeroPad(date.getMonth() + 1, 2), day = zeroPad(date.getDate(), 2);
logPath = path.join(LOG_DIR, `${year}-${month}-${day}.log`);

const log = createWriteStream(logPath, {flags: 'a'});
const write = o => (f => (...args) => (log.write(...args), f(...args)))(o.write.bind(o));
process.stdout.write = write(process.stdout);
process.stderr.write = write(process.stderr);
}
exports.initLog = initLog;

const log = (tag) => function(msg, ...args) {
console.log(`${dateStr()} [${tag}] ${msg}`, ...args);
const date = new Date().toLocaleString();
console.log(`${date} [${tag}] ${msg}`, ...args);
};

exports.debug = log('DEBUG');
exports.info = log('INFO');
exports.warn = log('WARN');
exports.error = log('ERROR');

async function currentLog() {
const suffix = `.${process.pid}.log`;
const files = await readdir(LOG_DIR);
const time = files.filter(f => f.endsWith(suffix))
.map(f => f.slice(0, -suffix.length))
.reduce((a, b) => Date.parse(a) > Date.parse(b) ? a : b, 0);

if (!time) return;

return path.join(LOG_DIR, time + suffix);
}
exports.currentLog = currentLog;
exports.currentLogPath = () => logPath;
2 changes: 1 addition & 1 deletion templates/components/AdvancedSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default {
},
openLog() {
ipcRenderer.invoke('openLog');
ipcRenderer.send('openLog');
},
openDataDir() {
ipcRenderer.send('openDataDir');
Expand Down

0 comments on commit 20bd76a

Please sign in to comment.