From 066482d8c97d391134b39c82884b6bc8425f102c Mon Sep 17 00:00:00 2001 From: David Vennik Date: Mon, 9 Jan 2023 00:02:29 +0000 Subject: [PATCH] Fixed codeloc option behaviour also fixed config file paths to have proper tree structure --- cmd/indra/main.go | 9 +++------ pkg/app/app.go | 27 ++++++++++++++------------- pkg/app/app_test.go | 2 +- pkg/cmds/args.go | 1 - pkg/cmds/args_test.go | 2 +- pkg/cmds/commands.go | 15 +++++---------- pkg/interrupt/interrupt.go | 8 +------- pkg/log/log.go | 19 ++++++++++++------- pkg/opts/Integer/spec.go | 12 +++--------- pkg/opts/duration/spec.go | 16 +++++----------- pkg/opts/float/spec.go | 12 +++--------- pkg/opts/list/spec.go | 8 ++------ pkg/opts/text/spec.go | 20 +++++--------------- pkg/opts/toggle/spec.go | 12 +++--------- pkg/p2p/metrics/host.go | 3 ++- pkg/p2p/seed/bootstrap.go | 5 +++-- pkg/wire/codec_test.go | 23 +++++++++++------------ pkg/wire/onion_test.go | 8 ++++---- version.go | 6 +++--- 19 files changed, 81 insertions(+), 127 deletions(-) diff --git a/cmd/indra/main.go b/cmd/indra/main.go index fdf2b123..946f6215 100644 --- a/cmd/indra/main.go +++ b/cmd/indra/main.go @@ -2,6 +2,8 @@ package main import ( "fmt" + "os" + "github.com/indra-labs/indra" "github.com/indra-labs/indra/pkg/app" "github.com/indra-labs/indra/pkg/cfg" @@ -14,7 +16,6 @@ import ( "github.com/indra-labs/indra/pkg/server" "github.com/libp2p/go-libp2p/core/crypto" "github.com/multiformats/go-multiaddr" - "os" ) var ( @@ -22,10 +23,6 @@ var ( check = log.E.Chk ) -func init() { - log2.App = "indra" -} - var commands = &cmds.Command{ Name: "indra", Description: "Network Freedom.", @@ -137,7 +134,7 @@ var commands = &cmds.Command{ func multiAddrSanitizer(opt *list.Opt) error { - //log.I.Ln("adding p2p listener", opt.String()) + // log.I.Ln("adding p2p listener", opt.String()) return nil } diff --git a/pkg/app/app.go b/pkg/app/app.go index 71a3e512..f24b386c 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -2,7 +2,7 @@ package app import ( "github.com/indra-labs/indra" - cmds2 "github.com/indra-labs/indra/pkg/cmds" + "github.com/indra-labs/indra/pkg/cmds" log2 "github.com/indra-labs/indra/pkg/log" ) @@ -12,30 +12,31 @@ var ( ) type App struct { - *cmds2.Command - launch *cmds2.Command + *cmds.Command + launch *cmds.Command runArgs []string - cmds2.Envs + cmds.Envs } -func New(cmd *cmds2.Command, args []string) (a *App, e error) { +func New(c *cmds.Command, args []string) (a *App, e error) { + log2.App = c.Name // Add the default configuration items for datadir/configfile - cmds2.GetConfigBase(cmd.Configs, cmd.Name, false) + cmds.GetConfigBase(c.Configs, c.Name, false) // Add the help function - cmd.AddCommand(cmds2.Help()) - a = &App{Command: cmd} + c.AddCommand(cmds.Help()) + a = &App{Command: c} + if a.Command, e = cmds.Init(c, nil); check(e) { + return + } // We first parse the CLI args, in case config file location has been // specified if a.launch, _, e = a.Command.ParseCLIArgs(args); check(e) { return } - if e = cmd.LoadConfig(); log.E.Chk(e) { + if e = c.LoadConfig(); log.E.Chk(e) { return } - if a.Command, e = cmds2.Init(cmd, nil); check(e) { - return - } - a.Envs = cmd.GetEnvs() + a.Envs = c.GetEnvs() if e = a.Envs.LoadFromEnvironment(); check(e) { return } diff --git a/pkg/app/app_test.go b/pkg/app/app_test.go index 07090bd5..eb3f707a 100644 --- a/pkg/app/app_test.go +++ b/pkg/app/app_test.go @@ -11,7 +11,7 @@ import ( func TestNew(t *testing.T) { log2.SetLogLevel(log2.Trace) - log2.CodeLoc = true + args1 := "/random/path/to/server_binary --cafile ~/some/cafile --LC=cn node -addrindex --BD 48h30s" args1s := strings.Split(args1, " ") var a *App diff --git a/pkg/cmds/args.go b/pkg/cmds/args.go index 69884b13..66768a7d 100644 --- a/pkg/cmds/args.go +++ b/pkg/cmds/args.go @@ -187,7 +187,6 @@ func lookAhead(cmd *Command, cfgName, arg string, iArgs []string, break } } - // Otherwise set the boolean value to the opposite of default. cur := cmd.Configs[cfgName].Meta().Default() cmd.Configs[cfgName].FromString(cur) v := !cmd.Configs[cfgName].Value().Bool() diff --git a/pkg/cmds/args_test.go b/pkg/cmds/args_test.go index 493aff6a..180cb51a 100644 --- a/pkg/cmds/args_test.go +++ b/pkg/cmds/args_test.go @@ -9,7 +9,7 @@ import ( func TestCommand_ParseCLIArgs(t *testing.T) { log2.SetLogLevel(log2.Trace) - log2.CodeLoc = true + ec := GetExampleCommands() o, _ := Init(ec, nil) args6 := "/random/path/to/server_binary --cafile ~/some/cafile --LC=cn " + diff --git a/pkg/cmds/commands.go b/pkg/cmds/commands.go index d7a3edeb..a43f292f 100644 --- a/pkg/cmds/commands.go +++ b/pkg/cmds/commands.go @@ -19,9 +19,7 @@ import ( type Op func(c *Command, args []string) error var NoOp = func(c *Command, args []string) error { return nil } -var Tags = func(s ...string) []string { - return s -} +var Tags = func(s ...string) []string { return s } // Command is a specification for a command and can include any number of // subcommands, and for each Command a list of options @@ -96,12 +94,9 @@ loaded from at application startup, and where it will be written if changed. Documentation: strings.TrimSpace(strings.TrimSpace(` Toggles on and off the printing of code locations in logs. `)), - Default: "true", + Default: "false", }, func(o *toggle.Opt) (err error) { - // fmt.Println(log2.CodeLoc) - // fmt.Println(runtime.Caller(1)) - // fmt.Println(spew.Sdump(o.Value().Bool())) - // log2.CodeLoc = o.Value().Bool() + log2.LogCodeLocations(o.Value().Bool()) return }), @@ -227,7 +222,8 @@ func (c *Command) GetOpt(path path.Path) (o config.Option) { // search subcommands for i := range c.Commands { if util.Norm(c.Commands[i].Name) == util.Norm(p[1]) { - return c.Commands[i].GetOpt(p[1:]) + cc := c.Commands[i] + return cc.GetOpt(p[1:]) } } case len(p) == 2: @@ -262,7 +258,6 @@ func (c *Command) GetCommand(p string) (o *Command) { } func (c *Command) GetValue(key string) config.Concrete { - return c.Configs[key].Value() } diff --git a/pkg/interrupt/interrupt.go b/pkg/interrupt/interrupt.go index 30e03efb..eab8cfe6 100644 --- a/pkg/interrupt/interrupt.go +++ b/pkg/interrupt/interrupt.go @@ -6,7 +6,6 @@ import ( "os/exec" "os/signal" "runtime" - "strings" "syscall" "go.uber.org/atomic" @@ -43,12 +42,6 @@ var interruptCallbackSources []string // responds to custom shutdown signals as required func Listener() { invokeCallbacks := func() { - log.I.Ln( - "running interrupt callbacks", - len(interruptCallbacks), - strings.Repeat(" ", 48), - interruptCallbackSources, - ) // run handlers in LIFO order. for i := range interruptCallbacks { idx := len(interruptCallbacks) - 1 - i @@ -103,6 +96,7 @@ out: case sig := <-ch: // if !requested { // L.Printf("\r>>> received signal (%s)\n", sig) + fmt.Print("\r") log.I.Ln("received interrupt signal", sig) requested.Store(true) invokeCallbacks() diff --git a/pkg/log/log.go b/pkg/log/log.go index 678f24a0..c2fa6d0a 100644 --- a/pkg/log/log.go +++ b/pkg/log/log.go @@ -144,7 +144,7 @@ var ( // allSubsystems stores all package subsystem names found in the current // application. allSubsystems []string - CodeLoc = true + codeLoc = false ) func GetAllSubsystems() (o []string) { @@ -202,6 +202,12 @@ func SetLogLevel(l LogLevel) { logLevel = l } +func LogCodeLocations(on bool) { + writerMx.Lock() + defer writerMx.Unlock() + codeLoc = on +} + // GetLoc calls runtime.Caller and formats as expected by source code editors // for terminal hyperlinks // @@ -246,8 +252,9 @@ func GetLoc(skip int, subsystem string) (output string) { } // LocTimeStampFormat is a custom time format that provides millisecond precision. -var LocTimeStampFormat = "2006-01-02T15:04:05.000000Z07:00" -var timeStampFormat = time.Stamp +var LocTimeStampFormat = "2006-01-02T15:04:05.000000" + +var timeStampFormat = "15:04:05.000" // SetTimeStampFormat sets a custom timeStampFormat for the logger func SetTimeStampFormat(format string) { @@ -256,9 +263,7 @@ func SetTimeStampFormat(format string) { // getTimeText is a helper that returns the current time with the // timeStampFormat that is configured. -func getTimeText(tsf string) string { - return time.Now().Format(tsf) -} +func getTimeText(tsf string) string { return time.Now().Format(tsf) } // joinStrings constructs a string from a slice of interface same as Println but // without the terminal newline @@ -286,7 +291,7 @@ func logPrint( formatString := "%v%s%s%-6v %s\n" loc := "" tsf := timeStampFormat - if CodeLoc { + if codeLoc { formatString = "%-58v%s%s%-6v %s\n" loc = GetLoc(3, subsystem) tsf = LocTimeStampFormat diff --git a/pkg/opts/Integer/spec.go b/pkg/opts/Integer/spec.go index 31a9b63f..199b5013 100644 --- a/pkg/opts/Integer/spec.go +++ b/pkg/opts/Integer/spec.go @@ -17,13 +17,9 @@ type Opt struct { h []Hook } -func (o *Opt) Path() (p path.Path) { - return o.p -} +func (o *Opt) Path() (p path.Path) { return o.p } -func (o *Opt) SetPath(p path.Path) { - o.p = p -} +func (o *Opt) SetPath(p path.Path) { o.p = p } var _ config.Option = &Opt{} @@ -70,9 +66,7 @@ func (o *Opt) String() (s string) { return strconv.FormatInt(o.v.Load(), 10) } -func (o *Opt) Expanded() (s string) { - return o.String() -} +func (o *Opt) Expanded() (s string) { return o.String() } func (o *Opt) SetExpanded(s string) { err := o.FromString(s) diff --git a/pkg/opts/duration/spec.go b/pkg/opts/duration/spec.go index ca407ce8..7c91cdd6 100644 --- a/pkg/opts/duration/spec.go +++ b/pkg/opts/duration/spec.go @@ -18,13 +18,9 @@ type Opt struct { h []Hook } -func (o *Opt) Path() (p path.Path) { - return o.p -} +func (o *Opt) Path() (p path.Path) { return o.p } -func (o *Opt) SetPath(p path.Path) { - o.p = p -} +func (o *Opt) SetPath(p path.Path) { o.p = p } var _ config.Option = &Opt{} @@ -68,13 +64,11 @@ func (o *Opt) String() (s string) { return fmt.Sprint(o.v.Load()) } -func (o *Opt) Expanded() (s string) { - return o.String() -} +func (o *Opt) Expanded() (s string) { return o.String() } func (o *Opt) SetExpanded(s string) { - err := o.FromString(s) - log.E.Chk(err) + if err := o.FromString(s); log.E.Chk(err) { + } } func (o *Opt) Value() (c config.Concrete) { diff --git a/pkg/opts/float/spec.go b/pkg/opts/float/spec.go index dd11f799..50f00c91 100644 --- a/pkg/opts/float/spec.go +++ b/pkg/opts/float/spec.go @@ -17,13 +17,9 @@ type Opt struct { h []Hook } -func (o *Opt) Path() (p path.Path) { - return o.p -} +func (o *Opt) Path() (p path.Path) { return o.p } -func (o *Opt) SetPath(p path.Path) { - o.p = p -} +func (o *Opt) SetPath(p path.Path) { o.p = p } var _ config.Option = &Opt{} @@ -70,9 +66,7 @@ func (o *Opt) String() (s string) { return strconv.FormatFloat(o.v.Load(), 'f', -1, 64) } -func (o *Opt) Expanded() (s string) { - return o.String() -} +func (o *Opt) Expanded() (s string) { return o.String() } func (o *Opt) SetExpanded(s string) { err := o.FromString(s) diff --git a/pkg/opts/list/spec.go b/pkg/opts/list/spec.go index f4e9922e..21a56adf 100644 --- a/pkg/opts/list/spec.go +++ b/pkg/opts/list/spec.go @@ -20,13 +20,9 @@ type Opt struct { var _ config.Option = &Opt{} -func (o *Opt) Path() (p path.Path) { - return o.p -} +func (o *Opt) Path() (p path.Path) { return o.p } -func (o *Opt) SetPath(p path.Path) { - o.p = p -} +func (o *Opt) SetPath(p path.Path) { o.p = p } type Hook func(*Opt) error diff --git a/pkg/opts/text/spec.go b/pkg/opts/text/spec.go index 6d0bbec3..789e2017 100644 --- a/pkg/opts/text/spec.go +++ b/pkg/opts/text/spec.go @@ -18,13 +18,9 @@ type Opt struct { h []Hook } -func (o *Opt) Path() (p path.Path) { - return o.p -} +func (o *Opt) Path() (p path.Path) { return o.p } -func (o *Opt) SetPath(p path.Path) { - o.p = p -} +func (o *Opt) SetPath(p path.Path) { o.p = p } var _ config.Option = &Opt{} @@ -62,17 +58,11 @@ func (o *Opt) FromString(s string) (e error) { return } -func (o *Opt) String() (s string) { - return o.v.Load() -} +func (o *Opt) String() (s string) { return o.v.Load() } -func (o *Opt) Expanded() (s string) { - return o.x.Load() -} +func (o *Opt) Expanded() (s string) { return o.x.Load() } -func (o *Opt) SetExpanded(s string) { - o.x.Store(s) -} +func (o *Opt) SetExpanded(s string) { o.x.Store(s) } func (o *Opt) Value() (c config.Concrete) { c = config.NewConcrete() diff --git a/pkg/opts/toggle/spec.go b/pkg/opts/toggle/spec.go index 8ad77bab..a135adc1 100644 --- a/pkg/opts/toggle/spec.go +++ b/pkg/opts/toggle/spec.go @@ -18,13 +18,9 @@ type Opt struct { h []Hook } -func (o *Opt) Path() (p path.Path) { - return o.p -} +func (o *Opt) Path() (p path.Path) { return o.p } -func (o *Opt) SetPath(p path.Path) { - o.p = p -} +func (o *Opt) SetPath(p path.Path) { o.p = p } var _ config.Option = &Opt{} @@ -78,9 +74,7 @@ func (o *Opt) String() (s string) { return strconv.FormatBool(o.v.Load()) } -func (o *Opt) Expanded() (s string) { - return o.String() -} +func (o *Opt) Expanded() (s string) { return o.String() } func (o *Opt) SetExpanded(s string) { err := o.FromString(s) diff --git a/pkg/p2p/metrics/host.go b/pkg/p2p/metrics/host.go index 406e992e..b5799137 100644 --- a/pkg/p2p/metrics/host.go +++ b/pkg/p2p/metrics/host.go @@ -2,10 +2,11 @@ package metrics import ( "context" + "time" + "github.com/indra-labs/indra" log2 "github.com/indra-labs/indra/pkg/log" "github.com/libp2p/go-libp2p/core/host" - "time" ) var ( diff --git a/pkg/p2p/seed/bootstrap.go b/pkg/p2p/seed/bootstrap.go index 9e1cfc00..20ac67c2 100644 --- a/pkg/p2p/seed/bootstrap.go +++ b/pkg/p2p/seed/bootstrap.go @@ -3,13 +3,14 @@ package seed import ( "context" "errors" + "sync" + "time" + "github.com/indra-labs/indra" log2 "github.com/indra-labs/indra/pkg/log" "github.com/libp2p/go-libp2p/core/host" "github.com/libp2p/go-libp2p/core/peer" "github.com/multiformats/go-multiaddr" - "sync" - "time" ) var ( diff --git a/pkg/wire/codec_test.go b/pkg/wire/codec_test.go index 92d85b95..516c8005 100644 --- a/pkg/wire/codec_test.go +++ b/pkg/wire/codec_test.go @@ -10,7 +10,6 @@ import ( "github.com/indra-labs/indra/pkg/key/prv" "github.com/indra-labs/indra/pkg/key/pub" - log2 "github.com/indra-labs/indra/pkg/log" "github.com/indra-labs/indra/pkg/nonce" "github.com/indra-labs/indra/pkg/sha256" "github.com/indra-labs/indra/pkg/slice" @@ -30,7 +29,7 @@ import ( ) func TestOnionSkins_Cipher(t *testing.T) { - log2.CodeLoc = true + var e error hdrP, pldP := GetTwoPrvKeys(t) // hdr, pld := pub.Derive(hdrP), pub.Derive(pldP) @@ -60,7 +59,7 @@ func TestOnionSkins_Cipher(t *testing.T) { } func TestOnionSkins_Confirmation(t *testing.T) { - log2.CodeLoc = true + var e error n := nonce.NewID() on := OnionSkins{}. @@ -85,7 +84,7 @@ func TestOnionSkins_Confirmation(t *testing.T) { } func TestOnionSkins_Delay(t *testing.T) { - log2.CodeLoc = true + var e error del := time.Duration(rand.Uint64()) on := OnionSkins{}. @@ -110,7 +109,7 @@ func TestOnionSkins_Delay(t *testing.T) { } func TestOnionSkins_Exit(t *testing.T) { - log2.CodeLoc = true + var e error prvs, pubs := GetCipherSet(t) ciphers := GenCiphers(prvs, pubs) @@ -161,7 +160,7 @@ func TestOnionSkins_Exit(t *testing.T) { } func TestOnionSkins_Forward(t *testing.T) { - log2.CodeLoc = true + var e error ipSizes := []int{net.IPv4len, net.IPv6len} for i := range ipSizes { @@ -204,7 +203,7 @@ func TestOnionSkins_Forward(t *testing.T) { } func TestOnionSkins_Layer(t *testing.T) { - log2.CodeLoc = true + var e error n := nonce.NewID() n1 := nonce.New() @@ -245,7 +244,7 @@ func TestOnionSkins_Layer(t *testing.T) { } func TestOnionSkins_Purchase(t *testing.T) { - log2.CodeLoc = true + var e error prvs, pubs := GetCipherSet(t) ciphers := GenCiphers(prvs, pubs) @@ -285,7 +284,7 @@ func TestOnionSkins_Purchase(t *testing.T) { } func TestOnionSkins_Reply(t *testing.T) { - log2.CodeLoc = true + var e error ipSizes := []int{net.IPv4len, net.IPv6len} for i := range ipSizes { @@ -328,7 +327,7 @@ func TestOnionSkins_Reply(t *testing.T) { } func TestOnionSkins_Response(t *testing.T) { - log2.CodeLoc = true + var e error var msg slice.Bytes var hash sha256.Hash @@ -360,7 +359,7 @@ func TestOnionSkins_Response(t *testing.T) { } func TestOnionSkins_Session(t *testing.T) { - log2.CodeLoc = true + var e error hdrP, pldP := GetTwoPrvKeys(t) hdr, pld := pub.Derive(hdrP), pub.Derive(pldP) @@ -391,7 +390,7 @@ func TestOnionSkins_Session(t *testing.T) { } func TestOnionSkins_Token(t *testing.T) { - log2.CodeLoc = true + var e error ni := nonce.NewID() n := sha256.Single(ni[:]) diff --git a/pkg/wire/onion_test.go b/pkg/wire/onion_test.go index dbbbf804..87cddc61 100644 --- a/pkg/wire/onion_test.go +++ b/pkg/wire/onion_test.go @@ -118,7 +118,7 @@ func PeelExit(t *testing.T, b slice.Bytes, } func TestPing(t *testing.T) { - log2.CodeLoc = true + _, ks, e := signer.New() if check(e) { t.Error(e) @@ -198,7 +198,7 @@ func TestPing(t *testing.T) { } func TestSendKeys(t *testing.T) { - log2.CodeLoc = true + _, ks, e := signer.New() if check(e) { t.Error(e) @@ -322,7 +322,7 @@ func TestSendKeys(t *testing.T) { } func TestSendPurchase(t *testing.T) { - log2.CodeLoc = true + log2.SetLogLevel(log2.Trace) _, ks, e := signer.New() if check(e) { @@ -424,7 +424,7 @@ func TestSendPurchase(t *testing.T) { } func TestSendExit(t *testing.T) { - log2.CodeLoc = true + _, ks, e := signer.New() if check(e) { t.Error(e) diff --git a/version.go b/version.go index bc02953e..d735e7c9 100644 --- a/version.go +++ b/version.go @@ -8,11 +8,11 @@ var ( // URL is the git URL for the repository. URL = "github.com/indra-labs/indra" // GitRef is the gitref, as in refs/heads/branchname. - GitRef = "refs/heads/protocol" + GitRef = "refs/heads/proc-debug" // ParentGitCommit is the commit hash of the parent HEAD. - ParentGitCommit = "0a37f65155ad746edf54a4f653d65bfc889e078a" + ParentGitCommit = "14449a6c621cb0478387c84053d31e1233af2115" // BuildTime stores the time when the current binary was built. - BuildTime = "2023-01-08T13:56:39Z" + BuildTime = "2023-01-09T00:02:29Z" // SemVer lists the (latest) git tag on the release. SemVer = "v0.1.5" // PathBase is the path base returned from runtime caller.