From c034bfdae5548e5d62281316bc39d6336af548cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Haibo=20Chen=28=E9=99=88=E6=B5=B7=E5=8D=9A=29?= <495810242@qq.com> Date: Mon, 12 May 2025 09:57:11 +0800 Subject: [PATCH] update (#25) * update log * update log level * Enhance config.yaml by adding a comment for log-level options * update log * Refactor SIP service start method to accept configuration parameter for log level management --- main/main.go | 2 +- pkg/service/inbound.go | 18 ++++++++++-------- pkg/service/service.go | 6 ++++-- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/main/main.go b/main/main.go index d193512..f3a28ca 100644 --- a/main/main.go +++ b/main/main.go @@ -62,7 +62,7 @@ func main() { return } - if err := sipSvr.Start(); err != nil { + if err := sipSvr.Start(conf); err != nil { slog.Error("start sip service failed", "error", err.Error()) return } diff --git a/pkg/service/inbound.go b/pkg/service/inbound.go index 9857486..3114a65 100644 --- a/pkg/service/inbound.go +++ b/pkg/service/inbound.go @@ -3,6 +3,7 @@ package service import ( "bytes" "encoding/xml" + "fmt" "log/slog" "net" "net/http" @@ -35,6 +36,8 @@ func (s *UAS) onRegister(req *sip.Request, tx sip.ServerTransaction) { return } + slog.Debug(fmt.Sprintf("Received REGISTER %s", req.String())) + if s.conf.GB28181.Auth.Enable { // Check if Authorization header exists authHeader := req.GetHeaders("Authorization") @@ -84,7 +87,7 @@ func (s *UAS) onRegister(req *sip.Request, tx sip.ServerTransaction) { NetworkType: req.Transport(), }) s.respondRegister(req, http.StatusOK, "OK", tx) - slog.Info("Register success", "device_id", id, "source", req.Source(), "request", req.String()) + slog.Info(fmt.Sprintf("Register success %s %s", id, req.Source())) go s.ConfigDownload(id) go s.Catalog(id) @@ -99,7 +102,7 @@ func (s *UAS) onRegister(req *sip.Request, tx sip.ServerTransaction) { DM.UpdateDevice(id, d) s.respondRegister(req, http.StatusOK, "OK", tx) - slog.Info("Re-register success", "device_id", id, "source", req.Source(), "request", req.String()) + slog.Info(fmt.Sprintf("Re-register success %s %s", id, req.Source())) } } } @@ -117,7 +120,7 @@ func (s *UAS) onMessage(req *sip.Request, tx sip.ServerTransaction) { slog.Error("invalid device ID", "request", req.String()) } - slog.Debug("Received MESSAGE", "request", req.String()) + slog.Debug(fmt.Sprintf("Received MESSAGE %s", req.String())) temp := &models.XmlMessageInfo{} decoder := xml.NewDecoder(bytes.NewReader([]byte(req.Body()))) @@ -125,10 +128,12 @@ func (s *UAS) onMessage(req *sip.Request, tx sip.ServerTransaction) { if err := decoder.Decode(temp); err != nil { slog.Error("decode message error", "error", err.Error(), "message", req.Body()) } + + slog.Info(fmt.Sprintf("Received MESSAGE %s %s %s", temp.CmdType, temp.DeviceID, req.Source())) + var body string switch temp.CmdType { case "Keepalive": - slog.Info("Keepalive") if d, ok := DM.GetDevice(temp.DeviceID); ok && d.Online { // 更新设备心跳时间 DM.UpdateDeviceHeartbeat(temp.DeviceID) @@ -138,16 +143,13 @@ func (s *UAS) onMessage(req *sip.Request, tx sip.ServerTransaction) { } case "SensorCatalog": // 兼容宇视,非国标 case "Catalog": - slog.Info("Catalog") DM.UpdateChannels(temp.DeviceID, temp.DeviceList...) //go s.AutoInvite(temp.DeviceID, temp.DeviceList...) case "ConfigDownload": - slog.Info("ConfigDownload") DM.UpdateDeviceConfig(temp.DeviceID, &temp.BasicParam) case "Alarm": slog.Info("Alarm") case "RecordInfo": - slog.Info("RecordInfo") // 从 recordQueryResults 中获取对应通道的结果通道 if ch, ok := s.recordQueryResults.Load(temp.DeviceID); ok { // 发送查询结果 @@ -164,6 +166,6 @@ func (s *UAS) onMessage(req *sip.Request, tx sip.ServerTransaction) { } func (s *UAS) onNotify(req *sip.Request, tx sip.ServerTransaction) { - slog.Info("Received NOTIFY request") + slog.Debug(fmt.Sprintf("Received NOTIFY %s", req.String())) tx.Respond(sip.NewResponseFromRequest(req, http.StatusOK, "OK", nil)) } diff --git a/pkg/service/service.go b/pkg/service/service.go index ac786d9..832e8e5 100644 --- a/pkg/service/service.go +++ b/pkg/service/service.go @@ -25,8 +25,10 @@ func NewService(ctx context.Context, r0 interface{}) (*Service, error) { return s, nil } -func (s *Service) Start() error { - zerolog.SetGlobalLevel(zerolog.Disabled) +func (s *Service) Start(conf *config.MainConfig) error { + if conf.Common.LogLevel != "debug" { + zerolog.SetGlobalLevel(zerolog.Disabled) + } ua, err := sipgo.NewUA( sipgo.WithUserAgent(UserAgent),