Skip to content

Commit

Permalink
Add print exec stderr to logs for debug level
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed May 27, 2024
1 parent 4534b4d commit 3932dba
Showing 1 changed file with 39 additions and 20 deletions.
59 changes: 39 additions & 20 deletions internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"crypto/md5"
"encoding/hex"
"errors"
"io"
"fmt"
"net/url"
"os"
"os/exec"
Expand Down Expand Up @@ -68,8 +68,9 @@ func execHandle(rawURL string) (core.Producer, error) {

args := shell.QuoteSplit(rawURL[5:]) // remove `exec:`
cmd := exec.Command(args[0], args[1:]...)
if log.Debug().Enabled() {
cmd.Stderr = os.Stderr
cmd.Stderr = &logWriter{
buf: make([]byte, 512),
debug: log.Debug().Enabled(),
}

if path == "" {
Expand Down Expand Up @@ -104,18 +105,10 @@ func handlePipe(_ string, cmd *exec.Cmd, query url.Values) (core.Producer, error

log.Debug().Stringer("launch", time.Since(ts)).Msg("[exec] run pipe")

return prod, err
return prod, fmt.Errorf("exec/pipe: %w\n%s", err, cmd.Stderr)
}

func handleRTSP(url string, cmd *exec.Cmd, path string) (core.Producer, error) {
stderr := limitBuffer{buf: make([]byte, 512)}

if cmd.Stderr != nil {
cmd.Stderr = io.MultiWriter(cmd.Stderr, &stderr)
} else {
cmd.Stderr = &stderr
}

if log.Trace().Enabled() {
cmd.Stdout = os.Stdout
}
Expand Down Expand Up @@ -150,10 +143,10 @@ func handleRTSP(url string, cmd *exec.Cmd, path string) (core.Producer, error) {
case <-time.After(time.Second * 60):
_ = cmd.Process.Kill()
log.Error().Str("url", url).Msg("[exec] timeout")
return nil, errors.New("timeout")
return nil, errors.New("exec: timeout")
case <-done:
// limit message size
return nil, errors.New("exec: " + stderr.String())
return nil, fmt.Errorf("exec/rtsp\n%s", cmd.Stderr)
case prod := <-waiter:
log.Debug().Stringer("launch", time.Since(ts)).Msg("[exec] run rtsp")
return prod, nil
Expand All @@ -168,21 +161,47 @@ var (
waitersMu sync.Mutex
)

type limitBuffer struct {
buf []byte
n int
type logWriter struct {
buf []byte
debug bool
n int
}

func (l *limitBuffer) String() string {
func (l *logWriter) String() string {
if l.n == len(l.buf) {
return string(l.buf) + "..."
}
return string(l.buf[:l.n])
}

func (l *limitBuffer) Write(p []byte) (int, error) {
func (l *logWriter) Write(p []byte) (n int, err error) {
if l.n < cap(l.buf) {
l.n += copy(l.buf[l.n:], p)
}
return len(p), nil
n = len(p)
if l.debug {
if p = trimSpace(p); p != nil {
log.Debug().Msgf("[exec] %s", p)
}
}
return
}

func trimSpace(b []byte) []byte {
start := 0
stop := len(b)
for ; start < stop; start++ {
if b[start] >= ' ' {
break // trim all ASCII before 0x20
}
}
for ; ; stop-- {
if stop == start {
return nil // skip empty output
}
if b[stop-1] > ' ' {
break // trim all ASCII before 0x21
}
}
return b[start:stop]
}

0 comments on commit 3932dba

Please sign in to comment.