diff --git a/internal/runtime/logger.go b/internal/runtime/logger.go index 879c6bc0..b3122acc 100644 --- a/internal/runtime/logger.go +++ b/internal/runtime/logger.go @@ -17,6 +17,7 @@ package runtime import ( + "errors" "fmt" "io" "os" @@ -43,7 +44,7 @@ func NewLogger() *Logger { } // Update constructs a Logger with a preddefined formatter -func (l *Logger) Update(filename string, logLevel string, argv []string) error { +func (l *Logger) Update(filename string, logLevel string, argv []string) { configFromArgs := parseArgs(argv) @@ -61,7 +62,7 @@ func (l *Logger) Update(filename string, logLevel string, argv []string) error { if !configFromArgs.version { configLogFile, err := createLogFile(filename) if err != nil { - return fmt.Errorf("error opening debug log file: %v", err) + argLogFileError = errors.Join(argLogFileError, err) } if configLogFile != nil { logFiles = append(logFiles, configLogFile) @@ -71,7 +72,7 @@ func (l *Logger) Update(filename string, logLevel string, argv []string) error { if argLogFile != nil { logFiles = append(logFiles, argLogFile) } - argLogFileError = err + argLogFileError = errors.Join(argLogFileError, err) } defer func() { if argLogFileError != nil { @@ -119,8 +120,6 @@ func (l *Logger) Update(filename string, logLevel string, argv []string) error { previousLogger: l.Logger, logFiles: logFiles, } - - return nil } // Reset closes the log file (if any) and resets the logger output to what it @@ -157,11 +156,16 @@ func (l *Logger) Reset() error { } func createLogFile(filename string) (*os.File, error) { - if filename != "" && filename != os.DevNull { - return os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if filename == "" || filename == os.DevNull { + return nil, nil } - - return nil, nil + if dir := filepath.Dir(filepath.Clean(filename)); dir != "." { + err := os.MkdirAll(dir, 0755) + if err != nil { + return nil, err + } + } + return os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) } type loggerConfig struct { diff --git a/internal/runtime/runtime.go b/internal/runtime/runtime.go index e659694d..1866ca0c 100644 --- a/internal/runtime/runtime.go +++ b/internal/runtime/runtime.go @@ -44,14 +44,11 @@ func (r rt) Run(argv []string) (rerr error) { if err != nil { return fmt.Errorf("error loading config: %v", err) } - err = r.logger.Update( + r.logger.Update( cfg.NVIDIAContainerRuntimeConfig.DebugFilePath, cfg.NVIDIAContainerRuntimeConfig.LogLevel, argv, ) - if err != nil { - return fmt.Errorf("failed to set up logger: %v", err) - } defer func() { if rerr != nil { r.logger.Errorf("%v", rerr)