From e68d86e1bd47f7905e4452f7ce0e04e683561f85 Mon Sep 17 00:00:00 2001 From: Fabricio Voznika Date: Thu, 11 Oct 2018 14:28:15 -0700 Subject: Make debug log file name configurable This is a breaking change if you're using --debug-log-dir. The fix is to replace it with --debug-log and add a '/' at the end: --debug-log-dir=/tmp/runsc ==> --debug-log=/tmp/runsc/ PiperOrigin-RevId: 216761212 Change-Id: I244270a0a522298c48115719fa08dad55e34ade1 --- runsc/boot/config.go | 7 +++---- runsc/main.go | 19 ++++++++----------- runsc/sandbox/sandbox.go | 6 +++--- runsc/specutils/specutils.go | 25 +++++++++++++++++++------ runsc/test/install.sh | 2 +- 5 files changed, 34 insertions(+), 25 deletions(-) (limited to 'runsc') diff --git a/runsc/boot/config.go b/runsc/boot/config.go index cd977c8a5..41af084b9 100644 --- a/runsc/boot/config.go +++ b/runsc/boot/config.go @@ -160,9 +160,8 @@ type Config struct { // LogFormat is the log format, "text" or "json". LogFormat string - // DebugLogDir is the directory to log debug information to, if not - // empty. - DebugLogDir string + // DebugLog is the path to log debug information to, if not empty. + DebugLog string // FileAccess indicates how the filesystem is accessed. FileAccess FileAccessType @@ -217,7 +216,7 @@ func (c *Config) ToFlags() []string { "--debug=" + strconv.FormatBool(c.Debug), "--log=" + c.LogFilename, "--log-format=" + c.LogFormat, - "--debug-log-dir=" + c.DebugLogDir, + "--debug-log=" + c.DebugLog, "--file-access=" + c.FileAccess.String(), "--overlay=" + strconv.FormatBool(c.Overlay), "--network=" + c.Network.String(), diff --git a/runsc/main.go b/runsc/main.go index 16d30f7a0..27aec1cd9 100644 --- a/runsc/main.go +++ b/runsc/main.go @@ -45,10 +45,10 @@ var ( // system that are not covered by the runtime spec. // Debugging flags. - debugLogDir = flag.String("debug-log-dir", "", "additional location for logs. It creates individual log files per command") - logPackets = flag.Bool("log-packets", false, "enable network packet logging") - logFD = flag.Int("log-fd", -1, "file descriptor to log to. If set, the 'log' flag is ignored.") - debugLogFD = flag.Int("debug-log-fd", -1, "file descriptor to write debug logs to. If set, the 'debug-log-dir' flag is ignored.") + debugLog = flag.String("debug-log", "", "additional location for logs. If it ends with '/', log files are created inside the directory with default names. The following variables are available: %TIMESTAMP%, %COMMAND%.") + logPackets = flag.Bool("log-packets", false, "enable network packet logging") + logFD = flag.Int("log-fd", -1, "file descriptor to log to. If set, the 'log' flag is ignored.") + debugLogFD = flag.Int("debug-log-fd", -1, "file descriptor to write debug logs to. If set, the 'debug-log-dir' flag is ignored.") // Debugging flags: strace related strace = flag.Bool("strace", false, "enable strace") @@ -131,7 +131,7 @@ func main() { Debug: *debug, LogFilename: *logFilename, LogFormat: *logFormat, - DebugLogDir: *debugLogDir, + DebugLog: *debugLog, FileAccess: fsAccess, Overlay: *overlay, Network: netType, @@ -195,13 +195,10 @@ func main() { } e = log.MultiEmitter{e, log.GoogleEmitter{&log.Writer{Next: f}}} - } else if *debugLogDir != "" { - if err := os.MkdirAll(*debugLogDir, 0775); err != nil { - cmd.Fatalf("error creating dir %q: %v", *debugLogDir, err) - } - f, err := specutils.DebugLogFile(*debugLogDir, subcommand) + } else if *debugLog != "" { + f, err := specutils.DebugLogFile(*debugLog, subcommand) if err != nil { - cmd.Fatalf("error opening debug log file in %q: %v", *debugLogDir, err) + cmd.Fatalf("error opening debug log file in %q: %v", *debugLog, err) } e = log.MultiEmitter{e, log.GoogleEmitter{&log.Writer{Next: f}}} } diff --git a/runsc/sandbox/sandbox.go b/runsc/sandbox/sandbox.go index 37a3efd09..a0de4a175 100644 --- a/runsc/sandbox/sandbox.go +++ b/runsc/sandbox/sandbox.go @@ -291,10 +291,10 @@ func (s *Sandbox) createSandboxProcess(spec *specs.Spec, conf *boot.Config, bund cmd.Args = append(cmd.Args, "--log-fd="+strconv.Itoa(nextFD)) nextFD++ } - if conf.DebugLogDir != "" { - debugLogFile, err := specutils.DebugLogFile(conf.DebugLogDir, "boot") + if conf.DebugLog != "" { + debugLogFile, err := specutils.DebugLogFile(conf.DebugLog, "boot") if err != nil { - return fmt.Errorf("error opening debug log file in %q: %v", conf.DebugLogDir, err) + return fmt.Errorf("error opening debug log file in %q: %v", conf.DebugLog, err) } defer debugLogFile.Close() cmd.ExtraFiles = append(cmd.ExtraFiles, debugLogFile) diff --git a/runsc/specutils/specutils.go b/runsc/specutils/specutils.go index ac017ba2d..6b3e52021 100644 --- a/runsc/specutils/specutils.go +++ b/runsc/specutils/specutils.go @@ -351,12 +351,25 @@ func WaitForReady(pid int, timeout time.Duration, ready func() (bool, error)) er return backoff.Retry(op, b) } -// DebugLogFile opens a file in logDir based on the timestamp and subcommand -// for writing. -func DebugLogFile(logDir, subcommand string) (*os.File, error) { - // Format: /runsc.log.. - filename := fmt.Sprintf("runsc.log.%s.%s", time.Now().Format("20060102-150405.000000"), subcommand) - return os.OpenFile(filepath.Join(logDir, filename), os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0664) +// DebugLogFile opens a log file using 'logPattern' as location. If 'logPattern' +// ends with '/', it's used as a directory with default file name. +// 'logPattern' can contain variables that are substitued: +// - %TIMESTAMP%: is replaced with a timestamp using the following format: +// +// - %COMMAND%: is replaced with 'command' +func DebugLogFile(logPattern, command string) (*os.File, error) { + if strings.HasSuffix(logPattern, "/") { + // Default format: /runsc.log.. + logPattern += "runsc.log.%TIMESTAMP%.%COMMAND%" + } + logPattern = strings.Replace(logPattern, "%TIMESTAMP%", time.Now().Format("20060102-150405.000000"), -1) + logPattern = strings.Replace(logPattern, "%COMMAND%", command, -1) + + dir := filepath.Dir(logPattern) + if err := os.MkdirAll(dir, 0775); err != nil { + return nil, fmt.Errorf("error creating dir %q: %v", dir, err) + } + return os.OpenFile(logPattern, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0664) } // Mount creates the mount point and calls Mount with the given flags. diff --git a/runsc/test/install.sh b/runsc/test/install.sh index c110d96f9..c239588d4 100755 --- a/runsc/test/install.sh +++ b/runsc/test/install.sh @@ -75,7 +75,7 @@ if [[ ${uninstall} == 0 ]]; then mkdir -p "${logdir}" sudo -n chmod a+wx "${logdir}" - declare -r args="--debug-log-dir "${logdir}" --debug --strace --log-packets" + declare -r args="--debug-log '${logdir}/' --debug --strace --log-packets" sudo -n "${dockercfg}" runtime-add "${runtime}" "${runsc}" ${args} sudo -n "${dockercfg}" runtime-add "${runtime}"-kvm "${runsc}" --platform=kvm ${args} sudo -n "${dockercfg}" runtime-add "${runtime}"-hostnet "${runsc}" --network=host ${args} -- cgit v1.2.3