Skip to content

Commit

Permalink
Normalized logging behavior of library
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthieu Vachon committed Dec 1, 2020
1 parent f01c386 commit 6c90f08
Show file tree
Hide file tree
Showing 21 changed files with 465 additions and 392 deletions.
40 changes: 20 additions & 20 deletions abidecoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ func (a *ABI) Decode(binaryDecoder *Decoder, structName string) ([]byte, error)
}

func (a *ABI) decode(binaryDecoder *Decoder, structName string) (map[string]interface{}, error) {
if loggingEnabled {
abiDecoderLog.Debug("decode struct", zap.String("name", structName))
if traceEnabled {
zlog.Debug("decode struct", zap.String("name", structName))
}

structure := a.StructForName(structName)
Expand All @@ -72,13 +72,13 @@ func (a *ABI) decode(binaryDecoder *Decoder, structName string) (map[string]inte

builtStruct := map[string]interface{}{}
if structure.Base != "" {
if loggingEnabled {
abiDecoderLog.Debug("struct has base struct", zap.String("name", structName), zap.String("base", structure.Base))
if traceEnabled {
zlog.Debug("struct has base struct", zap.String("name", structName), zap.String("base", structure.Base))
}

baseName, isAlias := a.TypeNameForNewTypeName(structure.Base)
if isAlias && loggingEnabled {
abiDecoderLog.Debug("base is an alias", zap.String("from", structure.Base), zap.String("to", baseName))
if isAlias && traceEnabled {
zlog.Debug("base is an alias", zap.String("from", structure.Base), zap.String("to", baseName))
}

var err error
Expand Down Expand Up @@ -122,8 +122,8 @@ func (a *ABI) resolveField(binaryDecoder *Decoder, initialFieldType string) (out
fieldType, isOptional, isArray, isBinaryExtension := analyzeFieldType(initialFieldType)
//fmt.Println("resolveField", isOptional, isArray, initialFieldType, fieldType)

if loggingEnabled {
abiDecoderLog.Debug("analyzed field",
if traceEnabled {
zlog.Debug("analyzed field",
zap.String("field_type", fieldType),
zap.Bool("is_optional", isOptional),
zap.Bool("is_array", isArray),
Expand All @@ -134,8 +134,8 @@ func (a *ABI) resolveField(binaryDecoder *Decoder, initialFieldType string) (out
// check if this field is an alias
aliasFieldType, isAlias := a.TypeNameForNewTypeName(fieldType)
if isAlias {
if loggingEnabled {
abiDecoderLog.Debug("type is an alias",
if traceEnabled {
zlog.Debug("type is an alias",
zap.String("from", fieldType),
zap.String("to", aliasFieldType),
)
Expand All @@ -145,8 +145,8 @@ func (a *ABI) resolveField(binaryDecoder *Decoder, initialFieldType string) (out

// check if the field is a binary extension
if isBinaryExtension && !binaryDecoder.hasRemaining() {
if loggingEnabled {
abiDecoderLog.Debug("type is a binary extension and no more data, skipping field", zap.String("type", fieldType))
if traceEnabled {
zlog.Debug("type is a binary extension and no more data, skipping field", zap.String("type", fieldType))
}
return skipField, nil
}
Expand All @@ -159,8 +159,8 @@ func (a *ABI) resolveField(binaryDecoder *Decoder, initialFieldType string) (out
}

if b == 0 {
if loggingEnabled {
abiDecoderLog.Debug("field is not present")
if traceEnabled {
zlog.Debug("field is not present")
}
if !a.fitNodeos {
return skipField, nil
Expand Down Expand Up @@ -230,13 +230,13 @@ func (a *ABI) read(binaryDecoder *Decoder, fieldType string) (interface{}, error
}

variantFieldType := variant.Types[variantIndex]
if loggingEnabled {
abiDecoderLog.Debug("field is a variant", zap.String("type", variantFieldType))
if traceEnabled {
zlog.Debug("field is a variant", zap.String("type", variantFieldType))
}

resolvedVariantFieldType, isAlias := a.TypeNameForNewTypeName(variantFieldType)
if isAlias && loggingEnabled {
abiDecoderLog.Debug("variant type is an alias", zap.String("from", fieldType), zap.String("to", resolvedVariantFieldType))
if isAlias && traceEnabled {
zlog.Debug("variant type is an alias", zap.String("from", fieldType), zap.String("to", resolvedVariantFieldType))
}

fieldType = resolvedVariantFieldType
Expand Down Expand Up @@ -381,8 +381,8 @@ func (a *ABI) read(binaryDecoder *Decoder, fieldType string) (interface{}, error
return nil, fmt.Errorf("read: %s", err)
}

if loggingEnabled {
abiDecoderLog.Debug("set field value",
if traceEnabled {
zlog.Debug("set field value",
zap.Reflect("value", value),
)
}
Expand Down
54 changes: 27 additions & 27 deletions abiencoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ func (a *ABI) EncodeStruct(structName string, json []byte) ([]byte, error) {
}

func (a *ABI) encode(binaryEncoder *Encoder, structName string, json []byte) error {
if loggingEnabled {
abiEncoderLog.Debug("abi encode struct", zap.String("name", structName))
if traceEnabled {
zlog.Debug("abi encode struct", zap.String("name", structName))
}

structure := a.StructForName(structName)
Expand All @@ -78,8 +78,8 @@ func (a *ABI) encode(binaryEncoder *Encoder, structName string, json []byte) err
}

if structure.Base != "" {
if loggingEnabled {
abiEncoderLog.Debug("struct has base struct", zap.String("struct", structName), zap.String("base", structure.Base))
if traceEnabled {
zlog.Debug("struct has base struct", zap.String("struct", structName), zap.String("base", structure.Base))
}
err := a.encode(binaryEncoder, structure.Base, json)
if err != nil {
Expand All @@ -91,24 +91,24 @@ func (a *ABI) encode(binaryEncoder *Encoder, structName string, json []byte) err
}
func (a *ABI) encodeFields(binaryEncoder *Encoder, fields []FieldDef, json []byte) error {

if loggingEnabled {
defer func(prev *zap.Logger) { abiEncoderLog = prev }(abiEncoderLog)
abiEncoderLog = encoderLog.Named("fields")
defer func(prev *zap.Logger) { encoderLog = prev }(encoderLog)
encoderLog = encoderLog.Named("fields")
if traceEnabled {
defer func(prev *zap.Logger) { zlog = prev }(zlog)
zlog = zlog.Named("fields")
defer func(prev *zap.Logger) { zlog = prev }(zlog)
zlog = zlog.Named("fields")
}

for _, field := range fields {

if loggingEnabled {
abiEncoderLog.Debug("encode field", zap.String("name", field.Name), zap.String("type", field.Type))
if traceEnabled {
zlog.Debug("encode field", zap.String("name", field.Name), zap.String("type", field.Type))
}

fieldType, isOptional, isArray, _ := analyzeFieldType(field.Type)
typeName, isAlias := a.TypeNameForNewTypeName(fieldType)
fieldName := field.Name
if isAlias && loggingEnabled {
abiEncoderLog.Debug("type is an alias", zap.String("from", field.Type), zap.String("to", typeName))
if isAlias && traceEnabled {
zlog.Debug("type is an alias", zap.String("from", field.Type), zap.String("to", typeName))
}

err := a.encodeField(binaryEncoder, fieldName, typeName, isOptional, isArray, json)
Expand All @@ -121,22 +121,22 @@ func (a *ABI) encodeFields(binaryEncoder *Encoder, fields []FieldDef, json []byt

func (a *ABI) encodeField(binaryEncoder *Encoder, fieldName string, fieldType string, isOptional bool, isArray bool, json []byte) (err error) {

if loggingEnabled {
abiEncoderLog.Debug("encode field json", zap.ByteString("json", json))
if traceEnabled {
zlog.Debug("encode field json", zap.ByteString("json", json))
}

value := gjson.GetBytes(json, fieldName)
if isOptional {
if value.Exists() {
if loggingEnabled {
abiEncoderLog.Debug("field is optional and present", zap.String("name", fieldName), zap.String("type", fieldType))
if traceEnabled {
zlog.Debug("field is optional and present", zap.String("name", fieldName), zap.String("type", fieldType))
}
if e := binaryEncoder.writeByte(1); e != nil {
return e
}
} else {
if loggingEnabled {
abiEncoderLog.Debug("field is optional and *not* present", zap.String("name", fieldName), zap.String("type", fieldType))
if traceEnabled {
zlog.Debug("field is optional and *not* present", zap.String("name", fieldName), zap.String("type", fieldType))
}
return binaryEncoder.writeByte(0)
}
Expand All @@ -147,8 +147,8 @@ func (a *ABI) encodeField(binaryEncoder *Encoder, fieldName string, fieldType st

if isArray {

if loggingEnabled {
abiEncoderLog.Debug("field is an array", zap.String("name", fieldName), zap.String("type", fieldType))
if traceEnabled {
zlog.Debug("field is an array", zap.String("name", fieldName), zap.String("type", fieldType))
}
if !value.IsArray() {
return fmt.Errorf("encode field: expected array for field [%s] got [%s]", fieldName, value.Type.String())
Expand All @@ -169,14 +169,14 @@ func (a *ABI) encodeField(binaryEncoder *Encoder, fieldName string, fieldType st

func (a *ABI) writeField(binaryEncoder *Encoder, fieldName string, fieldType string, value gjson.Result) error {

if loggingEnabled {
abiEncoderLog.Debug("write field", zap.String("name", fieldName), zap.String("type", fieldType), zap.String("json", value.Raw))
if traceEnabled {
zlog.Debug("write field", zap.String("name", fieldName), zap.String("type", fieldType), zap.String("json", value.Raw))
}

structure := a.StructForName(fieldType)
if structure != nil {
if loggingEnabled {
abiEncoderLog.Debug("field is a struct", zap.String("name", fieldName))
if traceEnabled {
zlog.Debug("field is a struct", zap.String("name", fieldName))
}

err := a.encodeFields(binaryEncoder, structure.Fields, []byte(value.Raw))
Expand Down Expand Up @@ -375,8 +375,8 @@ func (a *ABI) writeField(binaryEncoder *Encoder, fieldName string, fieldType str
return fmt.Errorf("writing field of type [%s]: unknown type", fieldType)
}

if loggingEnabled {
abiEncoderLog.Debug("write object", zap.Reflect("value", object))
if traceEnabled {
zlog.Debug("write object", zap.Reflect("value", object))
}

return binaryEncoder.Encode(object)
Expand Down
3 changes: 2 additions & 1 deletion cmd/eos-p2p-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log"

"github.com/dfuse-io/logging"
"github.com/eoscanada/eos-go/p2p"
)

Expand All @@ -17,7 +18,7 @@ func main() {
flag.Parse()

if *showLog {
p2p.EnableP2PLogging()
logging.Set(logging.MustCreateLogger(), "github.com/eoscanada/eos-go/p2p")
}
defer p2p.SyncLogger()

Expand Down
3 changes: 2 additions & 1 deletion cmd/eos-p2p-proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log"

"github.com/dfuse-io/logging"
"github.com/eoscanada/eos-go/p2p"
)

Expand All @@ -20,7 +21,7 @@ func main() {
fmt.Println("P2P Proxy")

if *showLog {
p2p.EnableP2PLogging()
logging.Set(logging.MustCreateLogger(), "github.com/eoscanada/eos-go/p2p")
}
defer p2p.SyncLogger()

Expand Down
6 changes: 3 additions & 3 deletions cmd/eos-p2p-relay/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package main

import (
"fmt"

"flag"
"fmt"

"github.com/dfuse-io/logging"
"github.com/eoscanada/eos-go/p2p"
)

Expand All @@ -16,7 +16,7 @@ func main() {
flag.Parse()

if *showLog {
p2p.EnableP2PLogging()
logging.Set(logging.MustCreateLogger(), "github.com/eoscanada/eos-go/p2p")
}
defer p2p.SyncLogger()

Expand Down
Loading

0 comments on commit 6c90f08

Please sign in to comment.