diff --git a/internal/atlas/image.go b/internal/atlas/image.go index cf21e3dfd..da6943789 100644 --- a/internal/atlas/image.go +++ b/internal/atlas/image.go @@ -866,7 +866,7 @@ func SwapBuffers(graphicsDriver graphicsdriver.Graphics) error { }() if debug.IsDebug { - debug.Logf("Internal image sizes:\n") + debug.FrameLogf("Internal image sizes:\n") imgs := make([]*graphicscommand.Image, 0, len(theBackends)) for _, backend := range theBackends { imgs = append(imgs, backend.image) diff --git a/internal/debug/debug.go b/internal/debug/debug.go index 93cd8d1d6..f0c6e1d60 100644 --- a/internal/debug/debug.go +++ b/internal/debug/debug.go @@ -14,7 +14,8 @@ package debug -type Logger interface { - Logf(format string, args ...any) +// FrameLogger defines the interface for logging debug information for each frame. +type FrameLogger interface { + FrameLogf(format string, args ...any) Flush() } diff --git a/internal/debug/debug_ebitenginedebug.go b/internal/debug/debug_ebitenginedebug.go index 8f6ae144d..853481ee0 100644 --- a/internal/debug/debug_ebitenginedebug.go +++ b/internal/debug/debug_ebitenginedebug.go @@ -23,30 +23,32 @@ import ( const IsDebug = true -var theLogger = &logger{} +var theFrameLogger = &frameLogger{} var flushM sync.Mutex -// Logf calls the current global logger's Logf. -// Logf buffers the arguments and doesn't dump the log immediately. +// FrameLogf calls the current global logger's FrameLogf. +// FrameLogf buffers the arguments and doesn't dump the log immediately. // You can dump logs by calling SwitchLogger and Flush. // -// Logf is not concurrent safe. -func Logf(format string, args ...any) { - theLogger.Logf(format, args...) +// FrameLogf is not concurrent safe. +// FrameLogf and SwitchFrameLogger must be called from the same goroutine. +func FrameLogf(format string, args ...any) { + theFrameLogger.FrameLogf(format, args...) } -// SwitchLogger sets a new logger as the current logger and returns the original global logger. +// SwitchFrameLogger sets a new logger as the current logger and returns the original global logger. // The new global logger and the returned logger have separate statuses, so you can use them for different goroutines. // -// SwitchLogger and a returned Logger are not concurrent safe. -func SwitchLogger() Logger { - current := theLogger - theLogger = &logger{} +// SwitchFrameLogger and a returned Logger are not concurrent safe. +// FrameLogf and SwitchFrameLogger must be called from the same goroutine. +func SwitchFrameLogger() FrameLogger { + current := theFrameLogger + theFrameLogger = &frameLogger{} return current } -type logger struct { +type frameLogger struct { items []logItem } @@ -55,14 +57,14 @@ type logItem struct { args []any } -func (l *logger) Logf(format string, args ...any) { +func (l *frameLogger) FrameLogf(format string, args ...any) { l.items = append(l.items, logItem{ format: format, args: args, }) } -func (l *logger) Flush() { +func (l *frameLogger) Flush() { // Flushing is protected by a mutex not to mix another logger's logs. flushM.Lock() defer flushM.Unlock() diff --git a/internal/debug/debug_notebitenginedebug.go b/internal/debug/debug_notebitenginedebug.go index 7f7c7509d..e8fc57030 100644 --- a/internal/debug/debug_notebitenginedebug.go +++ b/internal/debug/debug_notebitenginedebug.go @@ -18,17 +18,17 @@ package debug const IsDebug = false -func Logf(format string, args ...any) { +func FrameLogf(format string, args ...any) { } -func SwitchLogger() Logger { - return dummyLogger{} +func SwitchFrameLogger() FrameLogger { + return dummyFrameLogger{} } -type dummyLogger struct{} +type dummyFrameLogger struct{} -func (dummyLogger) Logf(format string, args ...any) { +func (dummyFrameLogger) FrameLogf(format string, args ...any) { } -func (dummyLogger) Flush() { +func (dummyFrameLogger) Flush() { } diff --git a/internal/graphicscommand/commandqueue.go b/internal/graphicscommand/commandqueue.go index 0f24dbf42..7176cb240 100644 --- a/internal/graphicscommand/commandqueue.go +++ b/internal/graphicscommand/commandqueue.go @@ -197,7 +197,7 @@ func (q *commandQueue) Flush(graphicsDriver graphicsdriver.Graphics, endFrame bo } } - logger := debug.SwitchLogger() + logger := debug.SwitchFrameLogger() var flushErr error runOnRenderThread(func() { @@ -223,7 +223,7 @@ func (q *commandQueue) Flush(graphicsDriver graphicsdriver.Graphics, endFrame bo } // flush must be called the render thread. -func (q *commandQueue) flush(graphicsDriver graphicsdriver.Graphics, endFrame bool, logger debug.Logger) (err error) { +func (q *commandQueue) flush(graphicsDriver graphicsdriver.Graphics, endFrame bool, logger debug.FrameLogger) (err error) { // If endFrame is true, Begin/End should be called to ensure the framebuffer is swapped. if len(q.commands) == 0 && !endFrame { return nil @@ -231,7 +231,7 @@ func (q *commandQueue) flush(graphicsDriver graphicsdriver.Graphics, endFrame bo es := q.indices vs := q.vertices - logger.Logf("Graphics commands:\n") + logger.FrameLogf("Graphics commands:\n") if err := graphicsDriver.Begin(); err != nil { return err @@ -294,7 +294,7 @@ func (q *commandQueue) flush(graphicsDriver graphicsdriver.Graphics, endFrame bo if err := c.Exec(q, graphicsDriver, indexOffset); err != nil { return err } - logger.Logf(" %s\n", c) + logger.FrameLogf(" %s\n", c) // TODO: indexOffset should be reset if the command type is different // from the previous one. This fix is needed when another drawing command is // introduced than drawTrianglesCommand. diff --git a/internal/graphicscommand/image.go b/internal/graphicscommand/image.go index be0eecf28..87ff76eff 100644 --- a/internal/graphicscommand/image.go +++ b/internal/graphicscommand/image.go @@ -234,6 +234,6 @@ func LogImagesInfo(images []*Image) { if i.screen { screen = " (screen)" } - debug.Logf(" %d: (%d, %d)%s\n", i.id, w, h, screen) + debug.FrameLogf(" %d: (%d, %d)%s\n", i.id, w, h, screen) } } diff --git a/internal/ui/context.go b/internal/ui/context.go index f7d2cb607..bf9ccce50 100644 --- a/internal/ui/context.go +++ b/internal/ui/context.go @@ -95,7 +95,7 @@ func (c *context) updateFrameImpl(graphicsDriver graphicsdriver.Graphics, update return nil } - debug.Logf("----\n") + debug.FrameLogf("----\n") if err := atlas.BeginFrame(graphicsDriver); err != nil { return err @@ -133,7 +133,7 @@ func (c *context) updateFrameImpl(graphicsDriver graphicsdriver.Graphics, update updateCount = 1 c.updateCalled = true } - debug.Logf("Update count per frame: %d\n", updateCount) + debug.FrameLogf("Update count per frame: %d\n", updateCount) // Update the game. for i := 0; i < updateCount; i++ {