46 lines
1.1 KiB
Go
46 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"sync"
|
|
)
|
|
|
|
// debugLog is a process-wide append-only log that gitea-mcp writes to
|
|
// when SHERLOCK_DEBUG_LOG is set in the environment. Useful because
|
|
// the agent (Copilot, Claude Code) owns stdin/stdout/stderr of the
|
|
// MCP subprocess — there's no terminal we can println to during a
|
|
// failing tool call.
|
|
//
|
|
// SHERLOCK_DEBUG_LOG=/tmp/gitea-mcp.log
|
|
//
|
|
// gitea-mcp will tee one line per HTTP request (method, URL, status,
|
|
// response body on error) to that file. Also dumps the OAuth token
|
|
// response on the first auth.
|
|
//
|
|
// Empty (default): no logging, no perf cost, no file touched.
|
|
var (
|
|
dbgOnce sync.Once
|
|
dbgLog *log.Logger
|
|
)
|
|
|
|
func dbg(format string, args ...any) {
|
|
dbgOnce.Do(func() {
|
|
path := os.Getenv("SHERLOCK_DEBUG_LOG")
|
|
if path == "" {
|
|
return
|
|
}
|
|
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o600)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "gitea-mcp: SHERLOCK_DEBUG_LOG=%q open: %v\n", path, err)
|
|
return
|
|
}
|
|
dbgLog = log.New(f, "gitea-mcp ", log.LstdFlags|log.Lmicroseconds)
|
|
})
|
|
if dbgLog == nil {
|
|
return
|
|
}
|
|
dbgLog.Printf(format, args...)
|
|
}
|