Skip to content

Commit

Permalink
feat: add debug file (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno authored Apr 19, 2024
1 parent 1351a2f commit 6cc7959
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ cover.out
tmtop
dist
**/.DS_Store
out.txt
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ This app has 2 modes, use [Tab] button to switch between them:
If you have issues with the app, try pressing the D button to open the debug panel.
Most likely, the app cannot connect to one of the hosts it needs to connect.
If there's something unusual, feel free to report a bug on this repository.
You can also enable debug logging and write them to file in addition to the debug panel
by running tmtop with `--verbose --debug-file <path-to-file>`.

Some common errors:

Expand Down
1 change: 1 addition & 0 deletions cmd/tmtop.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func main() {
rootCmd.PersistentFlags().DurationVar(&config.UpgradeRefreshRate, "upgrade-refresh-rate", 30*time.Minute, "Upgrades refresh rate")
rootCmd.PersistentFlags().DurationVar(&config.BlockTimeRefreshRate, "block-time-refresh-rate", 30*time.Second, "Block time refresh rate")
rootCmd.PersistentFlags().StringVar(&config.LCDHost, "lcd-host", "", "LCD API host URL")
rootCmd.PersistentFlags().StringVar(&config.DebugFile, "debug-file", "", "Path to file to write debug info/logs to")

if err := rootCmd.Execute(); err != nil {
logger.GetDefaultLogger().Fatal().Err(err).Msg("Could not start application")
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type Config struct {
ChainType ChainType
Verbose bool
DisableEmojis bool
DebugFile string

LCDHost string
}
Expand Down
30 changes: 29 additions & 1 deletion pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,44 @@ func GetDefaultLogger() *zerolog.Logger {

type Writer struct {
io.Writer
DebugFile *os.File
LogChannel chan string
}

func NewWriter(logChannel chan string, config configPkg.Config) Writer {
writer := Writer{
LogChannel: logChannel,
}

if config.DebugFile != "" {
debugFile, err := os.OpenFile(config.DebugFile, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0755)
if err != nil {
panic(err)
}

writer.DebugFile = debugFile
}

return writer
}

func (w Writer) Write(msg []byte) (int, error) {
w.LogChannel <- string(msg)

if w.DebugFile != nil {
if _, err := w.DebugFile.Write(msg); err != nil {
return 0, err
}
}

return len(msg), nil
}

func GetLogger(logChannel chan string, config configPkg.Config) *zerolog.Logger {
writer := zerolog.ConsoleWriter{Out: Writer{LogChannel: logChannel}, NoColor: true}
writer := zerolog.ConsoleWriter{
Out: NewWriter(logChannel, config),
NoColor: true,
}
log := zerolog.New(writer).With().Timestamp().Logger()

if config.Verbose {
Expand Down

0 comments on commit 6cc7959

Please sign in to comment.