Skip to content

Commit

Permalink
accept Ctrl-C in cri log viewer
Browse files Browse the repository at this point in the history
Fixes: # 1956
Signed-off-by: zhaojizhuang <[email protected]>
  • Loading branch information
zhaojizhuang committed Feb 3, 2023
1 parent b0701ba commit 78ea0d1
Showing 1 changed file with 49 additions and 43 deletions.
92 changes: 49 additions & 43 deletions pkg/logging/cri_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ func viewLogsCRI(lvopts LogViewOptions, stdout, stderr io.Writer, stopChannel ch
return fmt.Errorf("logpath is nil ")
}

return ReadLogs(context.Background(), &lvopts, stdout, stderr)
return ReadLogs(context.Background(), &lvopts, stdout, stderr, stopChannel)
}

// ReadLogs read the container log and redirect into stdout and stderr.
// Note that containerID is only needed when following the log, or else
// just pass in empty string "".
func ReadLogs(ctx context.Context, opts *LogViewOptions, stdout, stderr io.Writer) error {
func ReadLogs(ctx context.Context, opts *LogViewOptions, stdout, stderr io.Writer, stopChannel chan os.Signal) error {
var logPath = opts.LogPath
evaluated, err := filepath.EvalSymlinks(logPath)
if err != nil {
Expand Down Expand Up @@ -113,56 +113,62 @@ func ReadLogs(ctx context.Context, opts *LogViewOptions, stdout, stderr io.Write
writer := newLogWriter(stdout, stderr, opts)
msg := &logMessage{}
for {
if stop || (limitedMode && limitedNum == 0) {
logrus.Debugf("Finished parsing log file, path; %s", logPath)
select {
case <-stopChannel:
logrus.Debugf("received stop signal while reading cri logfile, returning")
return nil
}
l, err := r.ReadBytes(eol[0])
if err != nil {
if err != io.EOF { // This is an real error
return fmt.Errorf("failed to read log file %q: %v", logPath, err)
default:
if stop || (limitedMode && limitedNum == 0) {
logrus.Debugf("Finished parsing log file, path; %s", logPath)
return nil
}
if opts.Follow {
l, err := r.ReadBytes(eol[0])
if err != nil {
if err != io.EOF { // This is an real error
return fmt.Errorf("failed to read log file %q: %v", logPath, err)
}
if opts.Follow {

// Reset seek so that if this is an incomplete line,
// it will be read again.
if _, err := f.Seek(-int64(len(l)), io.SeekCurrent); err != nil {
return fmt.Errorf("failed to reset seek in log file %q: %v", logPath, err)
}

// Reset seek so that if this is an incomplete line,
// it will be read again.
if _, err := f.Seek(-int64(len(l)), io.SeekCurrent); err != nil {
return fmt.Errorf("failed to reset seek in log file %q: %v", logPath, err)
// If the container exited consume data until the next EOF
continue
}
// Should stop after writing the remaining content.
stop = true
if len(l) == 0 {
continue
}
logrus.Debugf("Incomplete line in log file, path: %s line: %s", logPath, l)
}

// If the container exited consume data until the next EOF
// Parse the log line.
msg.reset()
if err := ParseCRILog(l, msg); err != nil {
logrus.WithError(err).Errorf("Failed when parsing line in log file, path: %s line: %s", logPath, l)
continue
}
// Should stop after writing the remaining content.
stop = true
if len(l) == 0 {
continue
// Write the log line into the stream.
if err := writer.write(msg, isNewLine); err != nil {
if err == errMaximumWrite {
logrus.Debugf("Finished parsing log file, hit bytes limit path: %s", logPath)
return nil
}
logrus.WithError(err).Errorf("Failed when writing line to log file, path: %s line: %s", logPath, l)
return err
}
logrus.Debugf("Incomplete line in log file, path: %s line: %s", logPath, l)
}

// Parse the log line.
msg.reset()
if err := ParseCRILog(l, msg); err != nil {
logrus.WithError(err).Errorf("Failed when parsing line in log file, path: %s line: %s", logPath, l)
continue
}
// Write the log line into the stream.
if err := writer.write(msg, isNewLine); err != nil {
if err == errMaximumWrite {
logrus.Debugf("Finished parsing log file, hit bytes limit path: %s", logPath)
return nil
if limitedMode {
limitedNum--
}
if len(msg.log) > 0 {
isNewLine = msg.log[len(msg.log)-1] == eol[0]
} else {
isNewLine = true
}
logrus.WithError(err).Errorf("Failed when writing line to log file, path: %s line: %s", logPath, l)
return err
}
if limitedMode {
limitedNum--
}
if len(msg.log) > 0 {
isNewLine = msg.log[len(msg.log)-1] == eol[0]
} else {
isNewLine = true
}
}
}
Expand Down

0 comments on commit 78ea0d1

Please sign in to comment.