2020-08-24 09:54:34 +03:00
|
|
|
package build
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/btcsuite/btclog"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ShutdownLogger wraps an existing logger with a shutdown function which will
|
|
|
|
// be called on Critical/Criticalf to prompt shutdown.
|
|
|
|
type ShutdownLogger struct {
|
|
|
|
btclog.Logger
|
2020-08-28 12:18:15 +03:00
|
|
|
shutdown func()
|
2020-08-24 09:54:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewShutdownLogger creates a shutdown logger for the log provided which will
|
|
|
|
// use the signal package to request shutdown on critical errors.
|
2020-08-28 12:18:15 +03:00
|
|
|
func NewShutdownLogger(logger btclog.Logger, shutdown func()) *ShutdownLogger {
|
2020-08-24 09:54:34 +03:00
|
|
|
return &ShutdownLogger{
|
2020-08-28 12:18:15 +03:00
|
|
|
Logger: logger,
|
|
|
|
shutdown: shutdown,
|
2020-08-24 09:54:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Criticalf formats message according to format specifier and writes to
|
|
|
|
// log with LevelCritical. It will then call the shutdown logger's shutdown
|
|
|
|
// function to prompt safe shutdown.
|
|
|
|
//
|
|
|
|
// Note: it is part of the btclog.Logger interface.
|
|
|
|
func (s *ShutdownLogger) Criticalf(format string, params ...interface{}) {
|
|
|
|
s.Logger.Criticalf(format, params...)
|
2020-08-28 12:18:15 +03:00
|
|
|
s.Logger.Info("Sending request for shutdown")
|
2020-08-24 09:54:34 +03:00
|
|
|
s.shutdown()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Critical formats message using the default formats for its operands
|
|
|
|
// and writes to log with LevelCritical. It will then call the shutdown
|
|
|
|
// logger's shutdown function to prompt safe shutdown.
|
|
|
|
//
|
|
|
|
// Note: it is part of the btclog.Logger interface.
|
|
|
|
func (s *ShutdownLogger) Critical(v ...interface{}) {
|
|
|
|
s.Logger.Critical(v)
|
|
|
|
s.Logger.Info("Sending request for shutdown")
|
2020-08-28 12:18:15 +03:00
|
|
|
s.shutdown()
|
2020-08-24 09:54:34 +03:00
|
|
|
}
|