From b3a8f2bca314131530628d0c288240e8e1f30127 Mon Sep 17 00:00:00 2001 From: saxon Date: Fri, 14 Sep 2018 12:17:29 +0930 Subject: [PATCH] revid: improved logging key value pairs in revid.go --- revid/config.go | 4 +-- revid/revid.go | 83 +++++++++++++++++++++++++-------------------- revid/revid_test.go | 9 ++--- 3 files changed, 54 insertions(+), 42 deletions(-) diff --git a/revid/config.go b/revid/config.go index 447ba0c2..41ed0cbd 100644 --- a/revid/config.go +++ b/revid/config.go @@ -43,7 +43,7 @@ type Config struct { RtmpMethod uint8 Packetization uint8 QuantizationMode uint8 - Verbosity uint8 + Verbosity int8 HorizontalFlip uint8 VerticalFlip uint8 FramesPerClip int @@ -280,7 +280,7 @@ func (c *Config) Validate(r *Revid) error { } if c.Quantization == "" { - c.Logger.Log(smartlogger.Warning, "No quantization defined, defaulting to 35!") + c.Logger.Log(smartlogger.Warning, "No quantization defined") c.Quantization = defaultQuantization } else { if integer, err := strconv.Atoi(c.Quantization); integer < 0 || integer > 51 || err != nil { diff --git a/revid/revid.go b/revid/revid.go index 600483d8..16a1fea0 100644 --- a/revid/revid.go +++ b/revid/revid.go @@ -31,7 +31,7 @@ package revid import ( "errors" - "fmt" + _ "fmt" "io" "os" "os/exec" @@ -145,14 +145,14 @@ func (r *Revid) reset(config Config) error { r.config.Logger = config.Logger err := config.Validate(r) if err != nil { - return errors.New("Config struct is bad!: " + err.Error()) + return errors.New("Config struct is bad: " + err.Error()) } r.config = config if r.destination != nil { err = r.destination.close() if err != nil { - r.config.Logger.Log(smartlogger.Error, err.Error()) + r.config.Logger.Log(smartlogger.Error, "Could not close destination", "error", err.Error()) } } switch r.config.Output { @@ -186,10 +186,10 @@ func (r *Revid) reset(config Config) error { } switch r.config.InputCodec { case H264: - r.config.Logger.Log(smartlogger.Info, "using H264 lexer") + r.config.Logger.Log(smartlogger.Info, "Using H264 lexer") r.lexTo = lex.H264 case Mjpeg: - r.config.Logger.Log(smartlogger.Info, "using MJPEG lexer") + r.config.Logger.Log(smartlogger.Info, "Using MJPEG lexer") r.lexTo = lex.MJPEG } @@ -297,16 +297,17 @@ func (r *Revid) packClips() { case frame := <-r.encoder.Stream(): lenOfFrame := len(frame) if lenOfFrame > ringBufferElementSize { - r.config.Logger.Log(smartlogger.Warning, fmt.Sprintf("Frame was too big: %v bytes, getting another one!", lenOfFrame)) + r.config.Logger.Log(smartlogger.Warning, "Frame was too big", "frame size", lenOfFrame) frame = r.getFrame() lenOfFrame = len(frame) } _, err := r.ringBuffer.Write(frame) if err != nil { if err == ring.ErrDropped { - r.config.Logger.Log(smartlogger.Warning, fmt.Sprintf("dropped %d byte frame", len(frame))) + r.config.Logger.Log(smartlogger.Warning, "dropped frame", "frame size", len(frame)) } else { - r.config.Logger.Log(smartlogger.Error, err.Error()) + r.config.Logger.Log(smartlogger.Error, "Unexpected ringbuffer write error", + "error", err.Error()) } } packetCount++ @@ -352,31 +353,31 @@ func (r *Revid) outputClips() { r.config.Logger.Log(smartlogger.Debug, "About to send") err = r.destination.load(chunk) if err != nil { - r.config.Logger.Log(smartlogger.Error, "failed to load clip") + r.config.Logger.Log(smartlogger.Error, "Failed to load clip") } err = r.destination.send() if err == nil { - r.config.Logger.Log(smartlogger.Debug, "sent clip") + r.config.Logger.Log(smartlogger.Debug, "Sent clip") } if r.isRunning && err != nil && chunk.Len() > 11 { r.config.Logger.Log(smartlogger.Debug, "Send failed! Trying again") // Try and send again err = r.destination.send() - r.config.Logger.Log(smartlogger.Error, err.Error()) + r.config.Logger.Log(smartlogger.Error, "Destination send error", "error", err.Error()) // if there's still an error we try and reconnect, unless we're stopping for r.isRunning && err != nil { r.config.Logger.Log(smartlogger.Debug, "Send failed a again! Trying to reconnect...") time.Sleep(time.Duration(sendFailedDelay) * time.Millisecond) - r.config.Logger.Log(smartlogger.Error, err.Error()) + r.config.Logger.Log(smartlogger.Error, "Send failed with error", "error", err.Error()) if rs, ok := r.destination.(restarter); ok { - r.config.Logger.Log(smartlogger.Debug, fmt.Sprintf("restarting %T session", rs)) + r.config.Logger.Log(smartlogger.Debug, "restarting session", "session", rs) err = rs.restart() if err != nil { // TODO(kortschak): Make this "Fatal" when that exists. - r.config.Logger.Log(smartlogger.Error, "failed to restart rtmp session") + r.config.Logger.Log(smartlogger.Error, "failed to restart rtmp session", "error", err.Error()) r.isRunning = false return } @@ -386,7 +387,7 @@ func (r *Revid) outputClips() { r.config.Logger.Log(smartlogger.Debug, "Trying to send again with new connection...") err = r.destination.send() if err != nil { - r.config.Logger.Log(smartlogger.Error, err.Error()) + r.config.Logger.Log(smartlogger.Error, "Send failed with error", "error", err.Error()) } } } @@ -401,16 +402,16 @@ func (r *Revid) outputClips() { if deltaTime > bitrateTime { // FIXME(kortschak): For subsecond deltaTime, this will give infinite bitrate. r.bitrate = int(float64(bytes*8) / float64(deltaTime/time.Second)) - r.config.Logger.Log(smartlogger.Debug, fmt.Sprintf("Bitrate: %v bits/s\n", r.bitrate)) - r.config.Logger.Log(smartlogger.Debug, fmt.Sprintf("Ring buffer size: %v\n", r.ringBuffer.Len())) + r.config.Logger.Log(smartlogger.Debug, "Bitrate (bits/s)", "bitrate", r.bitrate) + r.config.Logger.Log(smartlogger.Debug, "Ring buffer size", "value", r.ringBuffer.Len()) prevTime = now bytes = 0 } } - r.config.Logger.Log(smartlogger.Info, "Not outputting clips anymore!") + r.config.Logger.Log(smartlogger.Info, "Not outputting clips anymore") err := r.destination.close() if err != nil { - r.config.Logger.Log(smartlogger.Error, "failed to close destination") + r.config.Logger.Log(smartlogger.Error, "Failed to close destination", "error", err.Error()) } } @@ -420,32 +421,42 @@ func (r *Revid) startRaspivid() error { r.config.Logger.Log(smartlogger.Info, "Starting raspivid!") switch r.config.InputCodec { case H264: - args := []string{ - "-cd", "H264", - "-o", "-", - "-n", - "-t", runContinuously, - "-b", r.config.Bitrate, - "-w", r.config.Width, - "-h", r.config.Height, - "-fps", r.config.FrameRate, - "-ih", - "-g", r.config.IntraRefreshPeriod, - } + // FIXME(saxon): consider using a map right from the start to store + // the raspivid args + raspiArgs := make(map[string]string) + raspiArgs["-cd"] = "H264" + raspiArgs["-o"] = "-" + raspiArgs["-n"] = "" + raspiArgs["-t"] = runContinuously + raspiArgs["-b"] = r.config.Bitrate + raspiArgs["-w"] = r.config.Width + raspiArgs["-h"] = r.config.Height + raspiArgs["-fps"] = r.config.FrameRate + raspiArgs["-ih"] = "" + raspiArgs["-g"] = r.config.IntraRefreshPeriod + if r.config.QuantizationMode == QuantizationOn { - args = append(args, "-qp", r.config.Quantization) + raspiArgs["-qp"] = r.config.Quantization } if r.config.HorizontalFlip == Yes { - args = append(args, "-hf") + raspiArgs["-hf"] = strconv.Itoa(int(r.config.HorizontalFlip)) } if r.config.VerticalFlip == Yes { - args = append(args, "-vf") + raspiArgs["-vf"] = strconv.Itoa(int(r.config.VerticalFlip)) + } + // Log all the args and create []string + args := make([]string, 0) + for i := range raspiArgs { + r.config.Logger.Log(smartlogger.Info, "Raspivid arg", i, raspiArgs[i]) + // First append the flag + args = append(args, i) + // and now append any values for the flag + args = append(args, raspiArgs[i]) } - - r.config.Logger.Log(smartlogger.Info, fmt.Sprintf("Starting raspivid with args: %v", args)) r.cmd = exec.Command("raspivid", args...) case Mjpeg: + // FIXME(saxon): do above in this case too r.cmd = exec.Command("raspivid", "-cd", "MJPEG", "-o", "-", diff --git a/revid/revid_test.go b/revid/revid_test.go index 6de215ce..bba9c562 100644 --- a/revid/revid_test.go +++ b/revid/revid_test.go @@ -28,8 +28,8 @@ LICENSE package revid import ( - "testing" - "time" + _ "testing" + _ "time" ) /* @@ -143,7 +143,7 @@ func TestRtmpOutputUsingLibRtmp(t *testing.T){ revidInst.Stop() } */ - +/* // Test revidInst with a Raspivid h264 input func TestRaspividToRtmp(t *testing.T) { config := Config{ @@ -157,7 +157,7 @@ func TestRaspividToRtmp(t *testing.T) { Packetization: Flv, FrameRate: "25", } - revidInst, err := New(config) + revidInst, err := New(config, nil) if err != nil { t.Errorf("Should not have got an error!") return @@ -166,3 +166,4 @@ func TestRaspividToRtmp(t *testing.T) { time.Sleep(43200 * time.Second) revidInst.Stop() } +*/