Print verbose output to STDERR

This commit is contained in:
2018-07-13 11:45:32 +01:00
parent eff9aa36f6
commit bba2ee915e

View File

@@ -2,6 +2,7 @@ package leak
import ( import (
"fmt" "fmt"
"io"
"time" "time"
) )
@@ -16,17 +17,18 @@ func NewFinder(filePath1, filePath2, filePath3 string) *Finder {
// Finder helps with finding a memory leak across three different memory dumps // Finder helps with finding a memory leak across three different memory dumps
// from a Ruby process. // from a Ruby process.
type Finder struct { type Finder struct {
FilePaths [3]string FilePaths [3]string
Dumps [3]*Dump Dumps [3]*Dump
Leaks []*string Leaks []*string
Verbose bool Verbose bool
VerboseWriter io.Writer
} }
// Process will will load and process each of the dump files. // Process will will load and process each of the dump files.
func (s *Finder) Process() error { func (s *Finder) Process() error {
for i, filePath := range s.FilePaths { for i, filePath := range s.FilePaths {
start := time.Now() start := time.Now()
s.log(fmt.Sprintf("Parsing %s", filePath)) s.verbose(fmt.Sprintf("Parsing %s", filePath))
dump := NewDump(filePath) dump := NewDump(filePath)
err := dump.Process() err := dump.Process()
@@ -36,7 +38,7 @@ func (s *Finder) Process() error {
s.Dumps[i] = dump s.Dumps[i] = dump
elapsed := time.Now().Sub(start) elapsed := time.Now().Sub(start)
s.log(fmt.Sprintf( s.verbose(fmt.Sprintf(
"Parsed %d objects in %.6f seconds", "Parsed %d objects in %.6f seconds",
len(dump.Index), len(dump.Index),
elapsed.Seconds(), elapsed.Seconds(),
@@ -96,3 +98,15 @@ func (s *Finder) log(msg string) {
fmt.Println(msg) fmt.Println(msg)
} }
} }
func (s *Finder) verbose(msg string) {
if s.Verbose {
w := s.VerboseWriter
if w == nil {
w = os.Stderr
}
fmt.Fprintln(w, msg)
}
}