From a16eaf109600672aa264655b8adc2b5a7bfa128a Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Mon, 12 Oct 2020 12:40:53 +0200
Subject: [PATCH] INSTANCE: Add the logger for all the application

Will write averything to a file 'lektor-app.log' and output it to stdout.
---
 LICENSE                 |  9 +++++----
 common/logger.js        | 22 +++++++++++-----------
 instance/main.js        |  6 +++---
 instance/views/main.ejs |  0
 main.js                 | 21 +++++++++++++++------
 package.json            |  4 +++-
 6 files changed, 37 insertions(+), 25 deletions(-)
 delete mode 100644 instance/views/main.ejs

diff --git a/LICENSE b/LICENSE
index 3a1116e..d2cf7e8 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,8 +1,9 @@
 Copyright 2020 to Lektor App contributors
 
-Permission to use, copy, modify, and/or distribute this software for any purpose
-with or without fee is hereby granted, provided that the above copyright notice
-and this permission notice appear in all copies.
+Permission to use, copy, modify, and/or distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice with the CONTRIBUTORS file and this permission notice appear
+in all copies.
 
 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
 REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
@@ -10,4 +11,4 @@ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
 INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
 OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
-THIS SOFTWARE.
\ No newline at end of file
+THIS SOFTWARE.
diff --git a/common/logger.js b/common/logger.js
index 6bea962..f27008d 100644
--- a/common/logger.js
+++ b/common/logger.js
@@ -30,17 +30,13 @@ const __loggerCustomLevels = {
     }
 };
 
-var transport;
-if (fs.existsSync("/dev/stderr")) {
-    /* /dev/stderr exists */
-    transport = new winston.transports.Stream({stream: fs.createWriteStream('/dev/stderr')});
-} else {
-    /* No /dev/stderr, may be windob' */
-    transport = new winston.transports.Console();
-}
-
-module.exports = global.logger = module.exports = winston.createLogger({
-    transports: [ transport ],
+var logger = winston.createLogger({
+    transports: [ new winston.transports.File({
+					eol: "\n",
+					filename: "./lektor-app.log",
+					maxsize: "10M",
+					tailable: true,
+				}) ],
     level: "debug",
     json: false,
     handleExceptions: true,
@@ -55,3 +51,7 @@ module.exports = global.logger = module.exports = winston.createLogger({
         })
     )
 });
+
+Object.assign(logger, {logfile: "./lektor-app.log"});
+
+module.exports = global.logger = module.exports = logger;
diff --git a/instance/main.js b/instance/main.js
index 14abccc..06f6d97 100644
--- a/instance/main.js
+++ b/instance/main.js
@@ -3,9 +3,9 @@ const logger          = require('../common/logger.js');
 
 const karaJSON = require('../test/dummyKara.json');
 const buttonList = [
-    [ "left", "Play", "commandPlay" ],
-    [ "left", "Stop", "commandStop" ],
-    [ "right", "Quit LektorApp", "closeButton" ]
+    [ "left",   "Play",             "commandPlay" ],
+    [ "left",   "Stop",             "commandStop" ],
+    [ "right",  "Quit LektorApp",   "closeButton" ]
 ];
 
 window.onload = () => {
diff --git a/instance/views/main.ejs b/instance/views/main.ejs
deleted file mode 100644
index e69de29..0000000
diff --git a/main.js b/main.js
index 66f68c6..a712539 100644
--- a/main.js
+++ b/main.js
@@ -2,13 +2,20 @@ const logger                 = require.main.require('./common/logger.js');
 const electron               = require('electron')
 const { app, BrowserWindow } = require('electron')
 const { ipcMain }            = require('electron')
-const { fork }               = require('child_process');
+const { fork, spawn }        = require('child_process');
+const fs					 = require("fs");
+var tail   					 = require('tail').Tail;
+var client;		/* Sub process for the express server */
 
-/* Sub process for the express server */
-var client;
+/* Reinit the logfile */
+fs.truncate(logger.logfile, 0, () => {});
+var tail = new tail(logger.logfile);
 
-/** Creates the main window and process for the admin interface
- */
+tail.on("line", function(data) {
+    console.log(data);
+});
+
+/* Creates the main window and process for the admin interface */
 function createInstanceWindow () {
     const win = new BrowserWindow({
         width: 1280,
@@ -20,7 +27,7 @@ function createInstanceWindow () {
     })
 
     win.loadFile('instance/index.html');
-    win.webContents.openDevTools();
+    // win.webContents.openDevTools();
 }
 
 ipcMain.on('close-app', (evt, arg) => {
@@ -28,6 +35,7 @@ ipcMain.on('close-app', (evt, arg) => {
 })
 
 app.on("quit", () => {
+	logger.info("Send SIGTERM to express process and tailler process");
     client.kill("SIGTERM");
 });
 
@@ -36,3 +44,4 @@ app.on('ready', () => {
     createInstanceWindow();
     client = fork('client/main.js');
 });
+
diff --git a/package.json b/package.json
index 7c424db..3dc218d 100644
--- a/package.json
+++ b/package.json
@@ -11,13 +11,15 @@
   ],
   "license": "ISC",
   "scripts": {
-    "start": "electron ."
+    "start": "electron .",
+    "test": "ELECTRON_ENABLE_LOGGING=0 ELECTRON_NO_ATTACH_CONSOLE=true electron ."
   },
   "dependencies": {
     "ejs": "^3.1.5",
     "electron": "^10.1.1",
     "express": "^4.17.1",
     "sqlite3": "^5.0.0",
+    "tail": "^2.0.4",
     "winston": "^3.3.3"
   },
   "config": {
-- 
GitLab