Skip to content

Commit 1c263a7

Browse files
committed
Add LogFile as a cio IO option
Signed-off-by: Michael Crosby <[email protected]>
1 parent c73794f commit 1c263a7

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

cio/io.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"io"
2323
"os"
24+
"path/filepath"
2425
"sync"
2526

2627
"github.com/containerd/containerd/defaults"
@@ -213,3 +214,44 @@ type DirectIO struct {
213214
}
214215

215216
var _ IO = &DirectIO{}
217+
218+
// LogFile creates a file on disk that logs the task's STDOUT,STDERR.
219+
// If the log file already exists, the logs will be appended to the file.
220+
func LogFile(path string) Creator {
221+
return func(_ string) (IO, error) {
222+
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
223+
return nil, err
224+
}
225+
f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
226+
if err != nil {
227+
return nil, err
228+
}
229+
f.Close()
230+
return &logIO{
231+
config: Config{
232+
Stdout: path,
233+
Stderr: path,
234+
},
235+
}, nil
236+
}
237+
}
238+
239+
type logIO struct {
240+
config Config
241+
}
242+
243+
func (l *logIO) Config() Config {
244+
return l.config
245+
}
246+
247+
func (l *logIO) Cancel() {
248+
249+
}
250+
251+
func (l *logIO) Wait() {
252+
253+
}
254+
255+
func (l *logIO) Close() error {
256+
return nil
257+
}

0 commit comments

Comments
 (0)