mirror of
https://github.com/Direct-Dev-Ru/go-lcg.git
synced 2026-11-09 17:06:40 +00:00
Исправления в ветке main
This commit is contained in:
43
config/debug.go
Normal file
43
config/debug.go
Normal file
@@ -0,0 +1,43 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user