Make sure the library can compile with json logging in trace mode

This commit is contained in:
Zahary Karadjov 2020-02-04 15:17:24 +01:00
parent 691efaa8a1
commit 7bd305471c
No known key found for this signature in database
GPG Key ID: C8936F8A3073D609
8 changed files with 27 additions and 26 deletions

View File

@ -48,13 +48,13 @@ proc readMsg*(conn: Connection): Future[Option[Msg]] {.async, gcsafe.} =
if headerVarint.isNone:
return
trace "read header varint", varint = headerVarint
trace "read header varint", varint = $headerVarint
let dataLenVarint = await conn.readMplexVarint()
var data: seq[byte]
if dataLenVarint.isSome and dataLenVarint.get() > 0.uint:
data = await conn.read(dataLenVarint.get().int)
trace "read size varint", varint = dataLenVarint
trace "read size varint", varint = $dataLenVarint
let header = headerVarint.get()
result = some((header shr 3, MessageType(header and 0x7), data))

View File

@ -69,7 +69,7 @@ proc decodeMsg*(buf: seq[byte]): IdentifyInfo =
result.pubKey = none(PublicKey)
var pubKey: PublicKey
if pb.getValue(1, pubKey) > 0:
trace "read public key from message", pubKey = pubKey
trace "read public key from message", pubKey = $pubKey
result.pubKey = some(pubKey)
result.addrs = newSeq[MultiAddress]()

View File

@ -70,9 +70,9 @@ method rpcHandler*(p: PubSub,
peer: PubSubPeer,
rpcMsgs: seq[RPCMsg]) {.async, base.} =
## handle rpc messages
trace "processing RPC message", peer = peer.id, msg = rpcMsgs
trace "processing RPC message", peer = peer.id, msg = $rpcMsgs
for m in rpcMsgs: # for all RPC messages
trace "processing messages", msg = rpcMsgs
trace "processing messages", msg = $rpcMsgs
if m.subscriptions.len > 0: # if there are any subscriptions
for s in m.subscriptions: # subscribe/unsubscribe the peer for each topic
p.subscribeTopic(s.topic, s.subscribe, peer.id)

View File

@ -58,7 +58,7 @@ proc handle*(p: PubSubPeer, conn: Connection) {.async.} =
continue
let msg = decodeRpcMsg(data)
trace "decoded msg from peer", peer = p.id, msg = msg
trace "decoded msg from peer", peer = p.id, msg = $msg
await p.handler(p, @[msg])
p.recvdRpcCache.put($hexData.hash)
except CatchableError as exc:

View File

@ -213,7 +213,7 @@ proc decodeMessages*(pb: var ProtoBuffer): seq[Message] {.gcsafe.} =
proc encodeRpcMsg*(msg: RPCMsg): ProtoBuffer {.gcsafe.} =
result = initProtoBuffer()
trace "encoding msg: ", msg = msg
trace "encoding msg: ", msg = $msg
if msg.subscriptions.len > 0:
var subs = initProtoBuffer()

View File

@ -36,7 +36,7 @@ proc put*[V](t: TimedCache[V],
key: string,
val: V = "",
timeout: Duration,
handler: ExpireHandler[V] = nil) =
handler: ExpireHandler[V] = nil) =
trace "adding entry to timed cache", key = key
t.cache[key] = TimedEntry[V](val: val, handler: handler)
@ -57,7 +57,7 @@ proc put*[V](t: TimedCache[V],
handler: ExpireHandler[V] = nil) =
t.put(key, val, t.timeout, handler)
proc contains*[V](t: TimedCache[V], key: string): bool =
proc contains*[V](t: TimedCache[V], key: string): bool =
t.cache.contains(key)
proc del*[V](t: TimedCache[V], key: string) =

View File

@ -268,19 +268,19 @@ proc transactMessage(conn: Connection,
if length <= SecioMaxMessageSize:
buf.setLen(length)
await conn.readExactly(addr buf[0], length)
trace "Received message body", conn = conn,
trace "Received message body", conn = $conn,
length = length,
buff = buf
result = buf
else:
trace "Received size of message exceed limits", conn = conn,
trace "Received size of message exceed limits", conn = $conn,
length = length
except AsyncStreamIncompleteError:
trace "Connection dropped while reading", conn = conn
trace "Connection dropped while reading", conn = $conn
except AsyncStreamReadError:
trace "Error reading from connection", conn = conn
trace "Error reading from connection", conn = $conn
except AsyncStreamWriteError:
trace "Could not write to connection", conn = conn
trace "Could not write to connection", conn = $conn
proc handshake(s: Secio, conn: Connection): Future[SecureConnection] {.async.} =
var
@ -310,7 +310,8 @@ proc handshake(s: Secio, conn: Connection): Future[SecureConnection] {.async.} =
localPeerId = PeerID.init(s.localPublicKey)
trace "Local proposal", schemes = SecioExchanges, ciphers = SecioCiphers,
trace "Local proposal", schemes = SecioExchanges,
ciphers = SecioCiphers,
hashes = SecioHashes,
pubkey = toHex(localBytesPubkey),
peer = localPeerId
@ -318,12 +319,12 @@ proc handshake(s: Secio, conn: Connection): Future[SecureConnection] {.async.} =
var answer = await transactMessage(conn, request)
if len(answer) == 0:
trace "Proposal exchange failed", conn = conn
trace "Proposal exchange failed", conn = $conn
raise newException(SecioError, "Proposal exchange failed")
if not decodeProposal(answer, remoteNonce, remoteBytesPubkey, remoteExchanges,
remoteCiphers, remoteHashes):
trace "Remote proposal decoding failed", conn = conn
trace "Remote proposal decoding failed", conn = $conn
raise newException(SecioError, "Remote proposal decoding failed")
if not remotePubkey.init(remoteBytesPubkey):
@ -359,11 +360,11 @@ proc handshake(s: Secio, conn: Connection): Future[SecureConnection] {.async.} =
var localExchange = createExchange(epubkey, signature.getBytes())
var remoteExchange = await transactMessage(conn, localExchange)
if len(remoteExchange) == 0:
trace "Corpus exchange failed", conn = conn
trace "Corpus exchange failed", conn = $conn
raise newException(SecioError, "Corpus exchange failed")
if not decodeExchange(remoteExchange, remoteEBytesPubkey, remoteEBytesSig):
trace "Remote exchange decoding failed", conn = conn
trace "Remote exchange decoding failed", conn = $conn
raise newException(SecioError, "Remote exchange decoding failed")
if not remoteESignature.init(remoteEBytesSig):
@ -372,10 +373,10 @@ proc handshake(s: Secio, conn: Connection): Future[SecureConnection] {.async.} =
var remoteCorpus = answer & request[4..^1] & remoteEBytesPubkey
if not remoteESignature.verify(remoteCorpus, remotePubkey):
trace "Signature verification failed", scheme = remotePubkey.scheme,
signature = remoteESignature,
pubkey = remotePubkey,
corpus = remoteCorpus
trace "Signature verification failed", scheme = $remotePubkey.scheme,
signature = $remoteESignature,
pubkey = $remotePubkey,
corpus = $remoteCorpus
raise newException(SecioError, "Signature verification failed")
trace "Signature verified", scheme = remotePubkey.scheme

View File

@ -124,7 +124,7 @@ proc mux(s: Switch, conn: Connection): Future[void] {.async, gcsafe.} =
await stream.close() # close identify stream
trace "connection's peerInfo", peerInfo = conn.peerInfo
trace "connection's peerInfo", peerInfo = $conn.peerInfo
# store it in muxed connections if we have a peer for it
if not isNil(conn.peerInfo):
@ -159,7 +159,7 @@ proc getMuxedStream(s: Switch, peerInfo: PeerInfo): Future[Connection] {.async,
result = conn
proc upgradeOutgoing(s: Switch, conn: Connection): Future[Connection] {.async, gcsafe.} =
trace "handling connection", conn = conn
trace "handling connection", conn = $conn
result = conn
# don't mux/secure twise
@ -350,7 +350,7 @@ proc newSwitch*(peerInfo: PeerInfo,
let s = result # can't capture result
result.streamHandler = proc(stream: Connection) {.async, gcsafe.} =
trace "handling connection for", peerInfo = stream.peerInfo
trace "handling connection for", peerInfo = $stream.peerInfo
await s.ms.handle(stream) # handle incoming connection
result.mount(identity)