Merge branch 'debuglog' of https://github.com/afh/cointop into afh-debuglog

pull/133/head
Miguel Mota 3 years ago
commit 52a1699764
No known key found for this signature in database
GPG Key ID: 67EC1161588A00F9

@ -101,6 +101,7 @@ type Cointop struct {
colorscheme *Colorscheme colorscheme *Colorscheme
debug bool debug bool
filecache *filecache.FileCache filecache *filecache.FileCache
logfile *os.File
forceRefresh chan bool forceRefresh chan bool
limiter <-chan time.Time limiter <-chan time.Time
maxTableWidth int maxTableWidth int
@ -278,6 +279,9 @@ func NewCointop(config *Config) (*Cointop, error) {
Input: NewInputView(), Input: NewInputView(),
}, },
} }
if debug {
ct.initlog()
}
err := ct.SetupConfig() err := ct.SetupConfig()
if err != nil { if err != nil {

@ -1,27 +1,25 @@
package cointop package cointop
import ( import (
"fmt" "log"
"os" "os"
"time"
) )
// debuglog writs a debug log to stdout func (ct *Cointop) initlog() {
func (ct *Cointop) debuglog(msg string) {
if !ct.debug {
return
}
filename := "/tmp/cointop.log" filename := "/tmp/cointop.log"
f, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600) f, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
if err != nil { if err != nil {
panic(err) panic(err)
} }
log.SetOutput(f)
ct.logfile = f
}
defer f.Close() // debuglog writes a debug log message to /tmp/cointop.log if the DEBUG environment is set.
func (ct *Cointop) debuglog(format string, args ...interface{}) {
text := fmt.Sprintf("%v %s\n", time.Now().Unix(), msg) if !ct.debug {
if _, err = f.WriteString(text); err != nil { return
panic(err)
} }
log.Printf(format+"\n", args...)
} }

@ -1,7 +1,6 @@
package cointop package cointop
import ( import (
"fmt"
"math" "math"
) )
@ -442,7 +441,7 @@ func (ct *Cointop) HighlightRow(pageRowIndex int) error {
cy = h - (l - pageRowIndex) 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.SetOrigin(ox, oy)
ct.Views.Table.SetCursor(cx, cy) ct.Views.Table.SetCursor(cx, cy)
return nil return nil

@ -327,7 +327,7 @@ func (ct *Cointop) UpdatePortfolioUpdateMenu() error {
coin := ct.HighlightedRowCoin() coin := ct.HighlightedRowCoin()
exists := ct.PortfolioEntryExists(coin) exists := ct.PortfolioEntryExists(coin)
value := strconv.FormatFloat(ct.CoinHoldings(coin), 'f', -1, 64) 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 mode string
var current string var current string
var submitText string var submitText string

@ -6,8 +6,9 @@ import (
"github.com/miguelmota/gocui" "github.com/miguelmota/gocui"
) )
// Quit quites the program // Quit quits the program
func (ct *Cointop) Quit() error { func (ct *Cointop) Quit() error {
ct.logfile.Close()
return gocui.ErrQuit return gocui.ErrQuit
} }
@ -28,6 +29,7 @@ func (ct *Cointop) QuitView() error {
// Exit safely exits the program // Exit safely exits the program
func (ct *Cointop) Exit() { func (ct *Cointop) Exit() {
ct.debuglog("exit()") ct.debuglog("exit()")
ct.logfile.Close()
if ct.g != nil { if ct.g != nil {
ct.g.Close() ct.g.Close()
} else { } else {

Loading…
Cancel
Save