From 584e19a2cd83a305c14ad7460e97bf4e49805fff Mon Sep 17 00:00:00 2001
From: yanghui <yanghui@aiotlink.com>
Date: 星期三, 16 六月 2021 19:15:06 +0800
Subject: [PATCH] test

---
 logc.go |  196 ++++++++++++++++++++++++++++--------------------
 1 files changed, 113 insertions(+), 83 deletions(-)

diff --git a/logc.go b/logc.go
index 494c91f..3c79b5c 100644
--- a/logc.go
+++ b/logc.go
@@ -2,9 +2,8 @@
 
 import (
 	"basic.com/valib/bhomebus.git"
-	"encoding/json"
+	"basic.com/valib/bhshmq.git/proto/source/bhome_msg"
 	"fmt"
-	uuid "github.com/satori/go.uuid"
 	"sync"
 	"time"
 )
@@ -27,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
@@ -40,7 +45,7 @@
 }
 
 type LogRegister struct {
-	Nodes      []bhomebus.NetNode
+	Nodes      []bhome_msg.BHAddress
 	Topic      string
 	Payload     []byte
 }
@@ -48,17 +53,21 @@
 type LogReportCallback func(*LogRegister)
 
 var (
-	msgChan chan []byte
-	//bhSock *bhomebus.Socket
-	//pubFn func(nodes []bhomebus.NetNode, topic string, data []byte, milliseconds int) int
+	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 {
+	logCh = make(chan LogPrinter, 300)
 
-func Init(flogWriter LogReportCallback, procId string, procName string, wg *sync.WaitGroup, done  chan struct{}) bool {
-	msgChan = make(chan []byte, 100)
+	if nil != log {
+		logger = log
+	} else {
+		logger = &Log{}
+	}
+
 	ProcName = procName
 	ProcID = procId
 
@@ -67,74 +76,93 @@
 	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
-//	}
-//
-//	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)
+func SaveOperationLog(log *OperationLog, timeout time.Duration) {
+	if nil == log {
 		return
 	}
 
-	msgChan <- data
+	log.ProcName = ProcName
+	log.ProcID = ProcID
+
+	deliverLog(log, timeout)
 }
 
+func SaveScheduleLog(category, level int, timeout time.Duration, v ...interface{}) {
+	msg := ""
+	if len(v) > 0 {
+		msg = fmt.Sprint(v...)
+	}
+
+	if msg == "" {
+		return
+	}
+
+	log := &ScheduleLog{
+		Timestamp: time.Now().Unix(),
+		ProcName:  ProcName,
+		ProcID:    ProcID,
+		Level:     level,
+		Type:      category,
+		Info:      msg,
+	}
+
+	deliverLog(log, timeout)
+}
+
+func deliverLog(l LogPrinter, timeout time.Duration) {
+	select {
+	case logCh <- l:
+		return
+	case <-time.After(timeout):
+		var info string
+		b, err := l.Marshal()
+		if nil == err {
+			info = string(b)
+		}
+		logger.Fatal("SaveScheduleLog failed to save log", info, l.Topic())
+	}
+}
+
+//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"
+	OperationLogTopic = "operationLogSaveTopic"
+	ScheduleLogTopic = "scheduleLogSaveTopic"
 )
 
 func saveLoop(logCallback LogReportCallback, wg *sync.WaitGroup, done  chan struct{}) {
@@ -148,20 +176,22 @@
 		select {
 		case <- done:
 			return
-		case data := <- msgChan:
-			var nodes []bhomebus.NetNode
-			nodes = append(nodes, bhomebus.NetNode{})
+		case log := <- logCh:
+			payload, err := log.Marshal()
+			if err != nil {
+				logger.Error("failed to Marshal", log)
+			} else {
+					var nodes []bhomebus.NetNode
+					nodes = append(nodes, bhomebus.NetNode{})
 
-			reg := &LogRegister {
-				nodes,
-				LogSaveTopic,
-				data,
+					reg := &LogRegister {
+						nodes,
+						log.Topic(),
+						payload,
+					}
+
+					logCallback(reg)
 			}
-
-			logCallback(reg)
-
-		default:
-			time.Sleep(10*time.Millisecond)
 		}
 	}
 }

--
Gitblit v1.8.0