github.com/aergoio/aergo@v1.3.1/types/p2plogging.go (about) 1 /* 2 * @file 3 * @copyright defined in aergo/LICENSE.txt 4 */ 5 6 package types 7 8 import ( 9 "fmt" 10 "github.com/aergoio/aergo/internal/enc" 11 "github.com/rs/zerolog" 12 ) 13 14 const ( 15 LogChainID = "chain_id" 16 LogBlkHash = "blk_hash" 17 LogBlkNo = "blk_no" 18 LogRespStatus = "status" 19 LogHasNext = "has_next" 20 ) 21 22 // LogB58EncMarshaller is zerolog array marshaller which print bytes array to base58 encoded string. 23 type LogB58EncMarshaller struct { 24 arr [][]byte 25 limit int 26 } 27 28 func NewLogB58EncMarshaller(arr [][]byte, limit int) *LogB58EncMarshaller { 29 return &LogB58EncMarshaller{arr: arr, limit: limit} 30 } 31 32 func (m LogB58EncMarshaller) MarshalZerologArray(a *zerolog.Array) { 33 size := len(m.arr) 34 if size > m.limit { 35 for i := 0; i < m.limit-1; i++ { 36 a.Str(enc.ToString(m.arr[i])) 37 } 38 a.Str(fmt.Sprintf("(and %d more)", size-m.limit+1)) 39 } else { 40 for _, element := range m.arr { 41 a.Str(enc.ToString(element)) 42 } 43 } 44 } 45 46 type LogBlockHashMarshaller struct { 47 arr []*Block 48 limit int 49 } 50 51 func (m LogBlockHashMarshaller) MarshalZerologArray(a *zerolog.Array) { 52 size := len(m.arr) 53 if size > m.limit { 54 for i := 0; i < m.limit-1; i++ { 55 a.Str(enc.ToString(m.arr[i].GetHash())) 56 } 57 a.Str(fmt.Sprintf("(and %d more)", size-m.limit+1)) 58 } else { 59 for _, element := range m.arr { 60 a.Str(enc.ToString(element.GetHash())) 61 } 62 } 63 } 64 65 type LogTxIDsMarshaller struct { 66 arr []TxID 67 limit int 68 } 69 70 func NewLogTxIDsMarshaller(arr []TxID, limit int) *LogTxIDsMarshaller { 71 return &LogTxIDsMarshaller{arr: arr, limit: limit} 72 } 73 74 func (m LogTxIDsMarshaller) MarshalZerologArray(a *zerolog.Array) { 75 size := len(m.arr) 76 if size > m.limit { 77 for i := 0; i < m.limit-1; i++ { 78 a.Str(m.arr[i].String()) 79 } 80 a.Str(fmt.Sprintf("(and %d more)", size-m.limit+1)) 81 } else { 82 for _, element := range m.arr { 83 a.Str(element.String()) 84 } 85 } 86 } 87 88 type RaftMbrsMarshaller struct { 89 arr []*MemberAttr 90 limit int 91 } 92 93 func (m RaftMbrsMarshaller) MarshalZerologArray(a *zerolog.Array) { 94 size := len(m.arr) 95 if size > m.limit { 96 for i := 0; i < m.limit-1; i++ { 97 a.Str(m.arr[i].GetName()) 98 } 99 a.Str(fmt.Sprintf("(and %d more)", size-m.limit+1)) 100 } else { 101 for _, element := range m.arr { 102 a.Str(element.GetName()) 103 } 104 } 105 } 106 107 func (m *GoAwayNotice) MarshalZerologObject(e *zerolog.Event) { 108 e.Str("reason", m.Message) 109 } 110 111 func (m *AddressesResponse) MarshalZerologObject(e *zerolog.Event) { 112 e.Str(LogRespStatus, m.Status.String()).Int("cnt", len(m.Peers)) 113 } 114 115 func (m *GetTransactionsRequest) MarshalZerologObject(e *zerolog.Event) { 116 e.Array("hashes", NewLogB58EncMarshaller(m.Hashes, 10)) 117 } 118 119 func (m *GetTransactionsResponse) MarshalZerologObject(e *zerolog.Event) { 120 e.Str(LogRespStatus, m.Status.String()).Bool(LogHasNext, m.HasNext).Array("hashes", NewLogB58EncMarshaller(m.Hashes, 10)) 121 } 122 123 func (m *GetBlockRequest) MarshalZerologObject(e *zerolog.Event) { 124 e.Array("hashes", NewLogB58EncMarshaller(m.Hashes, 10)) 125 } 126 127 func (m *GetBlockResponse) MarshalZerologObject(e *zerolog.Event) { 128 e.Str(LogRespStatus, m.Status.String()).Bool(LogHasNext, m.HasNext).Array("hashes", LogBlockHashMarshaller{m.Blocks, 10}) 129 } 130 131 func (m *NewTransactionsNotice) MarshalZerologObject(e *zerolog.Event) { 132 e.Array("hashes", NewLogB58EncMarshaller(m.TxHashes, 10)) 133 } 134 135 func (m *BlockProducedNotice) MarshalZerologObject(e *zerolog.Event) { 136 e.Str("bp", enc.ToString(m.ProducerID)).Uint64(LogBlkNo, m.BlockNo).Str(LogBlkHash, enc.ToString(m.Block.Hash)) 137 } 138 139 func (m *Ping) MarshalZerologObject(e *zerolog.Event) { 140 e.Str(LogBlkHash, enc.ToString(m.BestBlockHash)).Uint64(LogBlkNo, m.BestHeight) 141 } 142 143 func (m *GetHashesRequest) MarshalZerologObject(e *zerolog.Event) { 144 e.Str("prev_hash", enc.ToString(m.PrevHash)).Uint64("prev_no", m.PrevNumber) 145 } 146 147 func (m *GetHashesResponse) MarshalZerologObject(e *zerolog.Event) { 148 e.Str(LogRespStatus, m.Status.String()).Bool(LogHasNext, m.HasNext).Array("hashes", NewLogB58EncMarshaller(m.Hashes, 10)) 149 } 150 151 func (m *GetBlockHeadersRequest) MarshalZerologObject(e *zerolog.Event) { 152 e.Str(LogBlkHash, enc.ToString(m.Hash)).Uint64(LogBlkNo, m.Height).Bool("ascending", m.Asc).Uint32("size", m.Size) 153 } 154 155 func (m *GetBlockHeadersResponse) MarshalZerologObject(e *zerolog.Event) { 156 e.Str(LogRespStatus, m.Status.String()).Bool(LogHasNext, m.HasNext).Array("hashes", NewLogB58EncMarshaller(m.Hashes, 10)) 157 } 158 159 func (m *GetHashByNo) MarshalZerologObject(e *zerolog.Event) { 160 e.Uint64(LogBlkNo, m.BlockNo) 161 } 162 163 func (m *GetHashByNoResponse) MarshalZerologObject(e *zerolog.Event) { 164 e.Str(LogRespStatus, m.Status.String()).Str(LogBlkHash, enc.ToString(m.BlockHash)) 165 } 166 167 func (m *GetAncestorRequest) MarshalZerologObject(e *zerolog.Event) { 168 e.Array("hashes", NewLogB58EncMarshaller(m.Hashes, 10)) 169 } 170 171 func (m *GetAncestorResponse) MarshalZerologObject(e *zerolog.Event) { 172 e.Str(LogRespStatus, m.Status.String()).Str(LogBlkHash, enc.ToString(m.AncestorHash)).Uint64(LogBlkNo, m.AncestorNo) 173 } 174 175 func (m *GetClusterInfoRequest) MarshalZerologObject(e *zerolog.Event) { 176 e.Str("best_hash", enc.ToString(m.BestBlockHash)) 177 } 178 179 func (m *GetClusterInfoResponse) MarshalZerologObject(e *zerolog.Event) { 180 e.Str(LogChainID, enc.ToString(m.ChainID)).Str("err", m.Error).Array("members",RaftMbrsMarshaller{arr:m.MbrAttrs, limit:10}).Uint64("cluster_id", m.ClusterID) 181 } 182