From 54796f5e2227764ef21449c2ddb0593d092bd6aa Mon Sep 17 00:00:00 2001
From: yanghui <yanghui@aiotlink.com>
Date: 星期一, 26 四月 2021 17:17:49 +0800
Subject: [PATCH] complete log

---
 logc.go |  177 ++++++++++++++++++++++++++++++++---------------------------
 1 files changed, 96 insertions(+), 81 deletions(-)

diff --git a/logc.go b/logc.go
index 9a978df..dca99a3 100644
--- a/logc.go
+++ b/logc.go
@@ -2,12 +2,9 @@
 
 import (
 	"basic.com/valib/bhomebus.git"
-	"bytes"
-	"net/url"
-
 	"encoding/json"
 	"fmt"
-	uuid "github.com/satori/go.uuid"
+	"sync"
 	"time"
 )
 
@@ -41,108 +38,126 @@
 	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
-	pubFn func(nodes []bhomebus.NetNode, topic string, data []byte, milliseconds int) int
-	ProcName string
-	ProcID string
+	opChan chan *OperationLog
+	logger *Log
 )
 
-func init() {
 
-}
-
-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 Init(flogWriter LogReportCallback, log *Log, procId string, procName string, wg *sync.WaitGroup, done  chan struct{}) bool {
+	opChan = make(chan *OperationLog, 100)
+	if nil != log {
+		logger = log
+	} else {
+		logger = &Log{}
 	}
 
-	go saveLoop()
+	//ProcName = procName
+	//ProcID = procId
+
+	go saveLoop(flogWriter, wg, done)
 
 	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
+	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)
+	}
 }
+
+
+
+//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)
 		}
 	}
-}
-
-func Log(userName, method, path, contentType, module string, body *bytes.Buffer, values url.Values) {
-
 }

--
Gitblit v1.8.0