From 4a6c3ea781abc08b9eb40d24a4e1101bde8fe75c Mon Sep 17 00:00:00 2001
From: yanghui <yanghui@aiotlink.com>
Date: 星期一, 26 四月 2021 15:39:08 +0800
Subject: [PATCH] add msg

---
 logc.go |  159 +++++++++++++++++++++++------------------------------
 1 files changed, 69 insertions(+), 90 deletions(-)

diff --git a/logc.go b/logc.go
index f1c161f..7eddc7e 100644
--- a/logc.go
+++ b/logc.go
@@ -4,7 +4,7 @@
 	"basic.com/valib/bhomebus.git"
 	"encoding/json"
 	"fmt"
-	uuid "github.com/satori/go.uuid"
+	"sync"
 	"time"
 )
 
@@ -41,124 +41,103 @@
 type LogRegister struct {
 	Nodes      []bhomebus.NetNode
 	Topic      string
-	Reply       *bhomeclient.Reply
 	Payload     []byte
 }
 
 type LogReportCallback func(*LogRegister)
 
 var (
-	msgChan chan []byte
-	bhSock *bhomebus.Socket
-	pubFn func(nodes []bhomebus.NetNode, topic string, data []byte, milliseconds int) int
-	ProcName string
-	ProcID string
+	opChan chan *OperationLog
 
-	logCallback LogReportCallback
 )
 
-func init() {
 
-}
+func Init(flogWriter LogReportCallback, procId string, procName string, wg *sync.WaitGroup, done  chan struct{}) bool {
+	opChan = make(chan *OperationLog, 100)
+	//ProcName = procName
+	//ProcID = procId
 
-func Init(flogWriter LogReportCallback, procId string, procName string) bool {
-	msgChan = make(chan []byte, 100)
-	ProcName = procName
-	ProcID = procId
-	logCallback = flogWriter
-	if logCallback == nil {
-		return false
-	}
-
-	go saveLoop()
+	go saveLoop(flogWriter, wg, done)
 
 	return true
 }
 
-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
+func SaveOperationLog(log *OperationLog) {
+	if nil != log {
+		opChan <- log
 	}
-
-	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{}) {
-	cache(level, logType, "", "", v)
-}
-
-func SaveManual(level int, logType int, userID string, userName string, v ...interface{}) {
-	cache(level, logType, userID, userName, v)
-}
-
-func cache(level int, logType int, userID string, userName string, fmtArgs []interface{}) {
-	// Format with Sprint, Sprintf, or neither.
-	msg := ""
-	if len(fmtArgs) > 0 {
-		msg = fmt.Sprint(fmtArgs...)
-	}
-	fmt.Println(msg)
-
-	log := LogInfo {
-		ID:        uuid.NewV4().String(),
-		Timestamp: time.Now().Format("2006-01-02 15:04:05"),
-		ProcName:  ProcName,
-		ProcID:    ProcID,
-		Level:     level,
-		Type:      logType,
-		UserID:    userID,
-		UserName:  userName,
-		Info:      msg,
-	}
-
-	data,err := json.Marshal(log)
-	if err != nil {
-		fmt.Println("json.Marshal(log) error:", log)
-		return
-	}
-
-	msgChan <- data
-}
+//func Save(level int, logType int, v ...interface{}) {
+//	cache(level, logType, "", "", v)
+//}
+//
+//func SaveManual(level int, logType int, userID string, userName string, v ...interface{}) {
+//	cache(level, logType, userID, userName, v)
+//}
+//
+//func cache(level int, logType int, userID string, userName string, fmtArgs []interface{}) {
+//	// Format with Sprint, Sprintf, or neither.
+//	msg := ""
+//	if len(fmtArgs) > 0 {
+//		msg = fmt.Sprint(fmtArgs...)
+//	}
+//	fmt.Println(msg)
+//
+//	log := LogInfo {
+//		ID:        uuid.NewV4().String(),
+//		Timestamp: time.Now().Format("2006-01-02 15:04:05"),
+//		ProcName:  ProcName,
+//		ProcID:    ProcID,
+//		Level:     level,
+//		Type:      logType,
+//		UserID:    userID,
+//		UserName:  userName,
+//		Info:      msg,
+//	}
+//
+//	data,err := json.Marshal(log)
+//	if err != nil {
+//		fmt.Println("json.Marshal(log) error:", log)
+//		return
+//	}
+//
+//	msgChan <- data
+//}
 
 const (
 	LogSaveTopic = "logSaveTopic"
 )
 
-func saveLoop() {
+func saveLoop(logCallback LogReportCallback, wg *sync.WaitGroup, done  chan struct{}) {
+	defer wg.Done()
+
+	if nil == logCallback {
+		return
+	}
+
 	for {
 		select {
-		case data := <- msgChan:
-			var nodes []bhomebus.NetNode
-			nodes = append(nodes, bhomebus.NetNode{})
-			if bhSock != nil {
-				bhSock.PubTimeout(nodes, LogSaveTopic, data, 1000)
-			} else if pubFn != nil {
-				pubFn(nodes, LogSaveTopic, data, 1000)
+		case <- done:
+			return
+		case data := <- opChan:
+			payload, err := json.Marshal(data)
+			if err != nil {
+				fmt.Println("json.Marshal(operation data) error:", data)
 			} else {
-				fmt.Println("bhSock nil and pubFn nil")
-			}
+				var nodes []bhomebus.NetNode
+				nodes = append(nodes, bhomebus.NetNode{})
 
+				reg := &LogRegister {
+					nodes,
+					LogSaveTopic,
+					payload,
+				}
+
+				logCallback(reg)
+			}
 		default:
 			time.Sleep(10*time.Millisecond)
 		}

--
Gitblit v1.8.0