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 |  155 +++++++++++++++++++++++++++++++--------------------
 1 files changed, 93 insertions(+), 62 deletions(-)

diff --git a/logc.go b/logc.go
index 99ce25d..7eddc7e 100644
--- a/logc.go
+++ b/logc.go
@@ -2,10 +2,9 @@
 
 import (
 	"basic.com/valib/bhomebus.git"
-
 	"encoding/json"
 	"fmt"
-	uuid "github.com/satori/go.uuid"
+	"sync"
 	"time"
 )
 
@@ -18,13 +17,13 @@
 )
 
 const (
-	TypeManual = iota + 1
-	TypeLoginOut
-	TypePollInfo
-	TypeStackInfo
-	TypeWarnInfo
-	TypeRunInfo
-	TypeSysInfo
+	TypeManual = iota + 1  //浜哄伐鎿嶄綔鏃ュ織,绯荤粺鍗囩骇,鎽勫儚鏈轰慨鏀�,绠楁硶淇敼...
+	TypeLoginOut //鐧诲綍鐧诲嚭鏃ュ織
+	TypePollInfo //杞贰鎽勫儚鏈哄強鍏跺搴旂殑绠楁硶
+	TypeStackInfo //鏁版嵁鏍堝鐞嗘儏鍐�
+	TypeWarnInfo //鏁呴殰淇℃伅
+	TypeRunInfo //杩愯鎯呭喌,gpu,mem,cpu
+	TypeSysInfo //绯荤粺鍙傛暟鍙樻洿,ip,server name,寮�鍏虫満淇℃伅
 )
 
 type LogInfo struct {
@@ -39,76 +38,108 @@
 	Info      string `gorm:"column:info" json:"info"`         // 璇︽儏
 }
 
+type LogRegister struct {
+	Nodes      []bhomebus.NetNode
+	Topic      string
+	Payload     []byte
+}
+
+type LogReportCallback func(*LogRegister)
+
 var (
-	msgChan chan []byte
-	bhSock *bhomebus.Socket
-	ProcName string
-	ProcID string
+	opChan chan *OperationLog
+
 )
 
-func Init(sock *bhomebus.Socket, procId string, procName string) {
-	msgChan = make(chan []byte, 100)
-	ProcName = procName
-	ProcID = procId
-	bhSock = sock
 
-	go saveLoop()
+func Init(flogWriter LogReportCallback, procId string, procName string, wg *sync.WaitGroup, done  chan struct{}) bool {
+	opChan = make(chan *OperationLog, 100)
+	//ProcName = procName
+	//ProcID = procId
+
+	go saveLoop(flogWriter, wg, done)
+
+	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...)
+func SaveOperationLog(log *OperationLog) {
+	if nil != log {
+		opChan <- log
 	}
-	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{
-				Key: 8,
-			})
-			bhSock.PubTimeout(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 {
+				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