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)
}
// Hijack fs.LogPrint so that it doesn't corrupt the screen.
if logPrint := fs.LogPrint; !log.Redirected() {
// Hijack fs.LogOutput so that it doesn't corrupt the screen.
if logOutput := fs.LogOutput; !log.Redirected() {
type log struct {
text string
level fs.LogLevel
}
var logs []log
fs.LogPrint = func(level fs.LogLevel, text string) {
fs.LogOutput = func(level fs.LogLevel, text string) {
if len(logs) > 100 {
logs = logs[len(logs)-100:]
}
logs = append(logs, log{level: level, text: text})
}
defer func() {
fs.LogPrint = logPrint
fs.LogOutput = logOutput
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.
func startProgress() func() {
stopStats := make(chan struct{})
oldLogPrint := fs.LogPrint
oldLogOutput := fs.LogOutput
oldSyncPrint := operations.SyncPrintf
if !log.Redirected() {
// 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))
}
@ -60,7 +60,7 @@ func startProgress() func() {
case <-stopStats:
ticker.Stop()
printProgress("")
fs.LogPrint = oldLogPrint
fs.LogOutput = oldLogOutput
operations.SyncPrintf = oldSyncPrint
fmt.Println("")
return

View File

@ -60,8 +60,8 @@ var LogPrintPid = false
// InstallJSONLogger is a hook that --use-json-log calls
var InstallJSONLogger = func(logLevel LogLevel) {}
// LogPrint sends the text to the logger of level
var LogPrint = func(level LogLevel, text string) {
// LogOutput sends the text to the logger of level
var LogOutput = func(level LogLevel, text string) {
text = fmt.Sprintf("%-6s: %s", level, text)
if LogPrintPid {
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 {
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.SetOutput(w)
fs.LogPrint = func(level fs.LogLevel, text string) {
fs.LogOutput = func(level fs.LogLevel, text string) {
switch level {
case fs.LogLevelEmergency:
_ = w.Emerg(text)

View File

@ -26,7 +26,7 @@ func startSystemdLog() bool {
}
log.SetFlags(flags)
// 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)
_ = log.Output(4, text)
}