From b3d0abe5c752afec512706322e841ffcac73c79e Mon Sep 17 00:00:00 2001
From: yanghui <yanghui@aiotlink.com>
Date: 星期二, 27 四月 2021 15:03:18 +0800
Subject: [PATCH] consolidate log

---
 logc.go |   60 ++++++++++++++++++++++++++++--------------------------------
 1 files changed, 28 insertions(+), 32 deletions(-)

diff --git a/logc.go b/logc.go
index 5946d85..e3f36f5 100644
--- a/logc.go
+++ b/logc.go
@@ -26,6 +26,12 @@
 	TypeSysInfo //绯荤粺鍙傛暟鍙樻洿,ip,server name,寮�鍏虫満淇℃伅
 )
 
+type LogPrinter interface {
+	Marshal() ([]byte, error)
+	Topic() string
+}
+
+
 type LogInfo struct {
 	ID        string `gorm:"column:id;primary_key;unique" json:"id"`
 	Timestamp string `gorm:"column:timestamp" json:"timestamp"` // 2020-12-03 14:39:41
@@ -47,16 +53,14 @@
 type LogReportCallback func(*LogRegister)
 
 var (
-	opChan chan *OperationLog
-	schedualeChan chan *ScheduleLog
+	logCh chan LogPrinter
 	logger *Log
 	ProcName string
 	ProcID string
 )
 
 func Init(flogWriter LogReportCallback, log *Log, procId string, procName string, wg *sync.WaitGroup, done  chan struct{}) bool {
-	opChan = make(chan *OperationLog, 100)
-	schedualeChan = make(chan *ScheduleLog)
+	logCh = make(chan LogPrinter, 300)
 
 	if nil != log {
 		logger = log
@@ -80,17 +84,7 @@
 	log.ProcName = ProcName
 	log.ProcID = ProcID
 
-	select {
-	case opChan <- log:
-		return
-	case <-time.After(timeout):
-		var info string
-		b, err := json.Marshal(log)
-		if nil == err {
-			info = string(b)
-		}
-		logger.Fatal("SaveOperationLog failed to save log", info)
-	}
+	deliverLog(log, timeout)
 }
 
 func SaveScheduleLog(category, level int, timeout time.Duration, v ...interface{}) {
@@ -112,19 +106,22 @@
 		Info:      msg,
 	}
 
+	deliverLog(log, timeout)
+}
+
+func deliverLog(l LogPrinter, timeout time.Duration)  {
 	select {
-	case schedualeChan <- log:
+	case logCh <- l:
 		return
 	case <-time.After(timeout):
 		var info string
-		b, err := json.Marshal(log)
+		b, err := l.Marshal()
 		if nil == err {
 			info = string(b)
 		}
-		logger.Fatal("SaveScheduleLog failed to save log", info)
+		logger.Fatal("SaveScheduleLog failed to save log", info, l.Topic())
 	}
 }
-
 
 //func Save(level int, logType int, v ...interface{}) {
 //	cache(level, logType, "", "", v)
@@ -165,6 +162,7 @@
 
 const (
 	OperationLogTopic = "operationLogSaveTopic"
+	ScheduleLogTopic = "scheduleLogSaveTopic"
 )
 
 func saveLoop(logCallback LogReportCallback, wg *sync.WaitGroup, done  chan struct{}) {
@@ -178,24 +176,22 @@
 		select {
 		case <- done:
 			return
-		case data := <- opChan:
-			payload, err := json.Marshal(data)
+		case log := <- logCh:
+			payload, err := log.Marshal()
 			if err != nil {
-				fmt.Println("json.Marshal(operation data) error:", data)
+				logger.Error("failed to Marshal", log)
 			} else {
-				var nodes []bhomebus.NetNode
-				nodes = append(nodes, bhomebus.NetNode{})
+					var nodes []bhomebus.NetNode
+					nodes = append(nodes, bhomebus.NetNode{})
 
-				reg := &LogRegister {
-					nodes,
-					OperationLogTopic,
-					payload,
-				}
+					reg := &LogRegister {
+						nodes,
+						log.Topic(),
+						payload,
+					}
 
-				logCallback(reg)
+					logCallback(reg)
 			}
-		default:
-			time.Sleep(10*time.Millisecond)
 		}
 	}
 }

--
Gitblit v1.8.0