Added a logging system to show home much time it

took to do a certain part of the program
This commit is contained in:
Karma Riuk 2023-08-10 19:38:43 +02:00
parent fb59c890ca
commit a80e2c9cc3
6 changed files with 22 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import (
"image" "image"
"image/color" "image/color"
"image/png" "image/png"
"maze-solver/utils"
"os" "os"
"golang.org/x/image/draw" "golang.org/x/image/draw"
@ -16,6 +17,7 @@ type ImageReader struct {
} }
func (r *ImageReader) Read() (*RawMaze, error) { func (r *ImageReader) Read() (*RawMaze, error) {
defer utils.Timer("Image reader", 3)()
image, err := r.getShrunkImage() image, err := r.getShrunkImage()
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -11,6 +11,7 @@ type StringsReader struct {
} }
func (r *StringsReader) Read() (*RawMaze, error) { func (r *StringsReader) Read() (*RawMaze, error) {
defer utils.Timer("Strings Reader", 3)()
width, height := len((*r.Lines)[0]), len(*r.Lines) width, height := len((*r.Lines)[0]), len(*r.Lines)
ret := &RawMaze{ ret := &RawMaze{
Width: width, Width: width,

View File

@ -2,6 +2,7 @@ package reader
import ( import (
"bufio" "bufio"
"maze-solver/utils"
"os" "os"
) )
@ -11,6 +12,7 @@ type TextReader struct {
} }
func (r TextReader) Read() (*RawMaze, error) { func (r TextReader) Read() (*RawMaze, error) {
defer utils.Timer("Text Reader", 3)()
lines, err := getLines(r.Filename) lines, err := getLines(r.Filename)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -7,6 +7,7 @@ import (
"image/draw" "image/draw"
"image/png" "image/png"
"maze-solver/maze" "maze-solver/maze"
"maze-solver/utils"
"os" "os"
"github.com/mazznoer/colorgrad" "github.com/mazznoer/colorgrad"
@ -22,6 +23,7 @@ type ImageWriter struct {
} }
func (w *ImageWriter) Write() error { func (w *ImageWriter) Write() error {
defer utils.Timer("Image writer", 3)()
if w.Filename[len(w.Filename)-4:] != ".png" { 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") return errors.New("Filename of ImageWriter doesn't have .png extension. The only suppported image type is png")
} }

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"maze-solver/maze" "maze-solver/maze"
"maze-solver/utils"
) )
type StringsWriter struct { type StringsWriter struct {
@ -14,6 +15,7 @@ type StringsWriter struct {
} }
func (w *StringsWriter) Write() error { func (w *StringsWriter) Write() error {
defer utils.Timer("Strings writer", 3)()
w.lines = make([][]byte, w.Maze.Height) w.lines = make([][]byte, w.Maze.Height)
// Fill the lines with walls // Fill the lines with walls

View File

@ -1,8 +1,10 @@
package utils package utils
import ( import (
"fmt"
"log" "log"
"testing" "testing"
"time"
"golang.org/x/exp/constraints" "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...) 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))
}
}
}