Ryanhub - file viewer
filename: util/log.go
branch: main
back to repo
package util

import (
	"fmt"
	"log"
	"os"
	"sync"
)

var std = log.New(os.Stderr, "[assistant] ", log.LstdFlags|log.Lmsgprefix)

var (
	hookMu sync.Mutex
	logHook func(string)
)

// SetLogHook registers an optional callback invoked with the formatted log line
// (after writing to stderr). It is safe to call with nil to clear.
func SetLogHook(fn func(string)) {
	hookMu.Lock()
	defer hookMu.Unlock()
	logHook = fn
}

// Logf writes a line to stderr with the assistant prefix.
func Logf(format string, args ...any) {
	msg := fmt.Sprintf(format, args...)
	std.Output(2, msg)
	hookMu.Lock()
	h := logHook
	hookMu.Unlock()
	if h != nil {
		h(msg)
	}
}