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,33 +1,46 @@
import UCISocket from '@uci/socket'
// import UCISocket from '../../uci-socket/src'
import EventEmitter from 'events'
import { bindFuncs } from '@uci/utils/src/function'
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 {
constructor(opts={}) {
super()
log = logger.child(LOG_OPTS)
this.id = opts.id || opts.name || 'uci-base:'+ new Date().getTime()
this.desc = opts.desc // additional details for humans
this.socket={}
this._processors = { _default: processor }
this._defaultCmds = commands
this._namespaces = namespaces
opts.sockets.split(/[,|\s]+/).forEach( socketStr => {
let socket = {}
socketStr.split(/[>#]+/).map(function(prop,index) {
socket[SOCKET_INFO_KEYS[index]] = prop
if(opts.sockets) {
opts.sockets.split(/[,|\s]+/).forEach( socketStr => {
let socket = {}
socketStr.split(/[>#]+/).map(function(prop,index) {
socket[SOCKET_INFO_KEYS[index]] = prop
})
if (!opts[socket.name]) opts[socket.name] = {}
if (socket.transport ==='n') opts[socket.name].np = true
opts[socket.name].id = this.id +':'+ socket.name
// console.log(TRANSLATIONS[socket.type])
this.socket[socket.name] = new UCISocket[TRANSLATE[socket.type]](opts[socket.name])
// console.log(socket.name, this.socket[socket.name].send)
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)
})
if (!opts[socket.name]) opts[socket.name] = {}
if (socket.transport ==='n') opts[socket.name].np = true
opts[socket.name].id = this.id +':'+ socket.name
// console.log(TRANSLATIONS[socket.type])
this.socket[socket.name] = new UCISocket[TRANSLATE[socket.type]](opts[socket.name])
// console.log(socket.name, this.socket[socket.name].send)
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)
})
} else log.warn({opts:opts},'no sockets requested for creations -- on standard emitter')
this.bindFuncs = bindFuncs
// 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 addSocket(options) {} //TODO add ability to add another socket at runtime and initialize
async send (name,packet) {
if (typeof name !== 'string') {
packet = name

View File

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