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