From 6431dea09c64ec442102dbce5b6ea71aed23daa6 Mon Sep 17 00:00:00 2001 From: CDCDDCDC <792192820@qq.com> Date: Mon, 10 Nov 2025 21:27:24 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=BF=9D=E5=AD=98?= =?UTF-8?q?=E7=9A=84debug=E6=96=87=E4=BB=B6=E5=8F=AF=E8=83=BD=E4=B8=A2?= =?UTF-8?q?=E5=A4=B1=E9=83=A8=E5=88=86=20#6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- v5/pkg/connection.go | 38 +++++++++++++++++++++++++++----------- v5/pkg/service.go | 11 +++++++---- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/v5/pkg/connection.go b/v5/pkg/connection.go index 4c1e19f..34ee834 100644 --- a/v5/pkg/connection.go +++ b/v5/pkg/connection.go @@ -32,8 +32,11 @@ type connection struct { audioWriter *m7s.PublishAudioWriter[*format.Mpeg2Audio] videoWriter *m7s.PublishVideoWriter[*format.AnnexB] debug struct { - file *os.File - closeTime time.Time + hasRecord bool + file *os.File + closeTime time.Time + hasTemporaryStorage bool + temporaryStorage []byte } } @@ -61,7 +64,7 @@ func (c *connection) run(ctx context.Context, waitSubscriberOverTime time.Durati ticker.Stop() clear(data) c.stop() - if c.debug.file != nil { + if c.debug.hasRecord { _ = c.debug.file.Close() } }() @@ -89,6 +92,9 @@ func (c *connection) run(ctx context.Context, waitSubscriberOverTime time.Durati } return err } else if n > 0 { + if c.debug.hasTemporaryStorage { + c.debug.temporaryStorage = append(c.debug.temporaryStorage, data[:n]...) + } for pack, err := range packParse.parse(data[:n]) { if err == nil { once.Do(func() { @@ -111,14 +117,12 @@ func (c *connection) run(ctx context.Context, waitSubscriberOverTime time.Durati if handleErr != nil { return handleErr } - if c.debug.file != nil { - if _, err := c.debug.file.WriteString(fmt.Sprintf("%x", data[:n])); err != nil { - c.Warn("write debug file", - slog.String("err", err.Error())) - } - if time.Since(c.debug.closeTime) > 0 { - _ = c.debug.file.Close() - c.debug.file = nil + if c.debug.hasRecord { + if c.debug.hasTemporaryStorage { + c.debug.hasTemporaryStorage = false + c.saveDebugFile(c.debug.temporaryStorage) + } else { + c.saveDebugFile(data[:n]) } } } @@ -126,6 +130,18 @@ func (c *connection) run(ctx context.Context, waitSubscriberOverTime time.Durati } } +func (c *connection) saveDebugFile(data []byte) { + if _, err := c.debug.file.WriteString(fmt.Sprintf("%x", data)); err != nil { + c.Warn("write debug file", + slog.String("err", err.Error())) + } + _ = c.debug.file.Sync() + if time.Since(c.debug.closeTime) > 0 { + _ = c.debug.file.Close() + c.debug.hasRecord = false + } +} + func (c *connection) stop() { c.stopOnce.Do(func() { close(c.stopChan) diff --git a/v5/pkg/service.go b/v5/pkg/service.go index 2bec1fc..7d1e54b 100644 --- a/v5/pkg/service.go +++ b/v5/pkg/service.go @@ -55,6 +55,10 @@ func (s *Service) Run() { return } client := newConnection(conn, s.Logger, s.opts.timestampFunc) + if debug := s.opts.Debug; debug.enable { + client.debug.hasTemporaryStorage = true + client.debug.temporaryStorage = make([]byte, 0, 10*1024) + } httpBody := map[string]any{} ctx, cancel := context.WithCancel(context.Background()) client.onJoinEvent = func(c *connection, pack *jt1078.Packet) error { @@ -69,11 +73,11 @@ func (s *Service) Run() { "channel": pack.LogicChannel, "startTime": time.Now().Format(time.DateTime), } - if debug := s.opts.Debug; debug.enable { _ = os.MkdirAll(debug.dir, 0o755) - name := filepath.Join(debug.dir, strings.ReplaceAll(c.publisher.StreamPath, string(os.PathSeparator), "-")+"-debug.txt") - if file, err := os.OpenFile(name, os.O_CREATE|os.O_RDWR|os.O_TRUNC|os.O_APPEND, 0o666); err == nil { + name := filepath.Join(debug.dir, strings.ReplaceAll(c.publisher.StreamPath, string(os.PathSeparator), "-")) + if file, fileErr := os.OpenFile(name+"-debug.txt", os.O_CREATE|os.O_RDWR|os.O_TRUNC|os.O_APPEND, 0o666); fileErr == nil { + client.debug.hasRecord = true client.debug.file = file client.debug.closeTime = time.Now().Add(debug.time) httpBody["debugFile"] = name @@ -84,7 +88,6 @@ func (s *Service) Run() { slog.String("err", err.Error())) } } - onNoticeEvent(s.opts.onJoinURL, httpBody) return nil }