github.com/aergoio/aergo@v1.3.1/p2p/p2putil/loggingutil.go (about) 1 /* 2 * @file 3 * @copyright defined in aergo/LICENSE.txt 4 */ 5 6 package p2putil 7 8 import ( 9 "fmt" 10 "github.com/aergoio/aergo-lib/log" 11 "github.com/aergoio/aergo/p2p/p2pcommon" 12 "github.com/rs/zerolog" 13 ) 14 15 16 // Deprecated 17 func DebugLogReceiveMsg(logger *log.Logger, protocol p2pcommon.SubProtocol, msgID string, peer p2pcommon.RemotePeer, additional interface{}) { 18 if additional != nil { 19 logger.Debug().Str(LogProtoID, protocol.String()).Str(LogMsgID, msgID).Str("from_peer", peer.Name()).Str("other", fmt.Sprint(additional)). 20 Msg("Received a message") 21 } else { 22 logger.Debug().Str(LogProtoID, protocol.String()).Str(LogMsgID, msgID).Str("from_peer", peer.Name()). 23 Msg("Received a message") 24 } 25 } 26 27 // Deprecated 28 func DebugLogReceiveResponseMsg(logger *log.Logger, protocol p2pcommon.SubProtocol, msgID string, reqID string, peer p2pcommon.RemotePeer, additional interface{}) { 29 if additional != nil { 30 logger.Debug().Str(LogProtoID, protocol.String()).Str(LogMsgID, msgID).Str(LogOrgReqID, reqID).Str("from_peer", peer.Name()).Str("other", fmt.Sprint(additional)). 31 Msg("Received a response message") 32 } else { 33 logger.Debug().Str(LogProtoID, protocol.String()).Str(LogMsgID, msgID).Str(LogOrgReqID, reqID).Str("from_peer", peer.Name()). 34 Msg("Received a response message") 35 } 36 } 37 38 func DebugLogReceive(logger *log.Logger, protocol p2pcommon.SubProtocol, msgID string, peer p2pcommon.RemotePeer, additional zerolog.LogObjectMarshaler) { 39 if additional != nil { 40 logger.Debug().Str(LogProtoID, protocol.String()).Str(LogMsgID, msgID).Str("from_peer", peer.Name()).Object("body", additional).Msg("Received a message") 41 } else { 42 logger.Debug().Str(LogProtoID, protocol.String()).Str(LogMsgID, msgID).Str("from_peer", peer.Name()). 43 Msg("Received a message") 44 } 45 } 46 47 func DebugLogReceiveResponse(logger *log.Logger, protocol p2pcommon.SubProtocol, msgID string, reqID string, peer p2pcommon.RemotePeer, additional zerolog.LogObjectMarshaler) { 48 if additional != nil { 49 logger.Debug().Str(LogProtoID, protocol.String()).Str(LogMsgID, msgID).Str(LogOrgReqID, reqID).Str("from_peer", peer.Name()).Object("body", additional). 50 Msg("Received a response message") 51 } else { 52 logger.Debug().Str(LogProtoID, protocol.String()).Str(LogMsgID, msgID).Str(LogOrgReqID, reqID).Str("from_peer", peer.Name()). 53 Msg("Received a response message") 54 } 55 } 56 57 type LogStringersMarshaller struct { 58 arr []fmt.Stringer 59 limit int 60 } 61 62 func NewLogStringersMarshaller(arr []fmt.Stringer, limit int) *LogStringersMarshaller { 63 return &LogStringersMarshaller{arr: arr, limit: limit} 64 } 65 66 func (m *LogStringersMarshaller) MarshalZerologArray(a *zerolog.Array) { 67 size := len(m.arr) 68 if size > m.limit { 69 for i := 0; i < m.limit-1; i++ { 70 a.Str(m.arr[i].String()) 71 } 72 a.Str(fmt.Sprintf("(and %d more)", size-m.limit+1)) 73 } else { 74 for _, element := range m.arr { 75 a.Str(element.String()) 76 } 77 } 78 } 79 80 type LogPeerMetasMarshaller struct { 81 metas []p2pcommon.PeerMeta 82 limit int 83 } 84 85 func NewLogPeerMetasMarshaller(metas []p2pcommon.PeerMeta, limit int) *LogPeerMetasMarshaller { 86 return &LogPeerMetasMarshaller{metas: metas, limit: limit} 87 } 88 89 func (m *LogPeerMetasMarshaller) MarshalZerologArray(a *zerolog.Array) { 90 size := len(m.metas) 91 if size > m.limit { 92 for i := 0; i < m.limit-1; i++ { 93 a.Str(ShortMetaForm(m.metas[i])) 94 } 95 a.Str(fmt.Sprintf("(and %d more)", size-m.limit+1)) 96 } else { 97 for _, meta := range m.metas { 98 a.Str(ShortMetaForm(meta)) 99 } 100 } 101 } 102 103 104 type LogPeersMarshaller struct { 105 metas []p2pcommon.RemotePeer 106 limit int 107 } 108 109 func NewLogPeersMarshaller(metas []p2pcommon.RemotePeer, limit int) *LogPeersMarshaller { 110 return &LogPeersMarshaller{metas: metas, limit: limit} 111 } 112 113 func (m *LogPeersMarshaller) MarshalZerologArray(a *zerolog.Array) { 114 size := len(m.metas) 115 if size > m.limit { 116 for i := 0; i < m.limit-1; i++ { 117 a.Str(m.metas[i].Name()) 118 } 119 a.Str(fmt.Sprintf("(and %d more)", size-m.limit+1)) 120 } else { 121 for _, meta := range m.metas { 122 a.Str(meta.Name()) 123 } 124 } 125 } 126 127 128 type LogStringsMarshaller struct { 129 strs []string 130 limit int 131 } 132 133 func NewLogStringsMarshaller(strs []string, limit int) *LogStringsMarshaller { 134 return &LogStringsMarshaller{strs: strs, limit: limit} 135 } 136 137 func (m *LogStringsMarshaller) MarshalZerologArray(a *zerolog.Array) { 138 size := len(m.strs) 139 if size > m.limit { 140 for i := 0; i < m.limit-1; i++ { 141 a.Str(m.strs[i]) 142 } 143 a.Str(fmt.Sprintf("(and %d more)", size-m.limit+1)) 144 } else { 145 for _, meta := range m.strs { 146 a.Str(meta) 147 } 148 } 149 }