mirror of
https://github.com/opencontainers/runc.git
synced 2026-07-11 06:03:57 +08:00
9a599f62fb
Add support for children processes logging (including nsexec). A pipe is used to send logs from children to parent in JSON. The JSON format used is the same used by logrus JSON formatted, i.e. children process can use standard logrus APIs. Signed-off-by: Marco Vedovati <mvedovati@suse.com>
71 lines
1.2 KiB
Go
71 lines
1.2 KiB
Go
package libcontainer
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func forwardLogs(p *os.File) {
|
|
defer p.Close()
|
|
|
|
type jsonLog struct {
|
|
Level string `json:"level"`
|
|
Msg string `json:"msg"`
|
|
}
|
|
|
|
dec := json.NewDecoder(p)
|
|
for {
|
|
var jl jsonLog
|
|
|
|
type logFuncTable map[logrus.Level]func(args ...interface{})
|
|
logMapping := logFuncTable{
|
|
logrus.PanicLevel: logrus.Panic,
|
|
logrus.FatalLevel: logrus.Fatal,
|
|
logrus.ErrorLevel: logrus.Error,
|
|
logrus.WarnLevel: logrus.Warn,
|
|
logrus.InfoLevel: logrus.Info,
|
|
logrus.DebugLevel: logrus.Debug,
|
|
}
|
|
|
|
err := dec.Decode(&jl)
|
|
if err != nil {
|
|
if err == io.EOF {
|
|
logrus.Debug("child pipe closed")
|
|
return
|
|
}
|
|
logrus.Errorf("json logs decoding error: %+v", err)
|
|
return
|
|
}
|
|
|
|
lvl, err := logrus.ParseLevel(jl.Level)
|
|
if err != nil {
|
|
fmt.Printf("parsing error\n")
|
|
}
|
|
if logMapping[lvl] != nil {
|
|
logMapping[lvl](jl.Msg)
|
|
}
|
|
}
|
|
}
|
|
|
|
func rawLogs(p *os.File) {
|
|
defer p.Close()
|
|
|
|
data := make([]byte, 128)
|
|
for {
|
|
_, err := p.Read(data)
|
|
|
|
if err != nil {
|
|
if err == io.EOF {
|
|
logrus.Debug("child pipe closed")
|
|
return
|
|
}
|
|
return
|
|
}
|
|
fmt.Printf("Read data: %s\n", string(data))
|
|
}
|
|
}
|