@@ -2496,13 +2496,97 @@ Paho.MQTT = (function (global) {
24962496} ) ( window ) ;
24972497
24982498var webduino = webduino || {
2499- version : '0.4.23 '
2499+ version : '0.4.24 '
25002500} ;
25012501
25022502if ( typeof exports !== 'undefined' ) {
25032503 module . exports = webduino ;
25042504}
25052505
2506+ + ( function ( factory ) {
2507+ if ( typeof exports === 'undefined' ) {
2508+ factory ( webduino || { } ) ;
2509+ } else {
2510+ module . exports = factory ;
2511+ }
2512+ } ( function ( scope ) {
2513+ 'use strict' ;
2514+
2515+ var DEBUG_STR = 'DEBUG_WEBDUINOJS' ;
2516+
2517+ function Logger ( option ) {
2518+ if ( ! option ) {
2519+ option = '*' ;
2520+ }
2521+ if ( typeof option === 'string' ) {
2522+ option = { key : option } ;
2523+ }
2524+ this . _option = option ;
2525+ this . _key = option . key ;
2526+ this . _isShow = isShow . bind ( this ) ;
2527+ init . call ( this ) ;
2528+ }
2529+
2530+ function hasLocalStorage ( ) {
2531+ try {
2532+ return ! ! localStorage ;
2533+ } catch ( err ) {
2534+ return false ;
2535+ }
2536+ }
2537+
2538+ function hasProcess ( ) {
2539+ try {
2540+ return ! ! process ;
2541+ } catch ( err ) {
2542+ return false ;
2543+ }
2544+ }
2545+
2546+ function isShow ( ) {
2547+ var reg = new RegExp ( ) ;
2548+ var debugKeys = [ ] ;
2549+ var debugStr ;
2550+
2551+ if ( hasLocalStorage ( ) ) {
2552+ debugStr = localStorage . getItem ( DEBUG_STR ) ;
2553+ }
2554+
2555+ if ( hasProcess ( ) ) {
2556+ debugStr = process . env [ DEBUG_STR ] ;
2557+ }
2558+
2559+ if ( debugStr ) {
2560+ debugKeys = debugStr . split ( ',' ) . map ( function ( val ) {
2561+ return val . trim ( ) ;
2562+ } ) ;
2563+ }
2564+
2565+ if ( debugKeys . indexOf ( '*' ) !== - 1 || debugKeys . indexOf ( this . _key ) !== - 1 ) {
2566+ return true ;
2567+ }
2568+
2569+ return false ;
2570+ }
2571+
2572+ function init ( ) {
2573+ var self = this ;
2574+ var methodNames = [ 'log' , 'info' , 'warn' , 'error' ] ;
2575+ var noop = function ( ) { } ;
2576+ var isCopy = this . _isShow ( ) ;
2577+
2578+ methodNames . forEach ( function ( name ) {
2579+ if ( isCopy ) {
2580+ self [ name ] = Function . prototype . bind . call ( console [ name ] , console ) ;
2581+ } else {
2582+ self [ name ] = noop ;
2583+ }
2584+ } ) ;
2585+ }
2586+
2587+ scope . Logger = Logger ;
2588+ } ) ) ;
2589+
25062590+ ( function ( factory ) {
25072591 if ( typeof exports === 'undefined' ) {
25082592 factory ( webduino || { } ) ;
@@ -3930,6 +4014,7 @@ if (typeof exports !== 'undefined') {
39304014 var EventEmitter = scope . EventEmitter ,
39314015 TransportEvent = scope . TransportEvent ,
39324016 Transport = scope . Transport ,
4017+ Logger = scope . Logger ,
39334018 Pin = scope . Pin ,
39344019 util = scope . util ,
39354020 proto ;
@@ -4012,6 +4097,7 @@ if (typeof exports !== 'undefined') {
40124097 this . _numDigitalPortReportRequests = 0 ;
40134098 this . _transport = null ;
40144099 this . _pinStateEventCenter = new EventEmitter ( ) ;
4100+ this . _logger = new Logger ( 'Board' ) ;
40154101
40164102 this . _initialVersionResultHandler = onInitialVersionResult . bind ( this ) ;
40174103 this . _openHandler = onOpen . bind ( this ) ;
@@ -4044,6 +4130,8 @@ if (typeof exports !== 'undefined') {
40444130
40454131 function onMessage ( data ) {
40464132 try {
4133+ this . _logger . info ( 'onMessage' , data ) ;
4134+
40474135 var len = data . length ;
40484136
40494137 if ( len ) {
@@ -4060,6 +4148,7 @@ if (typeof exports !== 'undefined') {
40604148 }
40614149
40624150 function onError ( error ) {
4151+ this . _logger . warn ( 'onError' , error ) ;
40634152 this . _isReady = false ;
40644153 this . emit ( BoardEvent . ERROR , error ) ;
40654154 setImmediate ( this . disconnect . bind ( this ) ) ;
@@ -4098,10 +4187,6 @@ if (typeof exports !== 'undefined') {
40984187 }
40994188 }
41004189
4101- function debug ( msg ) {
4102- console && console . log ( msg . stack || msg ) ;
4103- }
4104-
41054190 Board . prototype = proto = Object . create ( EventEmitter . prototype , {
41064191
41074192 constructor : {
@@ -4181,6 +4266,7 @@ if (typeof exports !== 'undefined') {
41814266
41824267 switch ( command ) {
41834268 case DIGITAL_MESSAGE :
4269+ this . _logger . info ( 'processMultiByteCommand digital:' , channel , commandData [ 1 ] , commandData [ 2 ] ) ;
41844270 this . processDigitalMessage ( channel , commandData [ 1 ] , commandData [ 2 ] ) ;
41854271 break ;
41864272 case REPORT_VERSION :
@@ -4190,6 +4276,7 @@ if (typeof exports !== 'undefined') {
41904276 } ) ;
41914277 break ;
41924278 case ANALOG_MESSAGE :
4279+ this . _logger . info ( 'processMultiByteCommand analog:' , channel , commandData [ 1 ] , commandData [ 2 ] ) ;
41934280 this . processAnalogMessage ( channel , commandData [ 1 ] , commandData [ 2 ] ) ;
41944281 break ;
41954282 }
@@ -4751,11 +4838,11 @@ if (typeof exports !== 'undefined') {
47514838 resolution ;
47524839
47534840 for ( var i = 0 ; i < len ; i ++ ) {
4754- debug ( ' Pin ' + i + ':' ) ;
4841+ this . _logger . info ( "reportCapabilities, Pin " + i ) ;
47554842 for ( var mode in capabilities [ i ] ) {
47564843 if ( capabilities [ i ] . hasOwnProperty ( mode ) ) {
47574844 resolution = capabilities [ i ] [ mode ] ;
4758- debug ( '\t' + mode + ' (' + resolution + ( resolution > 1 ? ' bits)' : ' bit)' ) ) ;
4845+ this . _logger . info ( "reportCapabilities" , '\t' + mode + ' (' + resolution + ( resolution > 1 ? ' bits)' : ' bit)' ) ) ;
47594846 }
47604847 }
47614848 }
0 commit comments