From ffc8b90e98ef3bb537017abee72770a43dd63336 Mon Sep 17 00:00:00 2001 From: surtur Date: Fri, 17 Jun 2022 19:55:23 +0200 Subject: [PATCH] chore(go): print algo-stamps without errors --- algo/algo.go | 9 ++------- algo/randomSearch.go | 13 +++++++++---- algo/randomSearch_test.go | 4 +--- algo/stochasticHillClimbing.go | 13 +++++++++---- algo/stochasticHillClimbing_test.go | 4 +--- 5 files changed, 22 insertions(+), 21 deletions(-) diff --git a/algo/algo.go b/algo/algo.go index d6786f9..fe92778 100644 --- a/algo/algo.go +++ b/algo/algo.go @@ -4,7 +4,6 @@ package algo import ( - "log" "sync" ) @@ -16,9 +15,7 @@ type Values []float64 func DoRandomSearch(wg *sync.WaitGroup) { defer wg.Done() - if err := printRandomSearch("starting..."); err != nil { - log.Printf("randomSearch: error printing RandomSearch to stderr: %q", err) - } + printRandomSearch("starting...") } // DoStochasticHillClimbing performs a search using the 'Stochastic Hill @@ -26,7 +23,5 @@ func DoRandomSearch(wg *sync.WaitGroup) { func DoStochasticHillClimbing(wg *sync.WaitGroup) { defer wg.Done() - if err := printSHC("starting..."); err != nil { - log.Printf("shc: error printing SHC to stderr: %q", err) - } + printSHC("starting...") } diff --git a/algo/randomSearch.go b/algo/randomSearch.go index 06bc896..37c4d20 100644 --- a/algo/randomSearch.go +++ b/algo/randomSearch.go @@ -20,10 +20,15 @@ func fmtRandomSearchOut(input string) string { return getRandomSearchLogPrefix() + " " + input } -func printRandomSearch(input string) error { - _, err := fmt.Fprintln(os.Stderr, fmtRandomSearchOut(input)) - - return err +func printRandomSearch(input string) { + if _, err := fmt.Fprintln(os.Stderr, fmtRandomSearchOut(input)); err != nil { + fmt.Fprintf( + os.Stdout, + getRandomSearchLogPrefix(), + "error while printing to stderr: %q\n * original message was: %q", + err, input, + ) + } } func genValsRandomSearch(dimens uint, vals []float64, uniform *distuv.Uniform) { diff --git a/algo/randomSearch_test.go b/algo/randomSearch_test.go index 826e6e0..bf9c893 100644 --- a/algo/randomSearch_test.go +++ b/algo/randomSearch_test.go @@ -15,7 +15,5 @@ func TestFmtRandomSearchOut(t *testing.T) { } func TestPrintRandomSearch(t *testing.T) { - if err := printRandomSearch("whatever"); err != nil { - t.Errorf("error while printing to stderr: %q", err) - } + printRandomSearch("whatever") } diff --git a/algo/stochasticHillClimbing.go b/algo/stochasticHillClimbing.go index 2c6912a..a263d0a 100644 --- a/algo/stochasticHillClimbing.go +++ b/algo/stochasticHillClimbing.go @@ -16,8 +16,13 @@ func fmtSHCOut(input string) string { return getSHCLogPrefix() + " " + input } -func printSHC(input string) error { - _, err := fmt.Fprintln(os.Stderr, fmtSHCOut(input)) - - return err +func printSHC(input string) { + if _, err := fmt.Fprintln(os.Stderr, fmtSHCOut(input)); err != nil { + fmt.Fprintf( + os.Stdout, + getSHCLogPrefix(), + "error while printing to stderr: %q\n * original message was: %q", + err, input, + ) + } } diff --git a/algo/stochasticHillClimbing_test.go b/algo/stochasticHillClimbing_test.go index 4ab1f35..846c9f5 100644 --- a/algo/stochasticHillClimbing_test.go +++ b/algo/stochasticHillClimbing_test.go @@ -15,7 +15,5 @@ func TestFmtSHCOut(t *testing.T) { } func TestPrintSHC(t *testing.T) { - if err := printSHC("whatever"); err != nil { - t.Errorf("error while printing to stderr: %q", err) - } + printSHC("whatever") }