Add carriage return to log message when using --tty flag

Signed-off-by: Marco Vedovati <mvedovati@suse.com>

Add a raw text formatter for logrus to be used when terminal is in raw
mode (i.e. when allocating a pseudo-TTY for the container). The raw text
formatter terminates the log messages with the corret \r\n sequence.

Closes: #967
Approved by: rhatdan
This commit is contained in:
Marco Vedovati 2018-06-19 12:28:01 +02:00 committed by Atomic Bot
parent 2d0d1c4b5f
commit f228cf73e0
1 changed files with 23 additions and 2 deletions

View File

@ -14,6 +14,9 @@ import (
"k8s.io/client-go/tools/remotecommand" "k8s.io/client-go/tools/remotecommand"
) )
type RawTtyFormatter struct {
}
// Attach to a container // Attach to a container
func attachCtr(ctr *libpod.Container, stdout, stderr, stdin *os.File, detachKeys string, sigProxy bool) error { func attachCtr(ctr *libpod.Container, stdout, stderr, stdin *os.File, detachKeys string, sigProxy bool) error {
resize := make(chan remotecommand.TerminalSize) resize := make(chan remotecommand.TerminalSize)
@ -37,9 +40,10 @@ func attachCtr(ctr *libpod.Container, stdout, stderr, stdin *os.File, detachKeys
return errors.Wrapf(err, "unable to save terminal state") return errors.Wrapf(err, "unable to save terminal state")
} }
logrus.SetFormatter(&RawTtyFormatter{})
term.SetRawTerminal(os.Stdin.Fd()) term.SetRawTerminal(os.Stdin.Fd())
defer term.RestoreTerminal(os.Stdin.Fd(), oldTermState) defer restoreTerminal(oldTermState)
} }
streams := new(libpod.AttachStreams) streams := new(libpod.AttachStreams)
@ -93,9 +97,10 @@ func startAttachCtr(ctr *libpod.Container, stdout, stderr, stdin *os.File, detac
return errors.Wrapf(err, "unable to save terminal state") return errors.Wrapf(err, "unable to save terminal state")
} }
logrus.SetFormatter(&RawTtyFormatter{})
term.SetRawTerminal(os.Stdin.Fd()) term.SetRawTerminal(os.Stdin.Fd())
defer term.RestoreTerminal(os.Stdin.Fd(), oldTermState) defer restoreTerminal(oldTermState)
} }
streams := new(libpod.AttachStreams) streams := new(libpod.AttachStreams)
@ -171,3 +176,19 @@ func resizeTty(resize chan remotecommand.TerminalSize, resizeTerminate chan inte
} }
}() }()
} }
func restoreTerminal(state *term.State) error {
logrus.SetFormatter(&logrus.TextFormatter{})
return term.RestoreTerminal(os.Stdin.Fd(), state)
}
func (f *RawTtyFormatter) Format(entry *logrus.Entry) ([]byte, error) {
textFormatter := logrus.TextFormatter{}
bytes, err := textFormatter.Format(entry)
if err == nil {
bytes = append(bytes, '\r')
}
return bytes, err
}