Try to improve logging. It's not there, yet.

This commit is contained in:
mmcwilliams 2019-03-08 22:33:03 -05:00
parent e186b1b9fc
commit 95b6f77848
1 changed files with 32 additions and 12 deletions

View File

@ -7,7 +7,8 @@ const electron = require('electron')
const { Menu, MenuItem, ipcMain, BrowserWindow, app } = electron const { Menu, MenuItem, ipcMain, BrowserWindow, app } = electron
const fs = require('fs') const fs = require('fs')
const os = require('os') const os = require('os')
const winston = require('winston') const { createLogger, format, transports } = require('winston')
const { combine, timestamp, label, printf, prettyPrint } = format
const moment = require('moment') const moment = require('moment')
const uuid = require('uuid') const uuid = require('uuid')
const events = require('events') const events = require('events')
@ -216,7 +217,7 @@ dev.connectDevice = async function (device, type) {
} else if (type === 'projector,light') { } else if (type === 'projector,light') {
dev.connected.projector = device dev.connected.projector = device
dev.connected.light = device dev.connected.light = device
arduino.alias('light', device) arduino.aliasSerial('light', device)
try{ try{
connectSuccess = await arduino.connect('projector', device, false) connectSuccess = await arduino.connect('projector', device, false)
} catch (err) { } catch (err) {
@ -229,8 +230,8 @@ dev.connectDevice = async function (device, type) {
dev.connected.projector = device dev.connected.projector = device
dev.connected.camera = device dev.connected.camera = device
dev.connected.light = device dev.connected.light = device
arduino.alias('camera', device) arduino.aliasSerial('camera', device)
arduino.alias('light', device) arduino.aliasSerial('light', device)
try { try {
connectSuccess = await arduino.connect('projector', device, false) connectSuccess = await arduino.connect('projector', device, false)
} catch (err) { } catch (err) {
@ -242,7 +243,7 @@ dev.connectDevice = async function (device, type) {
} else if (type === 'projector,camera') { } else if (type === 'projector,camera') {
dev.connected.projector = device dev.connected.projector = device
dev.connected.camera = device dev.connected.camera = device
arduino.alias('camera', device) arduino.aliasSerial('camera', device)
try { try {
connectSuccess = await arduino.connect('projector', device, false) connectSuccess = await arduino.connect('projector', device, false)
} catch (err) { } catch (err) {
@ -252,7 +253,7 @@ dev.connectDevice = async function (device, type) {
log.info(`Connected to ${device} as PROJECTOR`, 'SERIAL', true, true) log.info(`Connected to ${device} as PROJECTOR`, 'SERIAL', true, true)
} else if (type === 'projector_second') { } else if (type === 'projector_second') {
dev.connected.projector_second = device dev.connected.projector_second = device
arduino.alias('projector_second', device) arduino.aliasSerial('projector_second', device)
try { try {
connectSuccess = await arduino.connect('projector_second', device, false) connectSuccess = await arduino.connect('projector_second', device, false)
} catch (err) { } catch (err) {
@ -763,10 +764,28 @@ log.file = function () {
return path.join(logPath, 'mcopy.log') return path.join(logPath, 'mcopy.log')
} }
log.time = 'MM/DD/YY-HH:mm:ss' log.time = 'MM/DD/YY-HH:mm:ss'
log.transport = winston.createLogger({ log.formatter = (options) => {
console.dir(options)
return options.timestamp() +' ['+ options.level.toUpperCase() +'] '+ (undefined !== options.message ? options.message : '') +
(options.meta && Object.keys(options.meta).length ? '\n\t'+ JSON.stringify(options.meta) : '' );
}
log.transport = createLogger({
transports: [ transports: [
new (winston.transports.Console)(), new (transports.Console)({
new (winston.transports.File)({ filename: log.file() }) json : false,
format : combine(
//label({ label: 'mcopy-app' }),
//timestamp(),
//format.colorize({ all: true }),
//format.splat(),
//format.json(),
//format.simple(),
log.formatter
)
}),
new (transports.File)({
filename: log.file()
})
] ]
}) })
log.init = function () { log.init = function () {
@ -777,18 +796,19 @@ log.display = function (obj) {
} }
log.listen = function () { log.listen = function () {
ipcMain.on('log', (event, arg) => { ipcMain.on('log', (event, arg) => {
log.transport.info('renderer', arg) arg.source = 'renderer'
log.transport.info(arg)
event.returnValue = true event.returnValue = true
}) })
} }
log.info = function (action, service, status, display) { log.info = function (action, service, status, display) {
var obj = { var obj = {
time : moment().format(log.time), source: 'main',
action : action, action : action,
service : service, service : service,
status : status status : status
} }
log.transport.info('main', obj) log.transport.info(obj)
if (display) { if (display) {
log.display(obj) log.display(obj)
} }