added logging

master
David Kebler 2018-03-02 08:35:25 -08:00
parent 2e07dab28d
commit df9a879fef
2 changed files with 31 additions and 14 deletions

View File

@ -1,19 +1,31 @@
import UCISocket from '@uci/socket' import UCISocket from '@uci/socket'
// import UCISocket from '../../uci-socket/src'
import EventEmitter from 'events' import EventEmitter from 'events'
import { bindFuncs } from '@uci/utils/src/function' import { bindFuncs } from '@uci/utils/src/function'
import { processor, commands, namespaces } from './processing.mjs' import { processor, commands, namespaces } from './processing.mjs'
import logger from '@uci/logger'
let log = {}
const LOG_OPTS = {
repo:'uci-base',
npm:'@uci/base',
file:'src/base.mjs',
class:'Base',
id:this.id,
instance_created:new Date().getTime()
}
export default class Base extends EventEmitter { export default class Base extends EventEmitter {
constructor(opts={}) { constructor(opts={}) {
super() super()
log = logger.child(LOG_OPTS)
this.id = opts.id || opts.name || 'uci-base:'+ new Date().getTime() this.id = opts.id || opts.name || 'uci-base:'+ new Date().getTime()
this.desc = opts.desc // additional details for humans this.desc = opts.desc // additional details for humans
this.socket={} this.socket={}
this._processors = { _default: processor } this._processors = { _default: processor }
this._defaultCmds = commands this._defaultCmds = commands
this._namespaces = namespaces this._namespaces = namespaces
if(opts.sockets) {
opts.sockets.split(/[,|\s]+/).forEach( socketStr => { opts.sockets.split(/[,|\s]+/).forEach( socketStr => {
let socket = {} let socket = {}
socketStr.split(/[>#]+/).map(function(prop,index) { socketStr.split(/[>#]+/).map(function(prop,index) {
@ -28,6 +40,7 @@ export default class Base extends EventEmitter {
Object.assign(this.socket[socket.name],socket) // copy socket info props to new socket Object.assign(this.socket[socket.name],socket) // copy socket info props to new socket
this.socket[socket.name]._packetProcess = this._packetProcess.bind(this,socket.name) this.socket[socket.name]._packetProcess = this._packetProcess.bind(this,socket.name)
}) })
} else log.warn({opts:opts},'no sockets requested for creations -- on standard emitter')
this.bindFuncs = bindFuncs this.bindFuncs = bindFuncs
// console.log('base opts', opts) // console.log('base opts', opts)
@ -51,6 +64,8 @@ export default class Base extends EventEmitter {
async end(name) {} // TODO end all or named sockets async end(name) {} // TODO end all or named sockets
async addSocket(options) {} //TODO add ability to add another socket at runtime and initialize
async send (name,packet) { async send (name,packet) {
if (typeof name !== 'string') { if (typeof name !== 'string') {
packet = name packet = name

View File

@ -59,12 +59,14 @@ const commands ={
}, },
c:{ c:{
error: function (packet) { error: function (packet) {
// TODO log and make this show only on env debug
console.log('==============Packet ERROR [consumer]==========') console.log('==============Packet ERROR [consumer]==========')
console.log(packet.error ) console.log(packet.error )
console.dir(packet.packet) console.dir(packet.packet)
console.log('===========================') console.log('===========================')
}, },
reply: function(packet) { reply: function(packet) {
// TODO log and make this show only on env debug
console.log('==============Packet returned from socket - default reply==========') console.log('==============Packet returned from socket - default reply==========')
console.dir(packet) console.dir(packet)
console.log('===========================') console.log('===========================')