From 1c263a7d5e114583972e54b32464044b72d568fb Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Thu, 26 Apr 2018 15:23:23 -0400 Subject: [PATCH] Add LogFile as a cio IO option Signed-off-by: Michael Crosby --- cio/io.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/cio/io.go b/cio/io.go index a49c11735..2f9b5f103 100644 --- a/cio/io.go +++ b/cio/io.go @@ -21,6 +21,7 @@ import ( "fmt" "io" "os" + "path/filepath" "sync" "github.com/containerd/containerd/defaults" @@ -213,3 +214,44 @@ type DirectIO struct { } var _ IO = &DirectIO{} + +// LogFile creates a file on disk that logs the task's STDOUT,STDERR. +// If the log file already exists, the logs will be appended to the file. +func LogFile(path string) Creator { + return func(_ string) (IO, error) { + if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil { + return nil, err + } + f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + return nil, err + } + f.Close() + return &logIO{ + config: Config{ + Stdout: path, + Stderr: path, + }, + }, nil + } +} + +type logIO struct { + config Config +} + +func (l *logIO) Config() Config { + return l.config +} + +func (l *logIO) Cancel() { + +} + +func (l *logIO) Wait() { + +} + +func (l *logIO) Close() error { + return nil +}