From 5cdfd830565a57b4d77969aa7696f0241d16e72d Mon Sep 17 00:00:00 2001 From: Alexis Hildebrandt Date: Sat, 24 Apr 2021 08:00:25 +0200 Subject: [PATCH 1/2] Use log package for logging --- cointop/cointop.go | 4 ++++ cointop/debug.go | 24 +++++++++++------------- cointop/navigation.go | 3 +-- cointop/portfolio.go | 2 +- cointop/quit.go | 4 +++- 5 files changed, 20 insertions(+), 17 deletions(-) diff --git a/cointop/cointop.go b/cointop/cointop.go index f54d59a..46def74 100644 --- a/cointop/cointop.go +++ b/cointop/cointop.go @@ -101,6 +101,7 @@ type Cointop struct { colorscheme *Colorscheme debug bool filecache *filecache.FileCache + logfile *os.File forceRefresh chan bool limiter <-chan time.Time maxTableWidth int @@ -278,6 +279,9 @@ func NewCointop(config *Config) (*Cointop, error) { Input: NewInputView(), }, } + if debug { + ct.initlog() + } err := ct.SetupConfig() if err != nil { diff --git a/cointop/debug.go b/cointop/debug.go index d655403..a48d038 100644 --- a/cointop/debug.go +++ b/cointop/debug.go @@ -1,27 +1,25 @@ package cointop import ( - "fmt" + "log" "os" - "time" ) -// debuglog writs a debug log to stdout -func (ct *Cointop) debuglog(msg string) { - if !ct.debug { - return - } - +func (ct *Cointop) initlog() { filename := "/tmp/cointop.log" f, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600) if err != nil { panic(err) } + log.SetOutput(f) + ct.logfile = f +} - defer f.Close() - - text := fmt.Sprintf("%v %s\n", time.Now().Unix(), msg) - if _, err = f.WriteString(text); err != nil { - panic(err) +// debuglog writes a debug log message to /tmp/cointop.log if the DEBUG environment is set. +func (ct *Cointop) debuglog(fmt string, arg ...interface{}) { + if !ct.debug { + return } + + log.Printf(fmt+"\n", arg...) } diff --git a/cointop/navigation.go b/cointop/navigation.go index df7b12f..1a0d178 100644 --- a/cointop/navigation.go +++ b/cointop/navigation.go @@ -1,7 +1,6 @@ package cointop import ( - "fmt" "math" ) @@ -442,7 +441,7 @@ func (ct *Cointop) HighlightRow(pageRowIndex int) error { cy = h - (l - pageRowIndex) } } - ct.debuglog(fmt.Sprintf("highlightRow idx:%v h:%v cy:%v oy:%v", pageRowIndex, h, cy, oy)) + ct.debuglog("highlightRow idx:%v h:%v cy:%v oy:%v", pageRowIndex, h, cy, oy) ct.Views.Table.SetOrigin(ox, oy) ct.Views.Table.SetCursor(cx, cy) return nil diff --git a/cointop/portfolio.go b/cointop/portfolio.go index b4e8495..f09ecb3 100644 --- a/cointop/portfolio.go +++ b/cointop/portfolio.go @@ -327,7 +327,7 @@ func (ct *Cointop) UpdatePortfolioUpdateMenu() error { coin := ct.HighlightedRowCoin() exists := ct.PortfolioEntryExists(coin) value := strconv.FormatFloat(ct.CoinHoldings(coin), 'f', -1, 64) - ct.debuglog(fmt.Sprintf("holdings %v", value)) + ct.debuglog("holdings %v", value) var mode string var current string var submitText string diff --git a/cointop/quit.go b/cointop/quit.go index fd8abf0..bd60222 100644 --- a/cointop/quit.go +++ b/cointop/quit.go @@ -6,8 +6,9 @@ import ( "github.com/miguelmota/gocui" ) -// Quit quites the program +// Quit quits the program func (ct *Cointop) Quit() error { + ct.logfile.Close() return gocui.ErrQuit } @@ -28,6 +29,7 @@ func (ct *Cointop) QuitView() error { // Exit safely exits the program func (ct *Cointop) Exit() { ct.debuglog("exit()") + ct.logfile.Close() if ct.g != nil { ct.g.Close() } else { From dcd0410a1953c26b6d80fc21b3ee0cc59142250c Mon Sep 17 00:00:00 2001 From: Alexis Hildebrandt Date: Sun, 25 Apr 2021 06:38:40 +0200 Subject: [PATCH 2/2] Address PR comments --- cointop/debug.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cointop/debug.go b/cointop/debug.go index a48d038..eff9c72 100644 --- a/cointop/debug.go +++ b/cointop/debug.go @@ -16,10 +16,10 @@ func (ct *Cointop) initlog() { } // debuglog writes a debug log message to /tmp/cointop.log if the DEBUG environment is set. -func (ct *Cointop) debuglog(fmt string, arg ...interface{}) { +func (ct *Cointop) debuglog(format string, args ...interface{}) { if !ct.debug { return } - log.Printf(fmt+"\n", arg...) + log.Printf(format+"\n", args...) }