Skip to content

Commit

Permalink
re enable debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
zulkhair committed Jun 14, 2024
1 parent 8685e76 commit 1df124c
Show file tree
Hide file tree
Showing 16 changed files with 30 additions and 64 deletions.
2 changes: 0 additions & 2 deletions cmd/world/cardinal/cardinal.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,4 @@ var BaseCmd = &cobra.Command{
func init() {
// Register subcommands - `world cardinal [subcommand]`
BaseCmd.AddCommand(startCmd, devCmd, restartCmd, purgeCmd, stopCmd)
// Add --log-debug flag
logger.AddLogFlag(startCmd, devCmd, restartCmd, purgeCmd, stopCmd)
}
2 changes: 0 additions & 2 deletions cmd/world/cardinal/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ var devCmd = &cobra.Command{
return err
}

logger.SetDebugMode(cmd)

cfg, err := config.GetConfig(cmd)
if err != nil {
return err
Expand Down
4 changes: 1 addition & 3 deletions cmd/world/cardinal/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/spf13/cobra"

"pkg.world.dev/world-cli/common/logger"
"pkg.world.dev/world-cli/common/teacmd"
)

Expand All @@ -20,8 +19,7 @@ var purgeCmd = &cobra.Command{
Short: "Stop and reset the state of your Cardinal game shard",
Long: `Stop and reset the state of your Cardinal game shard.
This command stop all Docker services and remove all Docker volumes.`,
RunE: func(cmd *cobra.Command, _ []string) error {
logger.SetDebugMode(cmd)
RunE: func(_ *cobra.Command, _ []string) error {
err := teacmd.DockerPurge()
if err != nil {
return err
Expand Down
5 changes: 1 addition & 4 deletions cmd/world/cardinal/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/spf13/cobra"

"pkg.world.dev/world-cli/common/config"
"pkg.world.dev/world-cli/common/logger"
"pkg.world.dev/world-cli/common/teacmd"
)

Expand All @@ -19,8 +18,6 @@ This will restart the following Docker services:
- Cardinal (Core game logic)
- Nakama (Relay)`,
RunE: func(cmd *cobra.Command, _ []string) error {
logger.SetDebugMode(cmd)

cfg, err := config.GetConfig(cmd)
if err != nil {
return err
Expand All @@ -37,7 +34,7 @@ This will restart the following Docker services:
if cfg.Debug {
err = teacmd.DockerRestart(cfg, []teacmd.DockerService{
teacmd.DockerServiceCardinalDebug,
teacmd.DockerServiceNakama,
teacmd.DockerServiceNakamaDebug,
})
} else {
err = teacmd.DockerRestart(cfg, []teacmd.DockerService{
Expand Down
4 changes: 1 addition & 3 deletions cmd/world/cardinal/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"pkg.world.dev/world-cli/common"
"pkg.world.dev/world-cli/common/config"
"pkg.world.dev/world-cli/common/logger"
"pkg.world.dev/world-cli/common/teacmd"
)

Expand Down Expand Up @@ -58,8 +57,6 @@ This will start the following Docker services and its dependencies:
- Nakama (Relay)
- Redis (Cardinal dependency)`,
RunE: func(cmd *cobra.Command, _ []string) error {
logger.SetDebugMode(cmd)

cfg, err := config.GetConfig(cmd)
if err != nil {
return err
Expand Down Expand Up @@ -150,6 +147,7 @@ func init() {
startCmd.Flags().Bool(flagBuild, true, "Rebuild Docker images before starting")
startCmd.Flags().Bool(flagDetach, false, "Run in detached mode")
startCmd.Flags().String(flagLogLevel, "", "Set the log level")
startCmd.Flags().Bool(flagDebug, false, "Enable delve debugging")
}

// replaceBoolWithFlag overwrites the contents of vale with the contents of the given flag. If the flag
Expand Down
4 changes: 1 addition & 3 deletions cmd/world/cardinal/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/spf13/cobra"

"pkg.world.dev/world-cli/common/logger"
"pkg.world.dev/world-cli/common/teacmd"
)

Expand All @@ -24,8 +23,7 @@ This will stop the following Docker services:
- Cardinal (Game shard)
- Nakama (Relay) + DB
- Redis (Cardinal dependency)`,
RunE: func(cmd *cobra.Command, _ []string) error {
logger.SetDebugMode(cmd)
RunE: func(_ *cobra.Command, _ []string) error {
err := teacmd.DockerStopAll()
if err != nil {
return err
Expand Down
3 changes: 0 additions & 3 deletions cmd/world/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package evm
import (
"github.com/spf13/cobra"

"pkg.world.dev/world-cli/common/logger"
"pkg.world.dev/world-cli/common/teacmd"
"pkg.world.dev/world-cli/tea/style"
)
Expand All @@ -28,6 +27,4 @@ var BaseCmd = &cobra.Command{
func init() {
// Register subcommands - `world evm [subcommand]`
BaseCmd.AddCommand(startCmd, stopCmd)
// Add --debug flag
logger.AddLogFlag(startCmd, stopCmd)
}
2 changes: 0 additions & 2 deletions cmd/world/evm/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ var startCmd = &cobra.Command{
Short: "Start the EVM base shard. Use --da-auth-token to pass in an auth token directly.",
Long: "Start the EVM base shard. Requires connection to celestia DA.",
RunE: func(cmd *cobra.Command, _ []string) error {
logger.SetDebugMode(cmd)

cfg, err := config.GetConfig(cmd)
if err != nil {
return err
Expand Down
5 changes: 1 addition & 4 deletions cmd/world/evm/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@ import (

"github.com/spf13/cobra"

"pkg.world.dev/world-cli/common/logger"
"pkg.world.dev/world-cli/common/teacmd"
)

var stopCmd = &cobra.Command{
Use: "stop",
Short: "Stop the EVM base shard and DA layer client.",
Long: "Stop the EVM base shard and data availability layer client if they are running.",
RunE: func(cmd *cobra.Command, _ []string) error {
logger.SetDebugMode(cmd)

RunE: func(_ *cobra.Command, _ []string) error {
err := teacmd.DockerStop(services(teacmd.DockerServiceEVM, teacmd.DockerServiceDA))
if err != nil {
return err
Expand Down
4 changes: 1 addition & 3 deletions cmd/world/root/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
tea "github.com/charmbracelet/bubbletea"
"github.com/spf13/cobra"

"pkg.world.dev/world-cli/common/logger"
"pkg.world.dev/world-cli/common/teacmd"
"pkg.world.dev/world-cli/tea/component/steps"
"pkg.world.dev/world-cli/tea/style"
Expand Down Expand Up @@ -204,8 +203,7 @@ If [directory_name] is set, it will automatically clone the starter project into
Otherwise, it will prompt you to enter a directory name.`,
GroupID: "starter",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
logger.SetDebugMode(cmd)
RunE: func(_ *cobra.Command, args []string) error {
p := tea.NewProgram(NewWorldCreateModel(args), tea.WithOutput(writer))
if _, err := p.Run(); err != nil {
return err
Expand Down
4 changes: 1 addition & 3 deletions cmd/world/root/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/spf13/cobra"

"pkg.world.dev/world-cli/common/dependency"
"pkg.world.dev/world-cli/common/logger"
"pkg.world.dev/world-cli/common/teacmd"
"pkg.world.dev/world-cli/tea/style"
)
Expand Down Expand Up @@ -84,8 +83,7 @@ World CLI requires the following dependencies to be installed:
- Go
- Docker`,
GroupID: "starter",
RunE: func(cmd *cobra.Command, _ []string) error {
logger.SetDebugMode(cmd)
RunE: func(_ *cobra.Command, _ []string) error {
p := tea.NewProgram(NewWorldDoctorModel(), tea.WithOutput(writer))
_, err := p.Run()
if err != nil {
Expand Down
2 changes: 0 additions & 2 deletions cmd/world/root/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ func getLoginCmd() *cobra.Command {
Use: "login",
Short: "Authenticate using an access token",
RunE: func(cmd *cobra.Command, _ []string) error {
logger.SetDebugMode(cmd)

err := loginOnBrowser(cmd.Context())
if err != nil {
return eris.Wrap(err, "failed to login")
Expand Down
7 changes: 2 additions & 5 deletions cmd/world/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@ func init() {
rootCmd.AddCommand(evm.BaseCmd)

config.AddConfigFlag(rootCmd)

// Add --debug flag
logger.AddLogFlag(createCmd)
logger.AddLogFlag(doctorCmd)
logger.AddVerboseFlag(rootCmd)
}

func checkLatestVersion() error {
Expand Down Expand Up @@ -146,7 +143,7 @@ func Execute() {
logger.Errors(err)
}
// print log stack
logger.PrintLogs()
logger.PrintLogs(rootCmd)
}

// contextWithSigterm provides a context that automatically terminates when either the parent context is canceled or
Expand Down
33 changes: 14 additions & 19 deletions common/logger/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ const (
DefaultTimeFormat = "15:04:05.000"
DefaultCallerSkipFrameCount = 3 // set to 3 because logger wrapped in logger.go

NoColor = true
UseCaller = false // for developer, if you want to expose line of code of caller
flagDebug = "log-debug"
NoColor = true
UseCaller = false // for developer, if you want to expose line of code of caller
flagVerbose = "verbose"
)

var (
logBuffer bytes.Buffer

// DebugMode flag for determining debug mode
DebugMode = false
// VerboseMode flag for determining verbose logging
VerboseMode = false
)

func init() {
Expand Down Expand Up @@ -51,8 +51,12 @@ func init() {
}

// PrintLogs print all stacked log
func PrintLogs() {
if DebugMode {
func PrintLogs(cmd *cobra.Command) {
verbose, err := cmd.PersistentFlags().GetBool(flagVerbose)
if err != nil {
log.Error().Err(err).Msg("Cannot get flag --verbose")
}
if verbose {
// Extract the logs from the buffer and print them
logs := logBuffer.String()
if len(logs) > 0 {
Expand All @@ -62,18 +66,9 @@ func PrintLogs() {
}
}

// SetDebugMode Allow particular logger/message to be printed
// This function will extract flag --log-debug from command
func SetDebugMode(cmd *cobra.Command) {
val, err := cmd.Flags().GetBool(flagDebug)
if err == nil {
DebugMode = val
}
}

// AddLogFlag set flag --log-debug
func AddLogFlag(cmd ...*cobra.Command) {
// AddVerboseFlag set flag --log-debug
func AddVerboseFlag(cmd ...*cobra.Command) {
for _, c := range cmd {
c.Flags().Bool(flagDebug, false, "Enable World CLI debug logs")
c.PersistentFlags().Bool(flagVerbose, true, "Enable World CLI debug logs")
}
}
6 changes: 3 additions & 3 deletions common/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,21 @@ func FatalWithFields(msg string, kv map[string]interface{}) {

// Printf standard printf with debug mode validation
func Printf(format string, v ...interface{}) {
if DebugMode {
if VerboseMode {
fmt.Printf(format, v...)
}
}

// Println standard println with debug mode validation
func Println(v ...interface{}) {
if DebugMode {
if VerboseMode {
fmt.Println(v...)
}
}

// Print standard print with debug mode validation
func Print(v ...interface{}) {
if DebugMode {
if VerboseMode {
fmt.Print(v...)
}
}
7 changes: 4 additions & 3 deletions common/teacmd/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
DockerServiceEVM DockerService = "evm"
DockerServiceDA DockerService = "celestia-devnet"
DockerServiceCardinalDebug DockerService = "cardinal-debug"
DockerServiceNakamaDebug DockerService = "nakama-debug"
)

type DockerService string
Expand Down Expand Up @@ -98,15 +99,14 @@ func DockerStart(cfg *config.Config, services []DockerService) error {
// DockerStartAll starts both cardinal and nakama
func DockerStartAll(cfg *config.Config) error {
services := []DockerService{
DockerServiceNakama,
DockerServiceNakamaDB,
DockerServiceRedis,
}

if cfg.Debug {
services = append(services, DockerServiceCardinalDebug)
services = append(services, DockerServiceCardinalDebug, DockerServiceNakamaDebug)
} else {
services = append(services, DockerServiceCardinal)
services = append(services, DockerServiceCardinal, DockerServiceNakama)
}

return DockerStart(cfg, services)
Expand Down Expand Up @@ -150,6 +150,7 @@ func DockerStopAll() error {
DockerServiceCardinal,
DockerServiceCardinalDebug,
DockerServiceNakama,
DockerServiceNakamaDebug,
DockerServiceNakamaDB,
DockerServiceRedis,
})
Expand Down

0 comments on commit 1df124c

Please sign in to comment.