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

feat: add debug file #18

Merged
merged 1 commit into from
Apr 19, 2024
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
@@ -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:

1 change: 1 addition & 0 deletions cmd/tmtop.go
Original file line number Diff line number Diff line change
@@ -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")
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -52,6 +52,7 @@ type Config struct {
ChainType ChainType
Verbose bool
DisableEmojis bool
DebugFile string

LCDHost string
}
30 changes: 29 additions & 1 deletion pkg/logger/logger.go
Original file line number Diff line number Diff line change
@@ -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 {