Skip to content

Commit 7b73406

Browse files
committed
Merge branch 'afh-debuglog'
2 parents a794be6 + 52a1699 commit 7b73406

File tree

5 files changed

+20
-17
lines changed

5 files changed

+20
-17
lines changed

cointop/cointop.go

+4
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ type Cointop struct {
101101
colorscheme *Colorscheme
102102
debug bool
103103
filecache *filecache.FileCache
104+
logfile *os.File
104105
forceRefresh chan bool
105106
limiter <-chan time.Time
106107
maxTableWidth int
@@ -278,6 +279,9 @@ func NewCointop(config *Config) (*Cointop, error) {
278279
Input: NewInputView(),
279280
},
280281
}
282+
if debug {
283+
ct.initlog()
284+
}
281285

282286
err := ct.SetupConfig()
283287
if err != nil {

cointop/debug.go

+11-13
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
package cointop
22

33
import (
4-
"fmt"
4+
"log"
55
"os"
6-
"time"
76
)
87

9-
// debuglog writs a debug log to stdout
10-
func (ct *Cointop) debuglog(msg string) {
11-
if !ct.debug {
12-
return
13-
}
14-
8+
func (ct *Cointop) initlog() {
159
filename := "/tmp/cointop.log"
1610
f, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
1711
if err != nil {
1812
panic(err)
1913
}
14+
log.SetOutput(f)
15+
ct.logfile = f
16+
}
2017

21-
defer f.Close()
22-
23-
text := fmt.Sprintf("%v %s\n", time.Now().Unix(), msg)
24-
if _, err = f.WriteString(text); err != nil {
25-
panic(err)
18+
// debuglog writes a debug log message to /tmp/cointop.log if the DEBUG environment is set.
19+
func (ct *Cointop) debuglog(format string, args ...interface{}) {
20+
if !ct.debug {
21+
return
2622
}
23+
24+
log.Printf(format+"\n", args...)
2725
}

cointop/navigation.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cointop
22

33
import (
4-
"fmt"
54
"math"
65
)
76

@@ -442,7 +441,7 @@ func (ct *Cointop) HighlightRow(pageRowIndex int) error {
442441
cy = h - (l - pageRowIndex)
443442
}
444443
}
445-
ct.debuglog(fmt.Sprintf("highlightRow idx:%v h:%v cy:%v oy:%v", pageRowIndex, h, cy, oy))
444+
ct.debuglog("highlightRow idx:%v h:%v cy:%v oy:%v", pageRowIndex, h, cy, oy)
446445
ct.Views.Table.SetOrigin(ox, oy)
447446
ct.Views.Table.SetCursor(cx, cy)
448447
return nil

cointop/portfolio.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ func (ct *Cointop) UpdatePortfolioUpdateMenu() error {
327327
coin := ct.HighlightedRowCoin()
328328
exists := ct.PortfolioEntryExists(coin)
329329
value := strconv.FormatFloat(ct.CoinHoldings(coin), 'f', -1, 64)
330-
ct.debuglog(fmt.Sprintf("holdings %v", value))
330+
ct.debuglog("holdings %v", value)
331331
var mode string
332332
var current string
333333
var submitText string

cointop/quit.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import (
66
"github.com/miguelmota/gocui"
77
)
88

9-
// Quit quites the program
9+
// Quit quits the program
1010
func (ct *Cointop) Quit() error {
11+
ct.logfile.Close()
1112
return gocui.ErrQuit
1213
}
1314

@@ -28,6 +29,7 @@ func (ct *Cointop) QuitView() error {
2829
// Exit safely exits the program
2930
func (ct *Cointop) Exit() {
3031
ct.debuglog("exit()")
32+
ct.logfile.Close()
3133
if ct.g != nil {
3234
ct.g.Close()
3335
} else {

0 commit comments

Comments
 (0)