From c5c00e3a4caf6f3cd55a5e679a218dacd0e6de2d Mon Sep 17 00:00:00 2001
From: chenshijun <csj_sky@126.com>
Date: 星期一, 18 一月 2021 19:51:37 +0800
Subject: [PATCH] 增加sock之外的bus推送函数指针模式,方便使用

---
 logc.go |   36 +++++++++++++++++++++++++++++++-----
 1 files changed, 31 insertions(+), 5 deletions(-)

diff --git a/logc.go b/logc.go
index 8d35977..79462d9 100644
--- a/logc.go
+++ b/logc.go
@@ -42,17 +42,38 @@
 var (
 	msgChan chan []byte
 	bhSock *bhomebus.Socket
+	pubFn func(nodes []bhomebus.NetNode, topic string, data []byte, milliseconds int) int
 	ProcName string
 	ProcID string
 )
 
-func Init(sock *bhomebus.Socket, procId string, procName string) {
+func InitBySock(sock *bhomebus.Socket, procId string, procName string) bool {
 	msgChan = make(chan []byte, 100)
 	ProcName = procName
 	ProcID = procId
 	bhSock = sock
+	if bhSock == nil {
+		return false
+	}
 
 	go saveLoop()
+
+	return true
+}
+
+func InitByPubFn(fn func(nodes []bhomebus.NetNode, topic string, data []byte, milliseconds int) int,
+				procId string, procName string) bool {
+	msgChan = make(chan []byte, 100)
+	ProcName = procName
+	ProcID = procId
+	pubFn = fn
+	if pubFn == nil {
+		return false
+	}
+
+	go saveLoop()
+
+	return true
 }
 
 func Save(level int, logType int, v ...interface{}) {
@@ -101,10 +122,15 @@
 		select {
 		case data := <- msgChan:
 			var nodes []bhomebus.NetNode
-			nodes = append(nodes, bhomebus.NetNode{
-				Key: 8,
-			})
-			bhSock.PubTimeout(nodes, LogSaveTopic, data, 1000)
+			nodes = append(nodes, bhomebus.NetNode{})
+			if bhSock != nil {
+				bhSock.PubTimeout(nodes, LogSaveTopic, data, 1000)
+			} else if pubFn != nil {
+				pubFn(nodes, LogSaveTopic, data, 1000)
+			} else {
+				fmt.Println("bhSock nil and pubFn nil")
+			}
+
 		default:
 			time.Sleep(10*time.Millisecond)
 		}

--
Gitblit v1.8.0