From a80e2c9cc38c147d2397331bd487e9c06e32ea20 Mon Sep 17 00:00:00 2001 From: Karma Riuk Date: Thu, 10 Aug 2023 19:38:43 +0200 Subject: [PATCH] Added a logging system to show home much time it took to do a certain part of the program --- io/reader/image.go | 2 ++ io/reader/strings.go | 1 + io/reader/text.go | 2 ++ io/writer/image.go | 2 ++ io/writer/strings.go | 2 ++ utils/utils.go | 13 +++++++++++++ 6 files changed, 22 insertions(+) diff --git a/io/reader/image.go b/io/reader/image.go index d02e879..c1ef5c4 100644 --- a/io/reader/image.go +++ b/io/reader/image.go @@ -4,6 +4,7 @@ import ( "image" "image/color" "image/png" + "maze-solver/utils" "os" "golang.org/x/image/draw" @@ -16,6 +17,7 @@ type ImageReader struct { } func (r *ImageReader) Read() (*RawMaze, error) { + defer utils.Timer("Image reader", 3)() image, err := r.getShrunkImage() if err != nil { return nil, err diff --git a/io/reader/strings.go b/io/reader/strings.go index d6b2fed..c548dd5 100644 --- a/io/reader/strings.go +++ b/io/reader/strings.go @@ -11,6 +11,7 @@ type StringsReader struct { } func (r *StringsReader) Read() (*RawMaze, error) { + defer utils.Timer("Strings Reader", 3)() width, height := len((*r.Lines)[0]), len(*r.Lines) ret := &RawMaze{ Width: width, diff --git a/io/reader/text.go b/io/reader/text.go index 6907088..70e244d 100644 --- a/io/reader/text.go +++ b/io/reader/text.go @@ -2,6 +2,7 @@ package reader import ( "bufio" + "maze-solver/utils" "os" ) @@ -11,6 +12,7 @@ type TextReader struct { } func (r TextReader) Read() (*RawMaze, error) { + defer utils.Timer("Text Reader", 3)() lines, err := getLines(r.Filename) if err != nil { return nil, err diff --git a/io/writer/image.go b/io/writer/image.go index e79f15f..2bdace9 100644 --- a/io/writer/image.go +++ b/io/writer/image.go @@ -7,6 +7,7 @@ import ( "image/draw" "image/png" "maze-solver/maze" + "maze-solver/utils" "os" "github.com/mazznoer/colorgrad" @@ -22,6 +23,7 @@ type ImageWriter struct { } func (w *ImageWriter) Write() error { + defer utils.Timer("Image writer", 3)() if w.Filename[len(w.Filename)-4:] != ".png" { return errors.New("Filename of ImageWriter doesn't have .png extension. The only suppported image type is png") } diff --git a/io/writer/strings.go b/io/writer/strings.go index 6929072..5aa16bf 100644 --- a/io/writer/strings.go +++ b/io/writer/strings.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "maze-solver/maze" + "maze-solver/utils" ) type StringsWriter struct { @@ -14,6 +15,7 @@ type StringsWriter struct { } func (w *StringsWriter) Write() error { + defer utils.Timer("Strings writer", 3)() w.lines = make([][]byte, w.Maze.Height) // Fill the lines with walls diff --git a/utils/utils.go b/utils/utils.go index 3ce6c66..b26edbf 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -1,8 +1,10 @@ package utils import ( + "fmt" "log" "testing" + "time" "golang.org/x/exp/constraints" ) @@ -27,3 +29,14 @@ func AssertEqual[T comparable](t *testing.T, got T, want T, msg string, args ... t.Fatalf(msg+"\nGot: %v, Want: %v", args...) } } + +var VERBOSE_LEVEL int + +func Timer(msg string, level int) func() { + start := time.Now() + return func() { + if level <= VERBOSE_LEVEL { + fmt.Printf("%-19s %12v\n", msg, time.Since(start)) + } + } +}