From bba2ee915ec9c869ee0ffc1af82b6e0ea510c1d4 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Fri, 13 Jul 2018 11:45:32 +0100 Subject: [PATCH] Print verbose output to STDERR --- leak/finder.go | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/leak/finder.go b/leak/finder.go index a824686..d4fa1e4 100644 --- a/leak/finder.go +++ b/leak/finder.go @@ -2,6 +2,7 @@ package leak import ( "fmt" + "io" "time" ) @@ -16,17 +17,18 @@ func NewFinder(filePath1, filePath2, filePath3 string) *Finder { // Finder helps with finding a memory leak across three different memory dumps // from a Ruby process. type Finder struct { - FilePaths [3]string - Dumps [3]*Dump - Leaks []*string - Verbose bool + FilePaths [3]string + Dumps [3]*Dump + Leaks []*string + Verbose bool + VerboseWriter io.Writer } // Process will will load and process each of the dump files. func (s *Finder) Process() error { for i, filePath := range s.FilePaths { start := time.Now() - s.log(fmt.Sprintf("Parsing %s", filePath)) + s.verbose(fmt.Sprintf("Parsing %s", filePath)) dump := NewDump(filePath) err := dump.Process() @@ -36,7 +38,7 @@ func (s *Finder) Process() error { s.Dumps[i] = dump elapsed := time.Now().Sub(start) - s.log(fmt.Sprintf( + s.verbose(fmt.Sprintf( "Parsed %d objects in %.6f seconds", len(dump.Index), elapsed.Seconds(), @@ -96,3 +98,15 @@ func (s *Finder) log(msg string) { 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) + } +}