[cmd] add config migration 2.1.0

- add harmonyconfig.FlagSliceToLogVerbosePrints
- rename --verbose-prints to --log.verbose-prints
- add detailed usage
pull/3734/head
Peter Chung 4 years ago
parent 1ed07e93d1
commit fe4361fbf1
  1. 10
      cmd/harmony/config_migrations.go
  2. 5
      cmd/harmony/default.go
  3. 19
      cmd/harmony/flags.go
  4. 18
      cmd/harmony/flags_test.go
  5. 2
      cmd/harmony/main.go
  6. 2
      go.mod
  7. 25
      internal/configs/harmony/harmony.go

@ -157,4 +157,14 @@ func init() {
confTree.Set("Version", "2.0.0")
return confTree
}
migrations["2.0.0"] = func(confTree *toml.Tree) *toml.Tree {
// Legacy conf missing fields
if confTree.Get("Log.VerbosePrints") == nil {
confTree.Set("Log.VerbosePrints", defaultConfig.Log.VerbosePrints)
}
confTree.Set("Version", "2.1.0")
return confTree
}
}

@ -5,7 +5,7 @@ import (
nodeconfig "github.com/harmony-one/harmony/internal/configs/node"
)
const tomlConfigVersion = "2.0.0"
const tomlConfigVersion = "2.1.0"
const (
defNetworkType = nodeconfig.Mainnet
@ -71,6 +71,9 @@ var defaultConfig = harmonyconfig.HarmonyConfig{
FileName: "harmony.log",
RotateSize: 100,
Verbosity: 3,
VerbosePrints: harmonyconfig.LogVerbosePrints{
Config: false,
},
},
DNSSync: getDefaultDNSSyncConfig(defNetworkType),
}

@ -135,7 +135,7 @@ var (
logContextIPFlag,
logContextPortFlag,
logVerbosityFlag,
verbosePrintsFlag,
logVerbosePrintsFlag,
legacyVerbosityFlag,
legacyLogFolderFlag,
@ -996,9 +996,10 @@ var (
Usage: "logging verbosity: 0=silent, 1=error, 2=warn, 3=info, 4=debug, 5=detail",
DefValue: defaultConfig.Log.Verbosity,
}
verbosePrintsFlag = cli.StringSliceFlag{
Name: "verbose-prints",
Usage: "verbose prints, available options: config",
logVerbosePrintsFlag = cli.StringSliceFlag{
Name: "log.verbose-prints",
Usage: "debugging feature. to print verbose internal objects as JSON in log file. available internal objects: config",
DefValue: []string{},
}
// TODO: remove context (this shall not be in the log)
logContextIPFlag = cli.StringFlag{
@ -1056,13 +1057,9 @@ func applyLogFlags(cmd *cobra.Command, config *harmonyconfig.HarmonyConfig) {
config.Log.Verbosity = cli.GetIntFlagValue(cmd, legacyVerbosityFlag)
}
if cli.IsFlagChanged(cmd, verbosePrintsFlag) {
verbosePrintsSlice := cli.GetStringSliceFlagValue(cmd, verbosePrintsFlag)
verbosePrintsMap := make(map[string]bool, 0)
for _, verbosePrint := range verbosePrintsSlice {
verbosePrintsMap[verbosePrint] = true
}
config.Log.VerbosePrints = verbosePrintsMap
if cli.IsFlagChanged(cmd, logVerbosePrintsFlag) {
verbosePrintsFlagSlice := cli.GetStringSliceFlagValue(cmd, logVerbosePrintsFlag)
config.Log.VerbosePrints = harmonyconfig.FlagSliceToLogVerbosePrints(verbosePrintsFlagSlice)
}
if cli.HasFlagsChanged(cmd, []cli.Flag{logContextIPFlag, logContextPortFlag}) {

@ -818,12 +818,15 @@ func TestLogFlags(t *testing.T) {
},
{
args: []string{"--log.dir", "latest_log", "--log.max-size", "10", "--log.name", "harmony.log",
"--log.verb", "5"},
"--log.verb", "5", "--log.verbose-prints", "config"},
expConfig: harmonyconfig.LogConfig{
Folder: "latest_log",
FileName: "harmony.log",
RotateSize: 10,
Verbosity: 5,
VerbosePrints: harmonyconfig.LogVerbosePrints{
Config: true,
},
Context: nil,
},
},
@ -834,6 +837,7 @@ func TestLogFlags(t *testing.T) {
FileName: defaultConfig.Log.FileName,
RotateSize: defaultConfig.Log.RotateSize,
Verbosity: defaultConfig.Log.Verbosity,
VerbosePrints: defaultConfig.Log.VerbosePrints,
Context: &harmonyconfig.LogContext{
IP: "8.8.8.8",
Port: 9001,
@ -848,23 +852,13 @@ func TestLogFlags(t *testing.T) {
FileName: "validator-8.8.8.8-9001.log",
RotateSize: 10,
Verbosity: 5,
VerbosePrints: defaultConfig.Log.VerbosePrints,
Context: &harmonyconfig.LogContext{
IP: "8.8.8.8",
Port: 9001,
},
},
},
{
args: []string{"--verbose-prints", "config"},
expConfig: harmonyconfig.LogConfig{
Folder: defaultConfig.Log.Folder,
FileName: defaultConfig.Log.FileName,
RotateSize: defaultConfig.Log.RotateSize,
Verbosity: defaultConfig.Log.Verbosity,
VerbosePrints: map[string]bool{"config": true},
Context: nil,
},
},
}
for i, test := range tests {
ts := newFlagTestSuite(t, append(logFlags, legacyMiscFlags...),

@ -382,7 +382,7 @@ func setupNodeAndRun(hc harmonyconfig.HarmonyConfig) {
nodeconfig.SetPeerID(myHost.GetID())
if val, ok := hc.Log.VerbosePrints["config"]; ok && val {
if hc.Log.VerbosePrints.Config {
utils.Logger().Info().Interface("config", rpc_common.Config{
HarmonyConfig: hc,
NodeConfig: *nodeConfig,

@ -40,7 +40,7 @@ require (
github.com/multiformats/go-multiaddr-dns v0.3.1
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/pborman/uuid v1.2.0
github.com/pelletier/go-toml v1.2.0
github.com/pelletier/go-toml v1.9.2
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.8.0
github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0

@ -1,5 +1,10 @@
package harmony
import (
"reflect"
"strings"
)
// HarmonyConfig contains all the configs user can set for running harmony binary. Served as the bridge
// from user set flags to internal node configs. Also user can persist this structure to a toml file
// to avoid inputting all arguments.
@ -90,10 +95,28 @@ type LogConfig struct {
FileName string
RotateSize int
Verbosity int
VerbosePrints map[string]bool `toml:",omitempty"`
VerbosePrints LogVerbosePrints
Context *LogContext `toml:",omitempty"`
}
type LogVerbosePrints struct {
Config bool
}
func FlagSliceToLogVerbosePrints(verbosePrintsFlagSlice []string) LogVerbosePrints {
verbosePrints := LogVerbosePrints{}
verbosePrintsReflect := reflect.Indirect(reflect.ValueOf(&verbosePrints))
for _, verbosePrint := range verbosePrintsFlagSlice {
verbosePrint = strings.Title(verbosePrint)
field := verbosePrintsReflect.FieldByName(verbosePrint)
if field.IsValid() && field.CanSet() {
field.SetBool(true)
}
}
return verbosePrints
}
type LogContext struct {
IP string
Port int

Loading…
Cancel
Save