2019-03-11 09:22:06 +00:00
|
|
|
const tracingEnabled = defined(p2pdump)
|
2019-02-05 15:40:29 +00:00
|
|
|
|
|
|
|
when tracingEnabled:
|
|
|
|
import
|
2019-03-11 09:22:06 +00:00
|
|
|
macros, typetraits,
|
2019-02-05 15:40:29 +00:00
|
|
|
serialization, json_serialization/writer,
|
|
|
|
chronicles, chronicles_tail/configuration
|
|
|
|
|
|
|
|
export
|
|
|
|
# XXX: Nim visibility rules get in the way here.
|
|
|
|
# It would be nice if the users of this module don't have to
|
|
|
|
# import json_serializer, but this won't work at the moment,
|
|
|
|
# because the `encode` call inside `logMsgEvent` has its symbols
|
|
|
|
# mixed in from the module where `logMsgEvent` is called
|
|
|
|
# (instead of from this module, which will be more logical).
|
|
|
|
init, writeValue, getOutput
|
|
|
|
# TODO: File this as an issue
|
|
|
|
|
|
|
|
logStream p2pMessages[json[file(p2p_messages.json,truncate)]]
|
|
|
|
p2pMessages.useTailPlugin "p2p_tracing_ctail_plugin.nim"
|
|
|
|
|
|
|
|
template logRecord(eventName: static[string], args: varargs[untyped]) =
|
|
|
|
p2pMessages.log LogLevel.NONE, eventName, topics = "p2pdump", args
|
|
|
|
|
2019-03-11 09:22:06 +00:00
|
|
|
proc initTracing(baseProtocol: ProtocolInfo,
|
|
|
|
userProtocols: seq[ProtocolInfo]) =
|
2019-02-05 15:40:29 +00:00
|
|
|
once:
|
|
|
|
var w = init StringJsonWriter
|
|
|
|
|
|
|
|
proc addProtocol(p: ProtocolInfo) =
|
|
|
|
w.writeFieldName p.name
|
|
|
|
w.beginRecord()
|
|
|
|
for msg in p.messages:
|
|
|
|
w.writeField $msg.id, msg.name
|
|
|
|
w.endRecordField()
|
|
|
|
|
|
|
|
w.beginRecord()
|
|
|
|
addProtocol baseProtocol
|
|
|
|
for userProtocol in userProtocols:
|
|
|
|
addProtocol userProtocol
|
|
|
|
w.endRecord()
|
|
|
|
|
|
|
|
logRecord "p2p_protocols", data = JsonString(w.getOutput)
|
|
|
|
|
|
|
|
proc logMsgEventImpl(eventName: static[string],
|
|
|
|
peer: Peer,
|
|
|
|
protocol: ProtocolInfo,
|
2019-03-11 09:22:06 +00:00
|
|
|
msgName: string,
|
2019-02-05 15:40:29 +00:00
|
|
|
json: string) =
|
|
|
|
# this is kept as a separate proc to reduce the code bloat
|
|
|
|
logRecord eventName, port = int(peer.network.address.tcpPort),
|
|
|
|
peer = $peer.remote,
|
|
|
|
protocol = protocol.name,
|
2019-03-11 09:22:06 +00:00
|
|
|
msg = msgName,
|
|
|
|
data = JsonString(json)
|
2019-02-05 15:40:29 +00:00
|
|
|
|
|
|
|
proc logMsgEvent[Msg](eventName: static[string], peer: Peer, msg: Msg) =
|
|
|
|
mixin msgProtocol, protocolInfo, msgId
|
|
|
|
|
|
|
|
logMsgEventImpl(eventName, peer,
|
|
|
|
Msg.msgProtocol.protocolInfo,
|
2019-03-11 09:22:06 +00:00
|
|
|
Msg.type.name,
|
2019-02-05 15:40:29 +00:00
|
|
|
StringJsonWriter.encode(msg))
|
|
|
|
|
2019-03-11 09:22:06 +00:00
|
|
|
proc logSentMsgFields(peer: NimNode,
|
|
|
|
protocolInfo: NimNode,
|
|
|
|
msgName: string,
|
|
|
|
fields: openarray[NimNode]): NimNode =
|
2019-02-05 15:40:29 +00:00
|
|
|
## This generates the tracing code inserted in the message sending procs
|
|
|
|
## `fields` contains all the params that were serialized in the message
|
|
|
|
var tracer = ident("tracer")
|
|
|
|
|
|
|
|
result = quote do:
|
|
|
|
var `tracer` = init StringJsonWriter
|
|
|
|
beginRecord(`tracer`)
|
|
|
|
|
|
|
|
for f in fields:
|
|
|
|
result.add newCall(bindSym"writeField", tracer, newLit($f), f)
|
|
|
|
|
|
|
|
result.add quote do:
|
|
|
|
endRecord(`tracer`)
|
|
|
|
logMsgEventImpl("outgoing_msg", `peer`,
|
2019-03-11 09:22:06 +00:00
|
|
|
`protocolInfo`, `msgName`, getOutput(`tracer`))
|
2019-02-05 15:40:29 +00:00
|
|
|
|
2019-03-11 09:22:06 +00:00
|
|
|
template logSentMsg(peer: Peer, msg: auto) =
|
2019-02-05 15:40:29 +00:00
|
|
|
logMsgEvent("outgoing_msg", peer, msg)
|
|
|
|
|
2019-03-11 09:22:06 +00:00
|
|
|
template logReceivedMsg(peer: Peer, msg: auto) =
|
2019-02-05 15:40:29 +00:00
|
|
|
logMsgEvent("incoming_msg", peer, msg)
|
|
|
|
|
2019-03-11 09:22:06 +00:00
|
|
|
template logConnectedPeer(p: Peer) =
|
2019-02-05 15:40:29 +00:00
|
|
|
logRecord "peer_connected",
|
|
|
|
port = int(p.network.address.tcpPort),
|
|
|
|
peer = $p.remote
|
|
|
|
|
2019-03-11 09:22:06 +00:00
|
|
|
template logAcceptedPeer(p: Peer) =
|
2019-02-05 15:40:29 +00:00
|
|
|
logRecord "peer_accepted",
|
|
|
|
port = int(p.network.address.tcpPort),
|
|
|
|
peer = $p.remote
|
|
|
|
|
2019-03-11 09:22:06 +00:00
|
|
|
template logDisconnectedPeer(p: Peer) =
|
2019-02-05 15:40:29 +00:00
|
|
|
logRecord "peer_disconnected",
|
|
|
|
port = int(p.network.address.tcpPort),
|
|
|
|
peer = $p.remote
|
|
|
|
|
|
|
|
else:
|
2019-03-11 09:22:06 +00:00
|
|
|
template initTracing(baseProtocol: ProtocolInfo,
|
2019-02-05 15:40:29 +00:00
|
|
|
userProtocols: seq[ProtocolInfo])= discard
|
2019-03-11 09:22:06 +00:00
|
|
|
template logSentMsg(peer: Peer, msg: auto) = discard
|
|
|
|
template logReceivedMsg(peer: Peer, msg: auto) = discard
|
|
|
|
template logConnectedPeer(peer: Peer) = discard
|
|
|
|
template logAcceptedPeer(peer: Peer) = discard
|
|
|
|
template logDisconnectedPeer(peer: Peer) = discard
|
2019-02-05 15:40:29 +00:00
|
|
|
|