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 42cc4f8717
commit 144d3c2aed
6 changed files with 22 additions and 0 deletions

View File

@ -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

View File

@ -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,

View File

@ -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

View File

@ -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")
}

View File

@ -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

View File

@ -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))
}
}
}