diff --git a/encoder.go b/encoder.go index e948f3d..856728b 100644 --- a/encoder.go +++ b/encoder.go @@ -10,7 +10,7 @@ import ( "fmt" vnc "github.com/amitbet/vnc2video" "github.com/amitbet/vnc2video/encoders" - log "github.com/sirupsen/logrus" + "github.com/sirupsen/logrus" "image" "image/color" "io" @@ -107,11 +107,11 @@ func encodePPMforRGBImage(w io.Writer, img *vnc.RGBImage) error { type X264ImageCustomEncoder struct { encoders.X264ImageEncoder - FFMpegBinPath string - cmd *exec.Cmd - input io.WriteCloser - closed bool - Framerate int + FFMpegBinPath string + cmd *exec.Cmd + input io.WriteCloser + closed bool + Framerate int ConstantRateFactor int } @@ -137,7 +137,7 @@ func (enc *X264ImageCustomEncoder) Init(videoFileName string) { encInput, err := cmd.StdinPipe() enc.input = encInput if err != nil { - log.Error("can't get ffmpeg input pipe") + logrus.WithError(err).Error("can't get ffmpeg input pipe.") } enc.cmd = cmd } @@ -147,10 +147,10 @@ func (enc *X264ImageCustomEncoder) Run(videoFileName string) error { } enc.Init(videoFileName) - log.Infof("launching binary: %v", enc.cmd) + logrus.Infof("launching binary: %v", enc.cmd) err := enc.cmd.Run() if err != nil { - log.Errorf("error while launching ffmpeg: %v\n err: %v", enc.cmd.Args, err) + logrus.WithError(err).Errorf("error while launching ffmpeg: %v", enc.cmd.Args) return err } return nil @@ -162,7 +162,7 @@ func (enc *X264ImageCustomEncoder) Encode(img image.Image) { err := encodePPM(enc.input, img) if err != nil { - log.Error("error while encoding image:", err) + logrus.WithError(err).Error("error while encoding image.") } } @@ -173,7 +173,7 @@ func (enc *X264ImageCustomEncoder) Close() { enc.closed = true err := enc.input.Close() if err != nil { - log.Error("could not close input", err) + logrus.WithError(err).Error("could not close input.") } } diff --git a/main.go b/main.go index a73b900..9b895cc 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,7 @@ import ( "context" "fmt" vnc "github.com/amitbet/vnc2video" - log "github.com/sirupsen/logrus" + "github.com/sirupsen/logrus" "github.com/urfave/cli/v2" "net" @@ -75,20 +75,20 @@ func main() { } if err := app.Run(os.Args); err != nil { - log.Fatal(err) + logrus.WithError(err).Fatal("recording failed.") } } func recorder(c *cli.Context) error { address := fmt.Sprintf("%s:%d", c.String("host"), c.Int("port")) - nc, err := net.DialTimeout("tcp", address, 5*time.Second) + dialer, err := net.DialTimeout("tcp", address, 5*time.Second) if err != nil { - log.Fatalf("Error connecting to VNC host. %v", err) + logrus.WithError(err).Error("connection to VNC host failed.") return err } - defer nc.Close() + defer dialer.Close() - log.Infof("Connected to %s", address) + logrus.WithField("address", address).Info("connection established.") // Negotiate connection with the server. cchServer := make(chan vnc.ServerMessage) @@ -106,7 +106,7 @@ func recorder(c *cli.Context) error { } } - ccfg := &vnc.ClientConfig{ + ccflags := &vnc.ClientConfig{ SecurityHandlers: secHandlers, DrawCursor: true, PixelFormat: vnc.PixelFormat32bit, @@ -127,30 +127,31 @@ func recorder(c *cli.Context) error { ErrorCh: errorCh, } - cc, err := vnc.Connect(context.Background(), nc, ccfg) - defer cc.Close() - - screenImage := cc.Canvas + vncConnection, err := vnc.Connect(context.Background(), dialer, ccflags) + defer vncConnection.Close() if err != nil { - log.Fatalf("Error negotiating connection to VNC host. %v", err) + logrus.WithError(err).Error("connection negotiation to VNC host failed.") return err } + screenImage := vncConnection.Canvas - ffmpeg_path, err := exec.LookPath(c.String("ffmpeg")) + ffmpegPath, err := exec.LookPath(c.String("ffmpeg")) if err != nil { - panic(err) + logrus.WithError(err).Error("ffmpeg binary not found.") + return err } - log.Infof("Using %s for encoding", ffmpeg_path) + logrus.WithField("ffmpeg", ffmpegPath).Info("ffmpeg binary for recording found") + vcodec := &X264ImageCustomEncoder{ - FFMpegBinPath: ffmpeg_path, - Framerate: c.Int("framerate"), + FFMpegBinPath: ffmpegPath, + Framerate: c.Int("framerate"), ConstantRateFactor: c.Int("crf"), } //goland:noinspection GoUnhandledErrorResult go vcodec.Run(c.String("outfile")) - for _, enc := range ccfg.Encodings { + for _, enc := range ccflags.Encodings { myRenderer, ok := enc.(vnc.Renderer) if ok { @@ -158,7 +159,7 @@ func recorder(c *cli.Context) error { } } - cc.SetEncodings([]vnc.EncodingType{ + vncConnection.SetEncodings([]vnc.EncodingType{ vnc.EncCursorPseudo, vnc.EncPointerPosPseudo, vnc.EncCopyRect, @@ -201,20 +202,28 @@ func recorder(c *cli.Context) error { case err := <-errorCh: panic(err) case msg := <-cchClient: - log.Debugf("Received client message type:%v msg:%v\n", msg.Type(), msg) + logrus.WithFields(logrus.Fields{ + "messageType": msg.Type(), + "message": msg, + }).Debug("client message received.") + case msg := <-cchServer: if msg.Type() == vnc.FramebufferUpdateMsgType { secsPassed := time.Now().Sub(timeStart).Seconds() frameBufferReq++ reqPerSec := float64(frameBufferReq) / secsPassed - log.Debugf("reqs=%d, seconds=%f, Req Per second= %f", frameBufferReq, secsPassed, reqPerSec) + logrus.WithFields(logrus.Fields{ + "reqs": frameBufferReq, + "seconds": secsPassed, + "Req Per second": reqPerSec, + }).Debug("framebuffer update") - reqMsg := vnc.FramebufferUpdateRequest{Inc: 1, X: 0, Y: 0, Width: cc.Width(), Height: cc.Height()} - reqMsg.Write(cc) + reqMsg := vnc.FramebufferUpdateRequest{Inc: 1, X: 0, Y: 0, Width: vncConnection.Width(), Height: vncConnection.Height()} + reqMsg.Write(vncConnection) } case signal := <-sigCh: if signal != nil { - log.Info(signal, " received, exit.") + logrus.WithField("signal", signal).Info("signal received.") vcodec.Close() // give some time to write the file time.Sleep(time.Second * 1)