diff --git a/client/src/log.js b/client/src/log.js new file mode 100644 index 0000000000000000000000000000000000000000..e6bed8c7d86640414ccde674c78a1778a96b0bab --- /dev/null +++ b/client/src/log.js @@ -0,0 +1,9 @@ +const logger = require('electron-log'); +logger.transports.file.resolvePath = () => require("path").join(require("os").homedir(), 'ztf', 'log', 'electron.log'); + +export function logInfo(str) { + logger.info(str); +} +export function logErr(str) { + logger.error(str); +} diff --git a/client/src/main.js b/client/src/main.js index 1b9387877bd6b0e711e04fc3591cbbca2d782710..1c6786b522c3f84961b034bd8cf6f2e3c03234e9 100644 --- a/client/src/main.js +++ b/client/src/main.js @@ -1,5 +1,6 @@ import {app, BrowserWindow} from 'electron'; -import {getUIServerUrl, startZtfServer, killZtfServer, logInfo, logErr} from './services'; +import {getUIServerUrl, startZtfServer, killZtfServer} from './services'; +import {logInfo, logErr} from './log'; // Handle creating/removing shortcuts on Windows when installing/uninstalling. if (require('electron-squirrel-startup')) { // eslint-disable-line global-require @@ -43,7 +44,7 @@ async function startApp() { const url = await getUIServerUrl(); - console.log('>> UI server url is', url); + logInfo('>> UI server url is', url); createWindow(url); diff --git a/client/src/services.js b/client/src/services.js index b9c63d43be11d43fe3e8f0945d357d141a2d22c8..a47689d45582d33d503019a9acbb649c8db8cf70 100644 --- a/client/src/services.js +++ b/client/src/services.js @@ -3,6 +3,7 @@ import {spawn} from 'child_process'; import os from 'os'; import {app} from 'electron'; import express from 'express'; +import {logInfo, logErr} from './log'; const DEBUG = process.env.NODE_ENV === 'development'; @@ -10,7 +11,7 @@ let _ztfServerProcess; export function startZtfServer() { if (process.env.SKIP_SERVER) { - console.log(`>> Skip to start ZTF Server by env "SKIP_SERVER=${process.env.SKIP_SERVER}".`); + logInfo(`>> Skip to start ZTF Server by env "SKIP_SERVER=${process.env.SKIP_SERVER}".`); return Promise.resolve(); } if (_ztfServerProcess) { @@ -29,13 +30,13 @@ export function startZtfServer() { } return new Promise((resolve, reject) => { const cwd = process.env.SERVER_CWD_PATH || path.dirname(serverExePath); - console.log(`>> Starting ZTF Server from exe path with command "${serverExePath} -P 8085" in "${cwd}"...`); + logInfo(`>> Starting ZTF Server from exe path with command "${serverExePath} -P 8085" in "${cwd}"...`); const cmd = spawn(serverExePath, ['-P', '8085'], { cwd, shell: true, }); cmd.on('close', (code) => { - console.log(`>> ZTF server closed with code ${code}`); + logInfo(`>> ZTF server closed with code ${code}`); _ztfServerProcess = null; cmd.kill() }); @@ -45,7 +46,7 @@ export function startZtfServer() { for (let i = 0; i < lines.length; i++) { const line = lines[i]; if (DEBUG) { - console.log('\t', line); + logInfo('\t', line); } if (line.includes('Now listening on: http')) { resolve(line.split('Now listening on:')[1].trim()); @@ -75,13 +76,13 @@ export function startZtfServer() { return new Promise((resolve, reject) => { const cwd = process.env.SERVER_CWD_PATH || path.resolve(app.getAppPath(), '../'); - console.log(`>> Starting ZTF development server from source with command "go run cmd/server/main.go -P 8085" in "${cwd}"`); + logInfo(`>> Starting ZTF development server from source with command "go run cmd/server/main.go -P 8085" in "${cwd}"`); const cmd = spawn('go', ['run', 'main.go', '-P', '8085'], { cwd, shell: true, }); cmd.on('close', (code) => { - console.log(`>> ZTF server closed with code ${code}`); + logInfo(`>> ZTF server closed with code ${code}`); _ztfServerProcess = null; }); cmd.stdout.on('data', data => { @@ -90,7 +91,7 @@ export function startZtfServer() { for (let i = 0; i < lines.length; i++) { const line = lines[i]; if (DEBUG) { - console.log('\t', line); + logInfo('\t', line); } if (line.includes('Now listening on: http')) { resolve(line.split('Now listening on:')[1].trim()); @@ -139,7 +140,7 @@ export function getUIServerUrl() { } const port = process.env.UI_SERVER_PORT || 8000; - console.log(`>> Starting UI serer at ${uiServerUrl} with port ${port}`); + logInfo(`>> Starting UI serer at ${uiServerUrl} with port ${port}`); const uiServer = express(); uiServer.use(express.static(uiServerUrl)); @@ -149,7 +150,7 @@ export function getUIServerUrl() { _uiServerApp = null; reject(serverError); } else { - console.log(`>> UI server started successfully on http://localhost:${port}.`); + logInfo(`>> UI server started successfully on http://localhost:${port}.`); resolve(`http://localhost:${port}`); } }); @@ -162,7 +163,7 @@ export function getUIServerUrl() { return new Promise((resolve, reject) => { const cwd = path.resolve(app.getAppPath(), '../ui'); - console.log(`>> Starting UI development server with command "npm run serve" in "${cwd}"...`); + logInfo(`>> Starting UI development server with command "npm run serve" in "${cwd}"...`); let resolved = false; const cmd = spawn('npm', ['run', 'serve'], { @@ -170,7 +171,7 @@ export function getUIServerUrl() { shell: true, }); cmd.on('close', (code) => { - console.log(`>> ZTF server closed with code ${code}`); + logInfo(`>> ZTF server closed with code ${code}`); _uiServerApp = null; }); cmd.stdout.on('data', data => { @@ -182,12 +183,12 @@ export function getUIServerUrl() { for (let i = 0; i < lines.length; i++) { const line = lines[i]; if (DEBUG) { - console.log('\t', line); + logInfo('\t', line); } if (line.includes('App running at:')) { const nextLine = lines[i + 1] || lines[i + 2]; if (DEBUG) { - console.log('\t', nextLine); + logInfo('\t', nextLine); } if (!nextLine) { console.error('\t', `Cannot grabing running address after line "${line}".`); @@ -211,13 +212,3 @@ export function getUIServerUrl() { _uiServerApp = cmd; }); } - - -const logger = require('electron-log'); -logger.transports.file.resolvePath = () => require("path").join(require("os").homedir(), 'ztf', 'log', 'electron.log'); -export function logInfo(str) { - logger.info(str); -} -export function logErr(str) { - logger.error(str); -} \ No newline at end of file