Skip to content

Commit

Permalink
Add Logger. Update menu transform option
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Varache committed Mar 8, 2018
1 parent 0ac76ab commit ea2de65
Show file tree
Hide file tree
Showing 8 changed files with 293 additions and 39 deletions.
52 changes: 37 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ ShellMenu.addItem('my-item', { label: 'My Menu Item', click() { console.log('My
// Includes automatic window state managment and UWP style titlebar for windows
// Custom preload as module supported
const shell = new Shell({
name: 'My App',
root: CONTENT_ROOT,
scheme: CONTENT_SCHEME,
// Can provide custom width and height
Expand All @@ -81,29 +82,50 @@ const shell = new Shell({
// Enable UWP style titlebar
uwpTitlebar: true,
devMode: false,
menuTransform(menu) {
const submenu = [ShellMenu.createMenuItem('my-item')];
// Can update when dev mode changes
if (shell.isDevMode()) {
submenu.push(ShellMenu.createMenuItem('separator'));
// Can add custom runtime generated menu items
submenu.push({ label: 'Dev option' });
}
// Inject in list of menus
menu.splice(1, 0, {
label: 'Custom Menu',
submenu,
});
return menu;
menu: {
transform(menu) {
const submenu = [ShellMenu.createMenuItem('my-item')];
// Can update when dev mode changes
if (shell.isDevMode()) {
submenu.push(ShellMenu.createMenuItem('separator'));
// Can add custom runtime generated menu items
submenu.push({ label: 'Dev option' });
}
// Inject in list of menus
menu.splice(1, 0, {
label: 'Custom Menu',
submenu,
});
return menu;
},
},
// Set custom window options through this object
windowOptions: {
icon: path.join(__dirname, 'res/icon_180.png'),
},
// Log options allows to customize logging
log: {
// Default output lod level
level: 'debug',
// File options. Log files are located in the app.getPath('userData') directory
file: {
// Options passed down to bunyan 'rotating-file'
period: '12h',
count: 14,
},
// options applied once devMode is enabled
devMode: {
level: 'debug',
file: {
level: 'debug',
},
}
},
});

app.on('ready', () => {
shell.createWindow();
// Logger can be found here
const { log } = shell;
// The main window is accessible through properties
console.log(shell.window);
});
Expand Down
45 changes: 28 additions & 17 deletions app/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ const CONTENT_ROOT = path.join(__dirname, './src');
// Custom menu Item added
ShellMenu.addItem('my-item', { label: 'My Menu Item', click() { console.log('My Menu Item'); } });

app.setName('My app');

// Create new Shell with custom options
// Includes automatic window state managment and UWP style titlebar for windows
// Custom preload as module supported
const shell = new Shell({
name: 'My App',
root: CONTENT_ROOT,
scheme: CONTENT_SCHEME,
// Can provide custom width and height
Expand All @@ -25,28 +24,40 @@ const shell = new Shell({
// Enable UWP style titlebar
uwpTitlebar: true,
devMode: false,
menuTransform(menu) {
const submenu = [ShellMenu.createMenuItem('my-item')];
// Can update when dev mode changes
if (shell.isDevMode()) {
submenu.push(ShellMenu.createMenuItem('separator'));
// Can add custom runtime generated menu items
submenu.push({ label: 'Dev option' });
}
// Inject in list of menus
menu.splice(1, 0, {
label: 'Custom Menu',
submenu,
});
return menu;
menu: {
transform(menu) {
const submenu = [ShellMenu.createMenuItem('my-item')];
// Can update when dev mode changes
if (shell.isDevMode()) {
submenu.push(ShellMenu.createMenuItem('separator'));
// Can add custom runtime generated menu items
submenu.push({ label: 'Dev option' });
}
// Inject in list of menus
menu.splice(1, 0, {
label: 'Custom Menu',
submenu,
});
return menu;
},
},
windowOptions: {
icon: path.join(__dirname, 'res/icon_180.png'),
},
log: {
level: 'info',
file: {
period: '1d',
count: 7,
},
},
});

app.on('ready', () => {
shell.createWindow();
// Can access logger through the shell object
const { log } = shell;
log.info('My has started');
// The main window is accessible through properties
console.log(shell.window);
// console.log(shell.window);
});
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const Shell = require('./shell');
const ShellMenu = require('./menu');
const Logger = require('./logger');

module.exports = {
Shell,
ShellMenu,
Logger,
};
73 changes: 73 additions & 0 deletions lib/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const path = require('path');
const bunyan = require('bunyan');
const bunyanDebugStream = require('bunyan-debug-stream');
const { app } = require('electron');

const loggers = new Map();

const defaultOptions = {
level: 'info',
file: {
level: 'warn',
period: '1d',
count: 7,
},
devMode: {
level: 'debug',
file: {
level: 'debug',
},
},
};

const Logger = {
getLogger(opts, name = null) {
const loggerName = name || app.getName();
const fileOptions = Object.assign({}, defaultOptions.file, opts.file || {});
const options = Object.assign({}, defaultOptions, opts);
if (!loggers.has(loggerName)) {
const streams = [{
level: options.level,
type: 'raw',
stream: bunyanDebugStream(),
name: 'stream',
}];
// Setting the file property to false will disable it
if (opts.file !== false) {
streams.push({
level: fileOptions.level,
type: 'rotating-file',
path: path.join(app.getPath('userData'), `${loggerName}.log`),
name: 'file',
period: fileOptions.period,
count: fileOptions.count,
});
}
const logger = bunyan.createLogger({
name: loggerName,
streams,
serializers: bunyanDebugStream.serializers,
});
loggers.set(loggerName, logger);
}
return loggers.get(loggerName);
},
enableDevMode(opts, name = null) {
const loggerName = name || app.getName();
if (!loggers.has(loggerName)) {
return;
}
const level = opts.devMode
&& opts.devMode.level
? opts.devMode.level : defaultOptions.devMode.level;
const fileLevel = opts.devMode
&& opts.devMode.file
&& opts.devMode.file.level
? opts.devMode.file.level : defaultOptions.devMode.file.level;
const logger = loggers.get(loggerName);
logger.levels('stream', level);
logger.levels('file', fileLevel);
},
};

module.exports = Logger;
32 changes: 26 additions & 6 deletions lib/menu/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { app, Menu } = require('electron');
const { app, shell, Menu } = require('electron');

const ITEMS = {
about: {
Expand Down Expand Up @@ -42,13 +42,15 @@ const ITEMS = {
toggledevtools: { role: 'toggledevtools' },
};

const defaultOptions = {
transform(menu) {
return menu;
},
};

class ShellMenu {
constructor(window, about, options) {
this.options = Object.assign({
transform(menu) {
return menu;
},
}, options);
this.options = Object.assign({}, defaultOptions, options);
this.window = window;

/* Menus for context menus */
Expand Down Expand Up @@ -129,7 +131,25 @@ class ShellMenu {
submenu: [],
};

if (debug) {
help.submenu.push({
label: 'Open logs directory',
click: () => {
let stream;
for (let i = 0; i < this.window.shell.log.streams.length; i += 1) {
stream = this.window.shell.log.streams[i];
if (stream.name === 'file') {
shell.showItemInFolder(stream.path);
break;
}
}
},
});
}
if (process.platform !== 'darwin') {
if (help.submenu.length) {
help.submenu.push(ShellMenu.createMenuItem('separator'));
}
help.submenu.push(this.aboutItem);
}

Expand Down
10 changes: 9 additions & 1 deletion lib/shell/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const About = require('../about');
const MenuGenerator = require('../menu');
const path = require('path');
const windowStateKeeper = require('electron-window-state');
const Logger = require('../logger');

const defaultOptions = {
scheme: 'shell',
Expand All @@ -21,6 +22,7 @@ const defaultOptions = {
show: false,
autoHideMenuBar: true,
},
name: 'Kano Desktop Shell',
};

class Shell {
Expand All @@ -29,6 +31,8 @@ class Shell {
if (!this.options.root) {
throw new Error('Missing root option');
}
app.setName(this.options.name);
this.log = Logger.getLogger(this.options.log || {});
protocol.registerStandardSchemes([this.options.scheme], { secure: true });
app.on('ready', () => {
appServer.registerProtocol(this.options.scheme, this.options.root);
Expand All @@ -40,9 +44,13 @@ class Shell {
this.about.on('enable-dev-mode', () => {
this.options.devMode = true;
this.updateMenu();
Logger.enableDevMode(this.options.log || {});
this.log.debug('Developer mode enabled');
});
this.log.level(this.options.logLevel);
}
createWindow() {
this.log.debug('Creating new window');
this.windowState = windowStateKeeper({
defaultWidth: this.options.width,
defaultHeight: this.options.height,
Expand All @@ -63,7 +71,7 @@ class Shell {
this.menuGenerator = new MenuGenerator(
this.window,
this.about,
{ transform: this.options.menuTransform },
this.options.menu || {},
);

this.updateMenu();
Expand Down
Loading

0 comments on commit ea2de65

Please sign in to comment.