fs: refactor base log method name for improved consistency

This commit is contained in:
albertony 2024-06-07 12:42:52 +02:00 committed by Nick Craig-Wood
parent dfc2c98bbf
commit 4a54cc134f
5 changed files with 13 additions and 13 deletions

View File

@ -929,23 +929,23 @@ func (u *UI) Run() error {
return fmt.Errorf("screen init: %w", err) return fmt.Errorf("screen init: %w", err)
} }
// Hijack fs.LogPrint so that it doesn't corrupt the screen. // Hijack fs.LogOutput so that it doesn't corrupt the screen.
if logPrint := fs.LogPrint; !log.Redirected() { if logOutput := fs.LogOutput; !log.Redirected() {
type log struct { type log struct {
text string text string
level fs.LogLevel level fs.LogLevel
} }
var logs []log var logs []log
fs.LogPrint = func(level fs.LogLevel, text string) { fs.LogOutput = func(level fs.LogLevel, text string) {
if len(logs) > 100 { if len(logs) > 100 {
logs = logs[len(logs)-100:] logs = logs[len(logs)-100:]
} }
logs = append(logs, log{level: level, text: text}) logs = append(logs, log{level: level, text: text})
} }
defer func() { defer func() {
fs.LogPrint = logPrint fs.LogOutput = logOutput
for i := range logs { for i := range logs {
logPrint(logs[i].level, logs[i].text) logOutput(logs[i].level, logs[i].text)
} }
}() }()
} }

View File

@ -28,12 +28,12 @@ const (
// It returns a func which should be called to stop the stats. // It returns a func which should be called to stop the stats.
func startProgress() func() { func startProgress() func() {
stopStats := make(chan struct{}) stopStats := make(chan struct{})
oldLogPrint := fs.LogPrint oldLogOutput := fs.LogOutput
oldSyncPrint := operations.SyncPrintf oldSyncPrint := operations.SyncPrintf
if !log.Redirected() { if !log.Redirected() {
// Intercept the log calls if not logging to file or syslog // Intercept the log calls if not logging to file or syslog
fs.LogPrint = func(level fs.LogLevel, text string) { fs.LogOutput = func(level fs.LogLevel, text string) {
printProgress(fmt.Sprintf("%s %-6s: %s", time.Now().Format(logTimeFormat), level, text)) printProgress(fmt.Sprintf("%s %-6s: %s", time.Now().Format(logTimeFormat), level, text))
} }
@ -60,7 +60,7 @@ func startProgress() func() {
case <-stopStats: case <-stopStats:
ticker.Stop() ticker.Stop()
printProgress("") printProgress("")
fs.LogPrint = oldLogPrint fs.LogOutput = oldLogOutput
operations.SyncPrintf = oldSyncPrint operations.SyncPrintf = oldSyncPrint
fmt.Println("") fmt.Println("")
return return

View File

@ -60,8 +60,8 @@ var LogPrintPid = false
// InstallJSONLogger is a hook that --use-json-log calls // InstallJSONLogger is a hook that --use-json-log calls
var InstallJSONLogger = func(logLevel LogLevel) {} var InstallJSONLogger = func(logLevel LogLevel) {}
// LogPrint sends the text to the logger of level // LogOutput sends the text to the logger of level
var LogPrint = func(level LogLevel, text string) { var LogOutput = func(level LogLevel, text string) {
text = fmt.Sprintf("%-6s: %s", level, text) text = fmt.Sprintf("%-6s: %s", level, text)
if LogPrintPid { if LogPrintPid {
text = fmt.Sprintf("[%d] %s", os.Getpid(), text) text = fmt.Sprintf("[%d] %s", os.Getpid(), text)
@ -143,7 +143,7 @@ func LogPrintf(level LogLevel, o interface{}, text string, args ...interface{})
if o != nil { if o != nil {
out = fmt.Sprintf("%v: %s", o, out) out = fmt.Sprintf("%v: %s", o, out)
} }
LogPrint(level, out) LogOutput(level, out)
} }
} }

View File

@ -51,7 +51,7 @@ func startSysLog() bool {
} }
log.SetFlags(0) log.SetFlags(0)
log.SetOutput(w) log.SetOutput(w)
fs.LogPrint = func(level fs.LogLevel, text string) { fs.LogOutput = func(level fs.LogLevel, text string) {
switch level { switch level {
case fs.LogLevelEmergency: case fs.LogLevelEmergency:
_ = w.Emerg(text) _ = w.Emerg(text)

View File

@ -26,7 +26,7 @@ func startSystemdLog() bool {
} }
log.SetFlags(flags) log.SetFlags(flags)
// TODO: Use the native journal.Print approach rather than a custom implementation // TODO: Use the native journal.Print approach rather than a custom implementation
fs.LogPrint = func(level fs.LogLevel, text string) { fs.LogOutput = func(level fs.LogLevel, text string) {
text = fmt.Sprintf("<%s>%-6s: %s", systemdLogPrefix(level), level, text) text = fmt.Sprintf("<%s>%-6s: %s", systemdLogPrefix(level), level, text)
_ = log.Output(4, text) _ = log.Output(4, text)
} }