Skip to content

Commit

Permalink
Accept an optional external logger, pass this to SCLang and SCServer.
Browse files Browse the repository at this point in the history
Support dryadic's rootContext

This allows use of standard logging tools like winston (remote logging,
log to file, log to database)
  • Loading branch information
crucialfelix committed Apr 21, 2016
1 parent 60627cb commit 1696372
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 23 deletions.
3 changes: 2 additions & 1 deletion src/dryads/SCLang.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import {Dryad} from 'dryadic';
import {boot} from '../lang/sclang';
import * as _ from 'underscore';

const defaultOptions = {
debug: true,
Expand Down Expand Up @@ -29,7 +30,7 @@ export default class SCLang extends Dryad {

prepareForAdd() {
return {
sclang: () => boot(this.properties.options)
sclang: (context) => boot(_.defaults(this.properties.options, {log: context.log}))
};
}

Expand Down
3 changes: 2 additions & 1 deletion src/dryads/SCServer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import {Dryad} from 'dryadic';
import {boot} from '../server/server';
import * as _ from 'underscore';

const defaultOptions = {
debug: false
Expand All @@ -24,7 +25,7 @@ export default class SCServer extends Dryad {

prepareForAdd() {
return {
scserver: () => boot(this.properties.options),
scserver: (context) => boot(_.defaults(this.properties.options, {log: context.log})),
group: 0
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/dryads/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ export const layer = {
* ...
* player.stop();
*/
export function dryadic(rootDryad, moreLayers=[]) {
return makeDryadPlayer(rootDryad, [layer].concat(moreLayers));
export function dryadic(rootDryad, moreLayers=[], rootContext={}) {
return makeDryadPlayer(rootDryad, [layer].concat(moreLayers), rootContext);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/lang/sclang.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class SCLang extends EventEmitter {
super();
this.options = options || {};
this.process = null;
this.log = new Logger(this.options.debug, this.options.echo);
this.log = new Logger(this.options.debug, this.options.echo, this.options.log);
this.log.dbug(this.options);
this.stateWatcher = this.makeStateWatcher();
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class Server extends EventEmitter {
}

_initLogger() {
this.log = new Logger(this.options.debug, this.options.echo);
this.log = new Logger(this.options.debug, this.options.echo, this.options.log);
this.send.subscribe((event) => {
// will be a type:msg or type:bundle
var out = JSON.stringify(event.payload || event, null, 2);
Expand Down
53 changes: 36 additions & 17 deletions src/utils/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var chalk = require('chalk');

const c = {
const colors = {
debug: 'gray',
error: 'yellow',
stdout: 'green',
Expand All @@ -28,49 +28,54 @@ const c = {

export default class Logger {

constructor(debug, echo) {
/**
* @param {winston.Logger|undefined} log - optional external winston Logger or compatible API
*/
constructor(debug, echo, log) {
this.debug = debug;
this.echo = echo;
this.colorize = typeof log === 'undefined';
this.log = log || console;
this.browser = typeof window !== 'undefined';
}

dbug(text) {
if (this.debug) {
this.print('debug ', text, c.debug);
this.print('debug ', text, colors.debug);
}
}

err(text) {
this.print('error ', text, c.error);
this.print('error ', text, colors.error);
}

stdin(text) {
if (this.echo) {
this.print('stdin ', text, c.stdin);
this.print('stdin ', text, colors.stdin);
}
}

stdout(text) {
if (this.echo) {
this.print('stdout ', text, c.stdout);
this.print('stdout ', text, colors.stdout);
}
}

stderr(text) {
if (this.echo) {
this.print('stderr ', text, c.stderr);
this.print('stderr ', text, colors.stderr);
}
}

sendosc(text) {
if (this.echo) {
this.print('sendosc', text, c.sendosc);
this.print('sendosc', text, colors.sendosc);
}
}

rcvosc(text) {
if (this.echo) {
this.print('rcvosc ', text, c.rcvosc);
this.print('rcvosc ', text, colors.rcvosc);
}
}

Expand All @@ -85,14 +90,28 @@ export default class Logger {
var
lines = text.split('\n'),
clean = [label + ': ' + lines[0]],
rest = lines.slice(1),
colorFn = chalk[color];
rest = rest.filter(function(s) { return s.length > 0; });
rest = rest.map(function(s) {
return ' ' + s;
});
clean = clean.concat(rest);
console.log(colorFn(clean.join('\n')));
rest = lines.slice(1)
.filter((s) => s.length > 0)
.map((s) => ' ' + s);
clean = clean.concat(rest).join('\n');
if (this.colorize) {
clean = chalk[color](clean);
}

switch (label.trim()) {
case 'debug':
case 'stdin':
case 'sendosc':
case 'rcvosc':
this.log.debug(clean);
break;
case 'stderr':
case 'error':
this.log.error(clean);
break;
default:
this.log.info(clean);
}
}
}
}

0 comments on commit 1696372

Please sign in to comment.