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

Fix logging on the simulator #873

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions x/programs/cmd/simulator/cmd/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ func (f *logFactory) makeLogger(config logging.Config) (logging.Logger, error) {
if _, ok := f.loggers[config.LoggerName]; ok {
return nil, fmt.Errorf("logger with name %q already exists", config.LoggerName)
}
consoleEnc := config.LogFormat.ConsoleEncoder()
consoleEnc := logging.Colors.ConsoleEncoder()
fileEnc := config.LogFormat.FileEncoder()

var consoleWriter io.WriteCloser
if config.DisableWriterDisplaying {
consoleWriter = newDiscardWriteCloser()
} else {
consoleWriter = os.Stdout
consoleWriter = os.Stderr
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We use Stderr in order not to interfere with the plans being piped from Stdin. This is hopefully a temporary solution.

}

consoleCore := logging.NewWrappedCore(config.DisplayLevel, consoleWriter, consoleEnc)
consoleCore := logging.NewWrappedCore(config.LogLevel, consoleWriter, consoleEnc)
consoleCore.WriterDisabled = config.DisableWriterDisplaying

rw := &lumberjack.Logger{
Expand Down
50 changes: 35 additions & 15 deletions x/programs/cmd/simulator/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,23 @@ const (
simulatorFolder = ".simulator"
)

const (
LogDisableDisplayLogsKey = "log-disable-display-plugin-logs"
LogLevelKey = "log-level"
CleanupKey = "cleanup"
)

type simulator struct {
log logging.Logger
logLevel string
log logging.Logger

vm *vm.VM
db *state.SimpleMutable
genesis *genesis.Genesis
cleanup func()
logLevel string
cleanup bool
disableWriterDisplaying bool

vm *vm.VM
db *state.SimpleMutable
genesis *genesis.Genesis
cleanupFn func()
}

func NewRootCmd() *cobra.Command {
Expand All @@ -48,21 +57,33 @@ func NewRootCmd() *cobra.Command {
Use: "simulator",
Short: "HyperSDK program VM simulator",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
// only display general help when no subcommand is passed
if len(args) == 0 {
cmd.Help()
os.Exit(0)
}
},
}

cmd.PersistentFlags().Bool("cleanup", false, "remove simulator directory on exit")
cmd.PersistentFlags().BoolVar(&s.cleanup, CleanupKey, false, "remove simulator directory on exit")
cmd.PersistentFlags().StringVar(&s.logLevel, LogLevelKey, "info", "log level")
cmd.PersistentFlags().BoolVar(&s.disableWriterDisplaying, LogDisableDisplayLogsKey, false, "disable displaying logs in stdout")

cobra.EnablePrefixMatching = true
cmd.CompletionOptions.HiddenDefaultCmd = true
cmd.DisableAutoGenTag = true
cmd.SilenceErrors = true
cmd.SetHelpCommand(&cobra.Command{Hidden: true})
cmd.PersistentFlags().StringVar(&s.logLevel, "log-level", "info", "log level")

// pre-parse the command to pre-parse flags
err := cmd.ParseFlags(os.Args[1:])
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

// initialize simulator vm
err := s.Init()
err = s.Init()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
Expand All @@ -86,9 +107,8 @@ func NewRootCmd() *cobra.Command {
}
}

cleanup, _ := cmd.Flags().GetBool("cleanup")
if cleanup {
s.cleanup()
if s.cleanup {
s.cleanupFn()
}
})

Expand Down Expand Up @@ -123,9 +143,9 @@ func (s *simulator) Init() error {
}
loggingConfig.Directory = path.Join(basePath, fmt.Sprintf("logs-%s", nodeID.String()))
loggingConfig.LogFormat = logging.JSON
loggingConfig.DisableWriterDisplaying = true
loggingConfig.DisableWriterDisplaying = s.disableWriterDisplaying

s.cleanup = func() {
s.cleanupFn = func() {
if err := os.RemoveAll(dbPath); err != nil {
fmt.Fprintf(os.Stderr, "failed to remove simulator directory: %s\n", err)
}
Expand Down
12 changes: 12 additions & 0 deletions x/programs/cmd/simulator/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/near/borsh-go v0.3.1
github.com/prometheus/client_golang v1.16.0
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.12.0
github.com/stretchr/testify v1.8.4
go.uber.org/zap v1.26.0
gopkg.in/natefinch/lumberjack.v2 v2.0.0
Expand All @@ -27,6 +28,7 @@ require (
github.com/cockroachdb/pebble v0.0.0-20230224221607-fccb83b60d5c // indirect
github.com/cockroachdb/redact v1.1.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/getsentry/sentry-go v0.18.0 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
Expand All @@ -38,23 +40,32 @@ require (
github.com/gorilla/rpc v1.2.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hdevalence/ed25519consensus v0.2.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/klauspost/compress v1.15.15 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/nbutton23/zxcvbn-go v0.0.0-20180912185939-ae427f1e4c1d // indirect
github.com/onsi/ginkgo/v2 v2.13.1 // indirect
github.com/openzipkin/zipkin-go v0.4.1 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.10.1 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.3.0 // indirect
github.com/supranational/blst v0.3.11 // indirect
go.opentelemetry.io/otel v1.11.2 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.11.2 // indirect
Expand All @@ -81,6 +92,7 @@ require (
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
google.golang.org/grpc v1.58.3 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

Expand Down
Loading
Loading