日志输出,空指针导致程序出错

This commit is contained in:
lijun 2025-01-15 23:41:01 +08:00
parent 7c8b0843a0
commit 10c7a6fb18
3 changed files with 27 additions and 26 deletions

View File

@ -144,11 +144,11 @@ func GetRealTimeImage(c *gin.Context) {
}
ws, err := upgrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
log.Fatalf("connect wss err:", err)
log.Printf("connect wss err:%v", err)
return
}
worker.SetRedisWithExpire(strconv.Itoa(int(device.ID))+"_is_play", "1", time.Minute*5)
log.Fatalf("device_id:", device_id_int, " has set is_play to 1")
log.Printf("device_id:%d has set is_play to 1", device_id_int)
go subscribeAndHandleMessagesV3(ws, device_id_int)
}
@ -168,12 +168,12 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) {
if c != count {
//将img转[]byte
if img.Empty() {
log.Fatalf("device:%d img is empty!", device_id)
log.Printf("device:%d img is empty!", device_id)
} else {
//gocv.Matrix转为jpeg
buf, err := gocv.IMEncode(".jpg", img)
if err != nil {
log.Fatalf("img encode err:", err)
log.Printf("img encode err:%v", err)
worker.SetRedisSetRemove(online_conn_key, con_id)
goto end
}
@ -181,7 +181,7 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) {
err2 := ws.WriteMessage(websocket.BinaryMessage, buf1)
if err2 != nil {
log.Fatalf("send message to client err:", err2)
log.Printf("send message to client err:%v", err2)
worker.SetRedisSetRemove(online_conn_key, con_id)
goto end
}
@ -192,7 +192,7 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) {
if t_count%10 == 0 {
err := ws.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(time.Second))
if err != nil {
log.Fatalf("Connection check failed:", err)
log.Printf("Connection check failed:%v", err)
worker.SetRedisSetRemove(online_conn_key, con_id)
goto end
}
@ -202,7 +202,7 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) {
t_count++
err := img.Close()
if err != nil {
log.Fatalf("close img err:", err)
log.Printf("close img err:%v", err)
}
}
@ -210,6 +210,6 @@ end:
// 查看是否还有其他连接,没有则设置 is_play 为 0
if worker.IsContainKey(online_conn_key) == false {
worker.SetRedisWithExpire(strconv.Itoa(device_id)+"_is_play", "1", time.Minute*5)
log.Fatalf("device_id:", device_id, " has set is_play to 0")
log.Printf("device_id: %d has set is_play to 0", device_id)
}
}

View File

@ -96,7 +96,7 @@ func ReadConfigAndSetSystem() {
}
if is_get == false && device.NextStop == false { //如果设备流已经停止且不暂停,则开启
go service.GetVideoStream(device.ID)
log.Fatalf("device:%d has started!\n", device.ID)
log.Printf("device:%d has started!\n", device.ID)
}
}
}

View File

@ -48,7 +48,8 @@ func GetDeviceCurrentFrame(device_id int) (gocv.Mat, int) {
}
mutex, ok := mutex_.(*sync.RWMutex)
if !ok {
log.Fatalf("DeviceRWMap 存储的不是 *sync.RWMutex 类型device_id: %d", device_id)
log.Printf("DeviceRWMap 存储的不是 *sync.RWMutex 类型device_id: %d", device_id)
return gocv.NewMat(), -1
}
mutex.RLock()
defer mutex.RUnlock()
@ -59,7 +60,7 @@ func GetDeviceCurrentFrame(device_id int) (gocv.Mat, int) {
}
frame, ok := frameIface.(gocv.Mat)
if !ok {
log.Fatalf("DeviceCurrentFrameMap 存储的不是 gocv.Mat 类型device_id: %d", device_id)
log.Printf("DeviceCurrentFrameMap 存储的不是 gocv.Mat 类型device_id: %d", device_id)
}
frame_countIface, ok := DeviceFrameCount.Load(device_id)
if !ok {
@ -67,7 +68,7 @@ func GetDeviceCurrentFrame(device_id int) (gocv.Mat, int) {
}
frame_count, ok := frame_countIface.(int)
if !ok {
log.Fatalf("DeviceFrameCount 存储的不是 int 类型device_id: %d", device_id)
log.Printf("DeviceFrameCount 存储的不是 int 类型device_id: %d", device_id)
}
return frame, frame_count
}
@ -76,19 +77,19 @@ func getVideoFrame(device proto.DeviceInfo) {
//捕获异常
defer func() {
if err := recover(); err != nil {
log.Fatalf("设备:%d 错误: %v\n", device.ID, err)
log.Printf("设备:%d 错误: %v\n", device.ID, err)
}
}()
webcam, err := gocv.OpenVideoCapture(device.Stream)
if err != nil {
log.Fatalf("设备:%s 错误: 无法打开视频流err: %v\n", device.ID, err)
log.Printf("设备:%s 错误: 无法打开视频流err: %v\n", device.ID, err)
return
}
defer func(webcam *gocv.VideoCapture) {
err2 := webcam.Close()
if err2 != nil {
log.Fatalf("设备:%d 错误: 无法关闭视频流,%s \n", device.ID, err2.Error())
log.Printf("设备:%d 错误: 无法关闭视频流,%s \n", device.ID, err2.Error())
}
}(webcam)
// 字体相关设置对应OpenCV默认字体等这里简化处理实际可按需求调整
@ -99,7 +100,7 @@ func getVideoFrame(device proto.DeviceInfo) {
z := 0
for {
if device.LogFrame > 0 && z%device.LogFrame == 0 {
log.Fatalf("设备:%d 当前帧: %d\n", device.ID, z)
log.Printf("设备:%d 当前帧: %d\n", device.ID, z)
}
if device.NextStop {
break
@ -107,11 +108,11 @@ func getVideoFrame(device proto.DeviceInfo) {
frame := gocv.NewMat()
ok := webcam.Read(&frame)
if !ok {
log.Fatalf("设备 错误: 无法从视频流中读取帧:", device, "\n")
log.Printf("设备 错误: 无法从视频流中读取帧:", device, "\n")
break
}
if frame.Empty() {
log.Fatalf("设备:%s 错误: 无法从视频流中读取帧\n", device)
log.Printf("设备:%s 错误: 无法从视频流中读取帧\n", device)
//等待50ms
time.Sleep(50 * time.Millisecond)
continue
@ -119,7 +120,7 @@ func getVideoFrame(device proto.DeviceInfo) {
height := frame.Rows()
width := frame.Cols()
if height < device.CheckFrameHeight || width < device.CheckFrameWidth {
log.Fatalf("设备:%s 帧尺寸已改变\n", device)
log.Printf("设备:%s 帧尺寸已改变\n", device)
break
}
currentTime := time.Now().Format("2006-01-02 15:04:05")
@ -127,12 +128,12 @@ func getVideoFrame(device proto.DeviceInfo) {
//需要将帧付给全局变量
err3 := SetDeviceCurrentFrame(frame, device.ID)
if err3 != nil {
log.Fatalf("设备:%d 错误: 无法设置当前帧,err:%s \n", device.ID, err3.Error())
log.Printf("设备:%d 错误: 无法设置当前帧,err:%s \n", device.ID, err3.Error())
}
z++
err2 := frame.Close()
if err2 != nil {
log.Fatalf("设备:%d,计数z=%d, 错误: 无法关闭帧\n", device.ID, z)
log.Printf("设备:%d,计数z=%d, 错误: 无法关闭帧\n", device.ID, z)
}
}
}
@ -154,11 +155,11 @@ func Get(url string) int {
func GetVideoStream(id int) {
is_get, ok := DeviceIsGettingFrame.Load(id)
if !ok {
fmt.Println("device:", id, " not found")
log.Printf("device: %d not found", id)
return
}
if is_get == true {
fmt.Println("device:", id, " is running!")
log.Printf("device: %d is running!", id)
return
}
for {
@ -174,12 +175,12 @@ func GetVideoStream(id int) {
}
if index == len(proto.Config.DeviceInfo) {
//设备不存在
log.Println("device:", id, " not found")
log.Printf("device: %d not found", id)
break
}
is_get, ok = DeviceIsGettingFrame.Load(id)
if is_get == true {
log.Println("for device:", id, " is running!")
log.Printf("for device:%d is running!", id)
break
}
@ -190,7 +191,7 @@ func GetVideoStream(id int) {
//设置设备控制信息
status := Get(device.Control)
DeviceIsGettingFrame.Store(id, true)
log.Println("device:", device.ID, " set control info status:", status)
log.Printf("device: %d set control info status: %d", device.ID, status)
getVideoFrame(device)
DeviceIsGettingFrame.Store(id, false)
//等待1s