Files
go-lcg/config/debug.go

44 lines
1.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package config
import (
"fmt"
"os"
"sync"
"time"
)
var (
debugFileMu sync.Mutex
debugFileWarned bool
)
// DebugLogf пишет строку в debug-файл (флаг --debug-file или переменная LCG_DEBUG_FILE),
// если он задан. Файл открывается в режиме дозаписи с правами 0600,
// каждая запись получает отметку времени. Ошибка открытия выводится один раз в stderr.
func DebugLogf(format string, args ...any) {
path := AppConfig.MainFlags.DebugFile
if path == "" {
path = os.Getenv("LCG_DEBUG_FILE")
}
if path == "" {
return
}
line := fmt.Sprintf(format, args...)
debugFileMu.Lock()
defer debugFileMu.Unlock()
file, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
if !debugFileWarned {
debugFileWarned = true
fmt.Fprintf(os.Stderr, "⚠️ Не удалось открыть debug-файл %s: %v\n", path, err)
}
return
}
defer file.Close()
fmt.Fprintf(file, "[%s] %s\n", time.Now().Format("2006-01-02 15:04:05.000"), line)
}