From 60050e7511ba48eab45c605563a3fe74d08e39b1 Mon Sep 17 00:00:00 2001 From: moruzerinho6 Date: Sat, 16 Jan 2021 14:30:43 -0300 Subject: [PATCH] implemented logging --- package-lock.json | 31 ++++++++++-- package.json | 6 ++- pageFiles/themes/moondance/assets/main.js | 32 ++++++++---- src/index.js | 62 +++++++++++++++++++++-- 4 files changed, 113 insertions(+), 18 deletions(-) diff --git a/package-lock.json b/package-lock.json index fdbc63a..0166a0a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -98,6 +98,14 @@ } } }, + "caller-id": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/caller-id/-/caller-id-0.1.0.tgz", + "integrity": "sha1-Wb2sCJPRLDhxQIJ5Ix+XRYNk8Hs=", + "requires": { + "stack-trace": "~0.0.7" + } + }, "clone-response": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/clone-response/-/clone-response-1.0.2.tgz", @@ -423,9 +431,7 @@ "lodash": { "version": "4.17.20", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.20.tgz", - "integrity": "sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA==", - "dev": true, - "optional": true + "integrity": "sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA==" }, "lowercase-keys": { "version": "1.0.1", @@ -474,6 +480,11 @@ "minimist": "^1.2.5" } }, + "moment": { + "version": "2.29.1", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.1.tgz", + "integrity": "sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ==" + }, "ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", @@ -648,6 +659,15 @@ "type-fest": "^0.13.1" } }, + "simple-node-logger": { + "version": "18.12.24", + "resolved": "https://registry.npmjs.org/simple-node-logger/-/simple-node-logger-18.12.24.tgz", + "integrity": "sha512-4dTqpYecHsvPjWo+i+J3pLty8WJDNbxOVesNj5ch8pYH95LIGAFH4dxMSqyf+Os0RTchXifEtI/mfm3AVJftmg==", + "requires": { + "lodash": "^4.17.12", + "moment": "^2.20.1" + } + }, "sprintf-js": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.1.2.tgz", @@ -655,6 +675,11 @@ "dev": true, "optional": true }, + "stack-trace": { + "version": "0.0.10", + "resolved": "https://registry.npmjs.org/stack-trace/-/stack-trace-0.0.10.tgz", + "integrity": "sha1-VHxws0fo0ytOEI6hoqFZ5f3eGcA=" + }, "string_decoder": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", diff --git a/package.json b/package.json index 35898fd..ed09caf 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,9 @@ "main": "src/index.js", "dependencies": { "@ibm/plex": "^5.1.3", - "ini-parser": "^0.0.2" + "caller-id": "^0.1.0", + "ini-parser": "^0.0.2", + "simple-node-logger": "^18.12.24" }, "scripts": { "start": "electron src/index.js" @@ -23,7 +25,7 @@ "url": "https://github.com/moruzerinho6/Webini/issues" }, "homepage": "https://github.com/moruzerinho6/Webini#readme", - "bin": "electron src/index.js", + "bin": "electron src/index.js", "pkg": { "targets": [ "node14" diff --git a/pageFiles/themes/moondance/assets/main.js b/pageFiles/themes/moondance/assets/main.js index 0887989..54369c7 100644 --- a/pageFiles/themes/moondance/assets/main.js +++ b/pageFiles/themes/moondance/assets/main.js @@ -81,6 +81,20 @@ window.api.send('toMain', 'start') const pages = [homeElem, aboutElem, downloadsElem, faqElem, controllersElem, noteskinsElem, toolsElem, historicalChangelogElem, communityPoliciesElem] const pagesStr = ['home', 'about', 'downloads', 'faq', 'controllers', 'noteskins', 'tools', 'historicalChangelog', 'communityPolicies'] +/** + * + * @param {string} arg The message to log info. + */ +const info = (arg) => { + window.api.send('toMain', ['logInfo', arg]) +} +/** + * + * @param {string} arg The message to log warn. + */ +const warn = (arg) => { + window.api.send('toMain', ['logWarn', arg]) +} /** * * @param {string} page @@ -105,18 +119,21 @@ const translate = (page, translation) => { } toggleStringsView.onclick = (() => { + info(`Switching keyView mode to ${!window.tinyWebGlobal.keyViewMode}`) window.tinyWebGlobal.keyViewMode = !window.tinyWebGlobal.keyViewMode window.api.send('toMain', 'translationUpdate') // translate(window.tinyWebGlobal.actualPage, window.tinyWebGlobal.translation) }) highlightElements.onclick = (() => { + info(`Switching highlight mode to ${!window.tinyWebGlobal.highlight}`) window.tinyWebGlobal.highlight = !window.tinyWebGlobal.highlight window.api.send('toMain', 'translationUpdate') // translate(window.tinyWebGlobal.actualPage, window.tinyWebGlobal.translation) }) generateFiles.onclick = (() => { + info('Generating HTMl Files') const languageCode = window.tinyWebGlobal.translation.Common.LanguageCode === "en" ? "" : `-${window.tinyWebGlobal.translation.Common.LanguageCode}` const htmlFiles = Object.keys(window.tinyWebGlobal.template) let allReady = false @@ -158,13 +175,6 @@ generateFiles.onclick = (() => { } window.api.send('toMain', ['mkdir', window.tinyWebGlobal.pathToGenerateFiles + '/' + 'static-pages' + languageCode]) - /* - fs.mkdir(window.tinyWebGlobal.pathToGenerateFiles + '/' + 'static-pages' + languageCode, { recursive: true }, err => { - if (err) { - console.log(err) - } - }) - */ const files = ['static-pages-historical-changelog.htm', 'static-pages-about.htm', 'static-pages-addons.htm', 'static-pages-add-ons.noteskins.htm', 'static-pages-faq.htm', 'static-pages-help-support.htm', 'static-pages-community-policies.htm'] @@ -192,6 +202,7 @@ const translationReady = (translation) => { for (let i = 0; i < pages.length; i++) { // Special case for home page with jumbatron if (i === 0) { + info('Switching to home page') pages[0].onclick = (_) => { jumbatronPlaceElem.innerHTML = jumbatron contentDivElem.innerHTML = window.tinyWebGlobal.files[0] @@ -201,16 +212,18 @@ const translationReady = (translation) => { continue } pages[i].onclick = (_) => { + info(`Switching to page ${pagesStr[i]}`) if (window.tinyWebGlobal.actualPage === 'home') { jumbatronPlaceElem.innerHTML = '' } window.tinyWebGlobal.actualPage = pagesStr[i] - contentDivElem.innerHTML = 'hi' //`${window.tinyWebGlobal.files[i]}` + contentDivElem.innerHTML = `${window.tinyWebGlobal.files[i]}` translate(pagesStr[i], translation) } } translationUpdate.onclick = (() => { + info('Requesting translation update') window.api.send('toMain', 'translationUpdate') }) } @@ -221,8 +234,7 @@ const translationReady = (translation) => { * @param {string} [actualPage] */ const translationReload = (translation, actualPage) => { - // The default page is always available in every page, so always update it. + info('Reloading translation') translate('default', translation) - // console.log(actualPage) translate(actualPage, translation) } \ No newline at end of file diff --git a/src/index.js b/src/index.js index 0aa1215..3d2123c 100644 --- a/src/index.js +++ b/src/index.js @@ -7,13 +7,50 @@ const { } = require("electron"); const path = require("path"); const fs = require("fs"); +fs.truncate(path.join(__dirname, '../logs/info.txt'), 0, ()=>{}) +const callerId = require('caller-id') +const logger = require('simple-node-logger').createSimpleFileLogger({ logFilePath: path.join(__dirname, '../logs/info.txt') }) const translation = parser.parseFileSync(path.join(__dirname, '../translated.ini')) const template = require('../template.json') let mainWindow; +/** + * Returns the name of the file from the file path. + * @param {String} path - The file Path + * @returns {String} - name of the file from the file path. + */ +function fileName (path) { + let pathSplit = path.split('\\') + if (pathSplit[0] === path) { + pathSplit = path.split('/') + } + + return pathSplit[pathSplit.length - 1].replace(/.js/gi, '') +} + +/** + * Executes log.info + * @param {String} msg - The message to info. + */ +const info = (msg) => { + const Data = callerId.getData() + logger.info(`[${fileName(Data.filePath)}] at line ${Data.lineNumber} - ${msg}`) +} + +/** + * Executes log.warn + * @param {String} msg - The message to warn. + */ +const warn = (msg) => { + const Data = callerId.getData() + logger.warn(`[${fileName(Data.filePath)}] at line ${Data.lineNumber} - ${msg}`) +} + function createWindow () { // Create the browser window. + + info('Creating window') mainWindow = new BrowserWindow({ width: 800, height: 600, @@ -29,36 +66,52 @@ function createWindow () { }) // and load the index.html of the app. + info('Loading default html') mainWindow.loadFile(path.join(__dirname, "../pageFiles/themes/moondance/assets/default.html")) mainWindow.once('ready-to-show', () => { + info('Showing window') mainWindow.show() }) } ipcMain.on("toMain", (event, args) => { if (args.length === 2) { + info('Recieved a call to Main') if (args[0] === 'writeFile') { + info(`Writing a file, arguments: ${[args[1][0], args[1][1]].join(', ')}`) fs.writeFileSync(args[1][0], args[1][1]) } if (args[0] === 'mkdir') { + info(`Writing a directory to ${args[1]}`) fs.mkdir(args[1], { recursive: true }, err => { - if (err) console.log(err) + if (err) warn(`Error while writing a directory, error: ${err}`) }) } if (args[0] === 'rename') { + info(`Renaming (or moving) ${args[1][0]} to ${args[1][1]}`) fs.rename(args[1][0], args[1][1], (err) => { - if (err) console.log(err) + if (err) warn(`Error while writing a renaming, error: ${err}`) }) } + + if (args[0] === 'logInfo') { + info(args[1]) + } + + if (args[0] === 'logwarn') { + warn(args[1]) + } return } if (args === 'translationUpdate') { + info('An translation update was requested, updating translation.') mainWindow.webContents.send('fromMain', ['translationUpdate', parser.parseFileSync(path.join(__dirname, '../translated.ini'))]) return } + info('Sending files, template, translation to isolated window.') mainWindow.webContents.send('fromMain', ['translation', translation]) mainWindow.webContents.send('fromMain', ['template', template]) mainWindow.webContents.send('fromMain', ['pathGeneratedFiles', path.join(__dirname, '../generatedFiles')]) @@ -80,6 +133,7 @@ ipcMain.on("toMain", (event, args) => { }) app.whenReady().then(() => { + info('App is ready, creating window') createWindow() app.on('activate', function () { @@ -87,7 +141,9 @@ app.whenReady().then(() => { }) }) - +/* +We don't support mac at the moment. app.on('window-all-closed', function () { if (process.platform !== 'darwin') app.quit() }) +*/