提交 3d301974 编写于 作者: E Evan Lezar

Capture returned errors

This change ensures that an error is outputed to the log
before it is closed and ensures that the closed log is not
used as the output.
Signed-off-by: NEvan Lezar <elezar@nvidia.com>
上级 ba94ed18
......@@ -27,8 +27,8 @@ import (
type Logger struct {
*logrus.Logger
previouOutput io.Writer
logFile *os.File
previousOutput io.Writer
logFile *os.File
}
func NewLogger() *Logger {
......@@ -54,7 +54,7 @@ func (l *Logger) LogToFile(filename string) error {
}
l.logFile = logFile
l.previouOutput = l.Out
l.previousOutput = l.Out
l.SetOutput(logFile)
return nil
......@@ -65,7 +65,7 @@ func (l *Logger) CloseFile() error {
return nil
}
logFile := l.logFile
l.SetOutput(l.previouOutput)
l.SetOutput(l.previousOutput)
l.logFile = nil
return logFile.Close()
......
......@@ -174,14 +174,16 @@ func addNVIDIAHook(spec *specs.Spec) error {
}
func main() {
err := run()
err := run(os.Args)
if err != nil {
logger.Errorf("Error running %v: %v", os.Args, err)
os.Exit(1)
}
}
func run() error {
// run is an entry point that allows for idiomatic handling of errors
// when calling from the main function.
func run(argv []string) (err error) {
cfg, err := getConfig()
if err != nil {
return fmt.Errorf("error loading config: %v", err)
......@@ -191,10 +193,16 @@ func run() error {
if err != nil {
return fmt.Errorf("error opening debug log file: %v", err)
}
defer logger.CloseFile()
defer func() {
// We capture and log a returning error before closing the log file.
if err != nil {
logger.Errorf("Error running %v: %v", argv, err)
}
logger.CloseFile()
}()
logger.Printf("Running %s\n", os.Args[0])
args, err := getArgs(os.Args)
logger.Printf("Running %s\n", argv[0])
args, err := getArgs(argv)
if err != nil {
return fmt.Errorf("error getting processing command line arguments: %v", err)
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册