Skip to content

Commit 693cac8

Browse files
committed
Merge branch 'logger'
2 parents 7f914e2 + e2f400f commit 693cac8

File tree

4 files changed

+94
-6
lines changed

4 files changed

+94
-6
lines changed

gulpfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const base = [
1414
'../setimmediate/setImmediate.js',
1515
'../paho/src/mqttws31.js',
1616
'src/webduino.js',
17+
'src/core/Logger.js',
1718
'src/core/EventEmitter.js',
1819
'src/core/util.js',
1920
'src/util/promisify.js',

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var webduino = require('./src/webduino');
33
require('setimmediate');
44

55
require('./src/core/EventEmitter')(webduino);
6+
require('./src/core/Logger')(webduino);
67
require('./src/core/util')(webduino);
78
require('./src/util/promisify')(webduino);
89
require('./src/core/Transport')(webduino);

src/core/Board.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
var EventEmitter = scope.EventEmitter,
1313
TransportEvent = scope.TransportEvent,
1414
Transport = scope.Transport,
15+
Logger = scope.Logger,
1516
Pin = scope.Pin,
1617
util = scope.util,
1718
proto;
@@ -94,6 +95,7 @@
9495
this._numDigitalPortReportRequests = 0;
9596
this._transport = null;
9697
this._pinStateEventCenter = new EventEmitter();
98+
this._logger = new Logger('Board');
9799

98100
this._initialVersionResultHandler = onInitialVersionResult.bind(this);
99101
this._openHandler = onOpen.bind(this);
@@ -126,6 +128,8 @@
126128

127129
function onMessage(data) {
128130
try {
131+
this._logger.info('onMessage', data);
132+
129133
var len = data.length;
130134

131135
if (len) {
@@ -142,6 +146,7 @@
142146
}
143147

144148
function onError(error) {
149+
this._logger.warn('onError', error);
145150
this._isReady = false;
146151
this.emit(BoardEvent.ERROR, error);
147152
setImmediate(this.disconnect.bind(this));
@@ -180,10 +185,6 @@
180185
}
181186
}
182187

183-
function debug(msg) {
184-
console && console.log(msg.stack || msg);
185-
}
186-
187188
Board.prototype = proto = Object.create(EventEmitter.prototype, {
188189

189190
constructor: {
@@ -263,6 +264,7 @@
263264

264265
switch (command) {
265266
case DIGITAL_MESSAGE:
267+
this._logger.info('processMultiByteCommand digital:', channel, commandData[1], commandData[2]);
266268
this.processDigitalMessage(channel, commandData[1], commandData[2]);
267269
break;
268270
case REPORT_VERSION:
@@ -272,6 +274,7 @@
272274
});
273275
break;
274276
case ANALOG_MESSAGE:
277+
this._logger.info('processMultiByteCommand analog:', channel, commandData[1], commandData[2]);
275278
this.processAnalogMessage(channel, commandData[1], commandData[2]);
276279
break;
277280
}
@@ -833,11 +836,11 @@
833836
resolution;
834837

835838
for (var i = 0; i < len; i++) {
836-
debug('Pin ' + i + ':');
839+
this._logger.info("reportCapabilities, Pin " + i);
837840
for (var mode in capabilities[i]) {
838841
if (capabilities[i].hasOwnProperty(mode)) {
839842
resolution = capabilities[i][mode];
840-
debug('\t' + mode + ' (' + resolution + (resolution > 1 ? ' bits)' : ' bit)'));
843+
this._logger.info("reportCapabilities", '\t' + mode + ' (' + resolution + (resolution > 1 ? ' bits)' : ' bit)'));
841844
}
842845
}
843846
}

src/core/Logger.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
+(function (factory) {
2+
if (typeof exports === 'undefined') {
3+
factory(webduino || {});
4+
} else {
5+
module.exports = factory;
6+
}
7+
}(function (scope) {
8+
'use strict';
9+
10+
var DEBUG_STR = 'DEBUG_WEBDUINOJS';
11+
12+
function Logger(option) {
13+
if (!option) {
14+
option = '*';
15+
}
16+
if (typeof option === 'string') {
17+
option = { key: option };
18+
}
19+
this._option = option;
20+
this._key = option.key;
21+
this._isShow = isShow.bind(this);
22+
init.call(this);
23+
}
24+
25+
function hasLocalStorage() {
26+
try {
27+
return !!localStorage;
28+
} catch (err) {
29+
return false;
30+
}
31+
}
32+
33+
function hasProcess() {
34+
try {
35+
return !!process;
36+
} catch (err) {
37+
return false;
38+
}
39+
}
40+
41+
function isShow() {
42+
var reg = new RegExp();
43+
var debugKeys = [];
44+
var debugStr;
45+
46+
if (hasLocalStorage()) {
47+
debugStr = localStorage.getItem(DEBUG_STR);
48+
}
49+
50+
if (hasProcess()) {
51+
debugStr = process.env[DEBUG_STR];
52+
}
53+
54+
if (debugStr) {
55+
debugKeys = debugStr.split(',').map(function (val) {
56+
return val.trim();
57+
});
58+
}
59+
60+
if (debugKeys.indexOf('*') !== -1 || debugKeys.indexOf(this._key) !== -1) {
61+
return true;
62+
}
63+
64+
return false;
65+
}
66+
67+
function init() {
68+
var self = this;
69+
var methodNames = ['log', 'info', 'warn', 'error'];
70+
var noop = function () { };
71+
var isCopy = this._isShow();
72+
73+
methodNames.forEach(function (name) {
74+
if (isCopy) {
75+
self[name] = Function.prototype.bind.call(console[name], console);
76+
} else {
77+
self[name] = noop;
78+
}
79+
});
80+
}
81+
82+
scope.Logger = Logger;
83+
}));

0 commit comments

Comments
 (0)