fix(tui): log image debug (#1316)

resolvicomai created

## What?
- route image protocol debug messages through the standard logger
instead of writing directly to stdout
- add coverage that verifies debug output goes to the logger and leaves
stdout untouched

## Why?
Debug output should respect the application logging path instead of
bypassing logger formatting and destinations.

Fixes #701

Change summary

view/html.go      |  3 ++-
view/html_test.go | 26 ++++++++++++++++++++++++++
2 files changed, 28 insertions(+), 1 deletion(-)

Detailed changes

view/html.go 🔗

@@ -14,6 +14,7 @@ import (
 	"charm.land/lipgloss/v2"
 	"github.com/floatpane/matcha/clib"
 	"github.com/floatpane/matcha/internal/httpclient"
+	"github.com/floatpane/matcha/internal/loglevel"
 	"github.com/floatpane/matcha/theme"
 	lru "github.com/hashicorp/golang-lru/v2"
 )
@@ -273,7 +274,7 @@ func debugImageProtocol(format string, args ...interface{}) {
 		return
 	}
 	msg := fmt.Sprintf("[img-protocol] "+format+"\n", args...)
-	fmt.Print(msg)
+	loglevel.Infof("%s", strings.TrimSuffix(msg, "\n"))
 	if path := os.Getenv("DEBUG_IMAGE_PROTOCOL_LOG"); path != "" {
 		if f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err == nil {
 			_, _ = f.WriteString(msg)

view/html_test.go 🔗

@@ -1,7 +1,9 @@
 package view
 
 import (
+	"bytes"
 	"fmt"
+	"log"
 	"os"
 	"regexp"
 	"strings"
@@ -68,6 +70,30 @@ func TestDecodeQuotedPrintable(t *testing.T) {
 	}
 }
 
+func TestDebugImageProtocolUsesLogger(t *testing.T) {
+	t.Setenv("DEBUG_IMAGE_PROTOCOL", "1")
+	t.Setenv("DEBUG_IMAGE_PROTOCOL_LOG", "")
+	t.Setenv("DEBUG_KITTY_IMAGES", "")
+	t.Setenv("DEBUG_KITTY_LOG", "")
+
+	var logBuf bytes.Buffer
+	originalLogOutput := log.Writer()
+	originalLogFlags := log.Flags()
+	log.SetOutput(&logBuf)
+	log.SetFlags(0)
+	t.Cleanup(func() {
+		log.SetOutput(originalLogOutput)
+		log.SetFlags(originalLogFlags)
+	})
+
+	debugImageProtocol("hello %s", "world")
+
+	want := "[img-protocol] hello world\n"
+	if got := logBuf.String(); got != want {
+		t.Fatalf("debugImageProtocol log output = %q, want %q", got, want)
+	}
+}
+
 func TestMarkdownToHTML(t *testing.T) {
 	testCases := []struct {
 		name     string