add a logScope to make tracing less ugly (#218)

* add a logScope to make tracing less ugly

* don't crash on nil pointer
This commit is contained in:
Dmitriy Ryajov 2020-06-15 12:10:41 -06:00 committed by GitHub
parent 85b56d0b3a
commit 9cd47fe816
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 37 additions and 41 deletions

View File

@ -81,21 +81,29 @@ method handle*(m: Mplex) {.async, gcsafe.} =
let channels = m.getChannelList(initiator)
if id notin channels:
trace "Channel not found, skipping", id = id,
initiator = initiator,
msg = msgType,
oid = m.oid
initiator = initiator,
msg = msgType,
oid = m.oid
continue
channel = channels[id]
logScope:
id = id
initiator = initiator
msgType = msgType
size = data.len
oid = m.oid
case msgType:
of MessageType.New:
let name = string.fromBytes(data)
channel = await m.newStreamInternal(false, id, name)
trace "created channel", id = id,
name = name,
inititator = channel.initiator,
channoid = channel.oid,
oid = m.oid
logScope:
name = channel.name
chann_iod = channel.oid
trace "created channel"
if not isNil(m.streamHandler):
let stream = newConnection(channel)
m.conns.add(stream)
@ -106,6 +114,8 @@ method handle*(m: Mplex) {.async, gcsafe.} =
proc handler() {.async.} =
try:
await m.streamHandler(stream)
trace "finished handling stream"
# doAssert(stream.closed, "connection not closed by handler!")
except CatchableError as exc:
trace "exception in stream handler", exc = exc.msg
finally:
@ -115,53 +125,39 @@ method handle*(m: Mplex) {.async, gcsafe.} =
fut = handler()
of MessageType.MsgIn, MessageType.MsgOut:
trace "pushing data to channel", id = id,
initiator = initiator,
msgType = msgType,
size = data.len,
name = channel.name,
channoid = channel.oid,
oid = m.oid
logScope:
name = channel.name
chann_iod = channel.oid
trace "pushing data to channel"
if data.len > MaxMsgSize:
raise newLPStreamLimitError()
await channel.pushTo(data)
of MessageType.CloseIn, MessageType.CloseOut:
trace "closing channel", id = id,
initiator = initiator,
msgType = msgType,
name = channel.name,
channoid = channel.oid,
oid = m.oid
logScope:
name = channel.name
chann_iod = channel.oid
trace "closing channel"
await channel.closeRemote()
m.getChannelList(initiator).del(id)
trace "deleted channel", id = id,
initiator = initiator,
msgType = msgType,
name = channel.name,
channoid = channel.oid,
oid = m.oid
trace "deleted channel"
of MessageType.ResetIn, MessageType.ResetOut:
trace "resetting channel", id = id,
initiator = initiator,
msgType = msgType,
name = channel.name,
channoid = channel.oid,
oid = m.oid
logScope:
name = channel.name
chann_iod = channel.oid
trace "resetting channel"
await channel.reset()
m.getChannelList(initiator).del(id)
trace "deleted channel", id = id,
initiator = initiator,
msgType = msgType,
name = channel.name,
channoid = channel.oid,
oid = m.oid
trace "deleted channel"
break
finally:
trace "stopping mplex main loop", oid = m.oid
await m.close()
trace "stopping mplex main loop", oid = m.oid
await m.close()
except CatchableError as exc:
trace "Exception occurred", exception = exc.msg, oid = m.oid