From 4d218c6feee18136c2b7a7bb8adcf9b05aa43c94 Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Fri, 1 Oct 2021 17:29:18 -0400 Subject: [PATCH 1/2] implement custom logging handler that print function name --- lib/runtime/wasmer/instance.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/runtime/wasmer/instance.go b/lib/runtime/wasmer/instance.go index 85eabbe730..892162b61b 100644 --- a/lib/runtime/wasmer/instance.go +++ b/lib/runtime/wasmer/instance.go @@ -20,6 +20,8 @@ import ( "errors" "fmt" "os" + "path/filepath" + "strings" "sync" "github.com/ChainSafe/gossamer/lib/common" @@ -100,6 +102,15 @@ func NewInstance(code []byte, cfg *Config) (*Instance, error) { return newInstance(code, cfg) } +// FuncCallerFileHandler returns a Handler that adds the name of the calling function to the context with key "func" +// and the line number and file of the calling function to the context with key "caller". +func FuncCallerFileHandler(h log.Handler) log.Handler { + return log.FuncHandler(func(r *log.Record) error { + r.Ctx = append(r.Ctx, "func", strings.TrimLeft(filepath.Ext(r.Call.Frame().Function), "."), "caller", fmt.Sprint(r.Call)) + return h.Log(r) + }) +} + func newInstance(code []byte, cfg *Config) (*Instance, error) { if len(code) == 0 { return nil, errors.New("code is empty") @@ -108,7 +119,7 @@ func newInstance(code []byte, cfg *Config) (*Instance, error) { // if cfg.LogLvl set to < 0, then don't change package log level if cfg.LogLvl >= 0 { h := log.StreamHandler(os.Stdout, log.TerminalFormat()) - h = log.CallerFileHandler(h) + h = FuncCallerFileHandler(h) logger.SetHandler(log.LvlFilterHandler(cfg.LogLvl, h)) } From 2f9bdc682bf90d1fc94045a6b947df2759e3d703 Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Tue, 5 Oct 2021 14:55:37 -0400 Subject: [PATCH 2/2] move log handler to runtime package --- lib/runtime/log_handler.go | 34 ++++++++++++++++++++++++++++++++++ lib/runtime/wasmer/instance.go | 13 +------------ 2 files changed, 35 insertions(+), 12 deletions(-) create mode 100644 lib/runtime/log_handler.go diff --git a/lib/runtime/log_handler.go b/lib/runtime/log_handler.go new file mode 100644 index 0000000000..e73f63f1d7 --- /dev/null +++ b/lib/runtime/log_handler.go @@ -0,0 +1,34 @@ +// Copyright 2019 ChainSafe Systems (ON) Corp. +// This file is part of gossamer. +// +// The gossamer library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The gossamer library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the gossamer library. If not, see . + +package runtime + +import ( + "fmt" + "path/filepath" + "strings" + + log "github.com/ChainSafe/log15" +) + +// CustomFileHandler returns a Handler that adds the name of the calling function to the context with key "func" +// and the line number and file of the calling function to the context with key "caller". +func CustomFileHandler(h log.Handler) log.Handler { + return log.FuncHandler(func(r *log.Record) error { + r.Ctx = append(r.Ctx, "func", strings.TrimLeft(filepath.Ext(r.Call.Frame().Function), "."), "caller", fmt.Sprint(r.Call)) + return h.Log(r) + }) +} diff --git a/lib/runtime/wasmer/instance.go b/lib/runtime/wasmer/instance.go index 892162b61b..64720bdba1 100644 --- a/lib/runtime/wasmer/instance.go +++ b/lib/runtime/wasmer/instance.go @@ -20,8 +20,6 @@ import ( "errors" "fmt" "os" - "path/filepath" - "strings" "sync" "github.com/ChainSafe/gossamer/lib/common" @@ -102,15 +100,6 @@ func NewInstance(code []byte, cfg *Config) (*Instance, error) { return newInstance(code, cfg) } -// FuncCallerFileHandler returns a Handler that adds the name of the calling function to the context with key "func" -// and the line number and file of the calling function to the context with key "caller". -func FuncCallerFileHandler(h log.Handler) log.Handler { - return log.FuncHandler(func(r *log.Record) error { - r.Ctx = append(r.Ctx, "func", strings.TrimLeft(filepath.Ext(r.Call.Frame().Function), "."), "caller", fmt.Sprint(r.Call)) - return h.Log(r) - }) -} - func newInstance(code []byte, cfg *Config) (*Instance, error) { if len(code) == 0 { return nil, errors.New("code is empty") @@ -119,7 +108,7 @@ func newInstance(code []byte, cfg *Config) (*Instance, error) { // if cfg.LogLvl set to < 0, then don't change package log level if cfg.LogLvl >= 0 { h := log.StreamHandler(os.Stdout, log.TerminalFormat()) - h = FuncCallerFileHandler(h) + h = runtime.CustomFileHandler(h) logger.SetHandler(log.LvlFilterHandler(cfg.LogLvl, h)) }