2021-09-06 11:26:48 +00:00
|
|
|
/*
|
|
|
|
# Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2023-02-07 20:09:53 +00:00
|
|
|
package runtime
|
2021-09-06 11:26:48 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2022-05-05 15:27:36 +00:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2021-09-06 11:26:48 +00:00
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
2023-12-01 01:10:10 +00:00
|
|
|
|
2023-12-04 21:22:18 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/errors"
|
2023-12-01 01:10:10 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
|
2021-09-06 11:26:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Logger adds a way to manage output to a log file to a logrus.Logger
|
|
|
|
type Logger struct {
|
2023-03-22 12:27:43 +00:00
|
|
|
logger.Interface
|
|
|
|
previousLogger logger.Interface
|
2022-05-05 15:27:36 +00:00
|
|
|
logFiles []*os.File
|
2021-09-06 11:26:48 +00:00
|
|
|
}
|
|
|
|
|
2022-05-05 15:27:36 +00:00
|
|
|
// NewLogger creates an empty logger
|
2021-09-06 11:26:48 +00:00
|
|
|
func NewLogger() *Logger {
|
2022-05-05 15:27:36 +00:00
|
|
|
return &Logger{
|
2023-03-22 12:27:43 +00:00
|
|
|
Interface: logrus.New(),
|
2022-05-05 15:27:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 20:09:53 +00:00
|
|
|
// Update constructs a Logger with a preddefined formatter
|
2023-05-25 12:44:00 +00:00
|
|
|
func (l *Logger) Update(filename string, logLevel string, argv []string) {
|
2023-02-16 12:57:12 +00:00
|
|
|
|
2022-05-05 15:27:36 +00:00
|
|
|
configFromArgs := parseArgs(argv)
|
2021-09-06 11:26:48 +00:00
|
|
|
|
2022-05-05 15:27:36 +00:00
|
|
|
level, logLevelError := configFromArgs.getLevel(logLevel)
|
2023-02-16 12:57:12 +00:00
|
|
|
defer func() {
|
|
|
|
if logLevelError != nil {
|
2023-06-06 19:46:38 +00:00
|
|
|
l.Warning(logLevelError)
|
2023-02-16 12:57:12 +00:00
|
|
|
}
|
|
|
|
}()
|
2022-05-05 15:27:36 +00:00
|
|
|
|
|
|
|
var logFiles []*os.File
|
2022-05-12 10:05:55 +00:00
|
|
|
var argLogFileError error
|
2022-05-05 15:27:36 +00:00
|
|
|
|
2022-05-12 10:05:55 +00:00
|
|
|
// We don't create log files if the version argument is supplied
|
|
|
|
if !configFromArgs.version {
|
|
|
|
configLogFile, err := createLogFile(filename)
|
|
|
|
if err != nil {
|
2023-05-25 12:44:00 +00:00
|
|
|
argLogFileError = errors.Join(argLogFileError, err)
|
2022-05-12 10:05:55 +00:00
|
|
|
}
|
|
|
|
if configLogFile != nil {
|
|
|
|
logFiles = append(logFiles, configLogFile)
|
|
|
|
}
|
2021-09-06 11:26:48 +00:00
|
|
|
|
2022-05-12 10:05:55 +00:00
|
|
|
argLogFile, err := createLogFile(configFromArgs.file)
|
|
|
|
if argLogFile != nil {
|
|
|
|
logFiles = append(logFiles, argLogFile)
|
|
|
|
}
|
2023-05-25 12:44:00 +00:00
|
|
|
argLogFileError = errors.Join(argLogFileError, err)
|
2021-09-06 11:26:48 +00:00
|
|
|
}
|
2023-02-16 12:57:12 +00:00
|
|
|
defer func() {
|
|
|
|
if argLogFileError != nil {
|
2023-06-06 19:46:38 +00:00
|
|
|
l.Warningf("Failed to open log file: %v", argLogFileError)
|
2023-02-16 12:57:12 +00:00
|
|
|
}
|
|
|
|
}()
|
2021-09-06 11:26:48 +00:00
|
|
|
|
2023-02-16 12:57:12 +00:00
|
|
|
newLogger := logrus.New()
|
2021-09-06 11:26:48 +00:00
|
|
|
|
2023-02-16 12:57:12 +00:00
|
|
|
newLogger.SetLevel(level)
|
2022-05-05 15:27:36 +00:00
|
|
|
if level == logrus.DebugLevel {
|
|
|
|
logrus.SetReportCaller(true)
|
|
|
|
// Shorten function and file names reported by the logger, by
|
|
|
|
// trimming common "github.com/opencontainers/runc" prefix.
|
|
|
|
// This is only done for text formatter.
|
|
|
|
_, file, _, _ := runtime.Caller(0)
|
|
|
|
prefix := filepath.Dir(file) + "/"
|
|
|
|
logrus.SetFormatter(&logrus.TextFormatter{
|
|
|
|
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
|
|
|
|
function := strings.TrimPrefix(f.Function, prefix) + "()"
|
|
|
|
fileLine := strings.TrimPrefix(f.File, prefix) + ":" + strconv.Itoa(f.Line)
|
|
|
|
return function, fileLine
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2021-09-06 11:26:48 +00:00
|
|
|
|
2022-05-05 15:27:36 +00:00
|
|
|
if configFromArgs.format == "json" {
|
2023-02-16 12:57:12 +00:00
|
|
|
newLogger.SetFormatter(new(logrus.JSONFormatter))
|
2022-05-05 15:27:36 +00:00
|
|
|
}
|
|
|
|
|
2023-08-29 08:12:45 +00:00
|
|
|
switch len(logFiles) {
|
|
|
|
case 0:
|
2023-02-16 12:57:12 +00:00
|
|
|
newLogger.SetOutput(io.Discard)
|
2023-08-29 08:12:45 +00:00
|
|
|
case 1:
|
2023-02-16 12:57:12 +00:00
|
|
|
newLogger.SetOutput(logFiles[0])
|
2023-08-29 08:12:45 +00:00
|
|
|
default:
|
2022-05-12 10:05:55 +00:00
|
|
|
var writers []io.Writer
|
|
|
|
for _, f := range logFiles {
|
|
|
|
writers = append(writers, f)
|
|
|
|
}
|
2023-02-16 12:57:12 +00:00
|
|
|
newLogger.SetOutput(io.MultiWriter(writers...))
|
2022-05-12 10:05:55 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 12:57:12 +00:00
|
|
|
*l = Logger{
|
2023-03-22 12:27:43 +00:00
|
|
|
Interface: newLogger,
|
|
|
|
previousLogger: l.Interface,
|
2023-02-16 12:57:12 +00:00
|
|
|
logFiles: logFiles,
|
2022-05-05 15:27:36 +00:00
|
|
|
}
|
2021-09-06 11:26:48 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 13:42:42 +00:00
|
|
|
// Reset closes the log file (if any) and resets the logger output to what it
|
2022-05-05 15:27:36 +00:00
|
|
|
// was before UpdateLogger was called.
|
2022-05-12 13:42:42 +00:00
|
|
|
func (l *Logger) Reset() error {
|
2022-05-05 15:27:36 +00:00
|
|
|
defer func() {
|
|
|
|
previous := l.previousLogger
|
|
|
|
if previous == nil {
|
|
|
|
previous = logrus.New()
|
|
|
|
}
|
2023-03-22 12:27:43 +00:00
|
|
|
l.Interface = previous
|
2023-02-16 12:57:12 +00:00
|
|
|
l.previousLogger = nil
|
|
|
|
l.logFiles = nil
|
2022-05-05 15:27:36 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
var errs []error
|
|
|
|
for _, f := range l.logFiles {
|
|
|
|
err := f.Close()
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
for _, e := range errs {
|
|
|
|
if err == nil {
|
|
|
|
err = e
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return fmt.Errorf("%v; %w", e, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func createLogFile(filename string) (*os.File, error) {
|
2023-05-25 13:17:00 +00:00
|
|
|
if filename == "" || filename == os.DevNull {
|
|
|
|
return nil, nil
|
2022-05-05 15:27:36 +00:00
|
|
|
}
|
2023-05-25 13:17:00 +00:00
|
|
|
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)
|
2022-05-05 15:27:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type loggerConfig struct {
|
2022-05-12 10:05:55 +00:00
|
|
|
file string
|
|
|
|
format string
|
|
|
|
debug bool
|
|
|
|
version bool
|
2022-05-05 15:27:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c loggerConfig) getLevel(logLevel string) (logrus.Level, error) {
|
|
|
|
if c.debug {
|
|
|
|
return logrus.DebugLevel, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if logLevel, err := logrus.ParseLevel(logLevel); err == nil {
|
|
|
|
return logLevel, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return logrus.InfoLevel, fmt.Errorf("invalid log-level '%v'", logLevel)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Informed by Taken from https://github.com/opencontainers/runc/blob/7fd8b57001f5bfa102e89cb434d96bf71f7c1d35/main.go#L182
|
|
|
|
func parseArgs(args []string) loggerConfig {
|
|
|
|
c := loggerConfig{}
|
|
|
|
|
|
|
|
expected := map[string]*string{
|
|
|
|
"log-format": &c.format,
|
|
|
|
"log": &c.file,
|
|
|
|
}
|
|
|
|
|
|
|
|
found := make(map[string]bool)
|
|
|
|
|
|
|
|
for i := 0; i < len(args); i++ {
|
2022-05-12 10:05:55 +00:00
|
|
|
if len(found) == 4 {
|
2022-05-05 15:27:36 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
param := args[i]
|
|
|
|
|
|
|
|
parts := strings.SplitN(param, "=", 2)
|
2022-05-12 10:05:55 +00:00
|
|
|
trimmed := strings.TrimLeft(parts[0], "-")
|
|
|
|
// If this is not a flag we continue
|
|
|
|
if parts[0] == trimmed {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the version flag
|
|
|
|
if trimmed == "version" {
|
|
|
|
c.version = true
|
|
|
|
found["version"] = true
|
|
|
|
// For the version flag we don't process any other flags
|
2022-05-05 15:27:36 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the debug flag
|
|
|
|
if trimmed == "debug" {
|
|
|
|
c.debug = true
|
|
|
|
found["debug"] = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
destination, exists := expected[trimmed]
|
|
|
|
if !exists {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var value string
|
2023-08-29 08:12:45 +00:00
|
|
|
switch {
|
|
|
|
case len(parts) == 2:
|
2022-05-05 15:27:36 +00:00
|
|
|
value = parts[2]
|
2023-08-29 08:12:45 +00:00
|
|
|
case i+1 < len(args):
|
2022-05-05 15:27:36 +00:00
|
|
|
value = args[i+1]
|
|
|
|
i++
|
2023-08-29 08:12:45 +00:00
|
|
|
default:
|
2022-05-05 15:27:36 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
*destination = value
|
|
|
|
found[trimmed] = true
|
2021-09-06 11:26:48 +00:00
|
|
|
}
|
|
|
|
|
2022-05-05 15:27:36 +00:00
|
|
|
return c
|
2021-09-06 11:26:48 +00:00
|
|
|
}
|