a7dc7c01c0f5891a234d87f06988ef26c5df4279 — mural 5 days ago b331407
app/internal/log: add logger for Windows DebugView

Signed-off-by: mural <mural@ctli.io>
1 files changed, 34 insertions(+), 0 deletions(-)

A app/internal/log/log_windows.go
A app/internal/log/log_windows.go => app/internal/log/log_windows.go +34 -0
@@ 0,0 1,34 @@
+// SPDX-License-Identifier: Unlicense OR MIT
+
+package log
+
+import (
+	"log"
+	"syscall"
+	"unsafe"
+)
+
+type logger struct{}
+
+var (
+	kernel32           = syscall.NewLazyDLL("kernel32")
+	outputDebugStringW = kernel32.NewProc("OutputDebugStringW")
+	debugView          *logger
+)
+
+func init() {
+	// Windows DebugView already includes timestamps.
+	if syscall.Stderr == 0 {
+		log.SetFlags(log.Flags() &^ log.LstdFlags)
+		log.SetOutput(debugView)
+	}
+}
+
+func (l *logger) Write(buf []byte) (int, error) {
+	p, err := syscall.UTF16PtrFromString(string(buf))
+	if err != nil {
+		return 0, err
+	}
+	outputDebugStringW.Call(uintptr(unsafe.Pointer(p)))
+	return len(buf), nil
+}