Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(runtime): implement custom logging handler that print function name #1825

Merged
merged 4 commits into from
Oct 5, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/runtime/wasmer/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"sync"

"github.com/ChainSafe/gossamer/lib/common"
Expand Down Expand Up @@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rename to customFileHandler and make the function private.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we put this in the runtime package? since the other wasm interpreters will likely end up using the same logger

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to runtime package, renamed to CustomFileHandler, kept public since it was moved to runtime package.

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")
Expand All @@ -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))
}

Expand Down