From cf01dcde2de2be62f01f938417e72ff48d6abd6e Mon Sep 17 00:00:00 2001
From: yanghui <yanghui@aiotlink.com>
Date: 星期一, 26 四月 2021 14:49:39 +0800
Subject: [PATCH] logc api done

---
 logc.go         |  136 +++++++++++++++++++--------------------------
 operationlog.go |   14 ++++
 2 files changed, 71 insertions(+), 79 deletions(-)

diff --git a/logc.go b/logc.go
index 494c91f..7eddc7e 100644
--- a/logc.go
+++ b/logc.go
@@ -4,7 +4,6 @@
 	"basic.com/valib/bhomebus.git"
 	"encoding/json"
 	"fmt"
-	uuid "github.com/satori/go.uuid"
 	"sync"
 	"time"
 )
@@ -48,90 +47,65 @@
 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
 
 )
 
 
 func Init(flogWriter LogReportCallback, procId string, procName string, wg *sync.WaitGroup, done  chan struct{}) bool {
-	msgChan = make(chan []byte, 100)
-	ProcName = procName
-	ProcID = procId
+	opChan = make(chan *OperationLog, 100)
+	//ProcName = procName
+	//ProcID = procId
 
 	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
+	}
+}
+
+
+
+//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,
 //	}
 //
-//	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
+//	data,err := json.Marshal(log)
+//	if err != nil {
+//		fmt.Println("json.Marshal(log) error:", log)
+//		return
 //	}
 //
-//	go saveLoop()
-//
-//	return true
+//	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"
@@ -148,18 +122,22 @@
 		select {
 		case <- done:
 			return
-		case data := <- msgChan:
-			var nodes []bhomebus.NetNode
-			nodes = append(nodes, bhomebus.NetNode{})
+		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,
-				data,
+				reg := &LogRegister {
+					nodes,
+					LogSaveTopic,
+					payload,
+				}
+
+				logCallback(reg)
 			}
-
-			logCallback(reg)
-
 		default:
 			time.Sleep(10*time.Millisecond)
 		}
diff --git a/operationlog.go b/operationlog.go
new file mode 100644
index 0000000..423d25e
--- /dev/null
+++ b/operationlog.go
@@ -0,0 +1,14 @@
+package logc
+
+type OperationLog struct {
+	ID        string `gorm:"column:id;primaryKey;unique;autoIncrement" json:"id"` // 涓婚敭id
+	Timestamp int64  `gorm:"column:timestamp" json:"timestamp"`                   // 鏃堕棿鎴砋nix time
+	UserName  string `gorm:"column:userName" json:"userName"`                     // 鐢ㄦ埛鍚嶅瓧
+	Module    string `gorm:"column:module" json:"module"`                         // 妯″潡
+	Function  string `gorm:"column:function" json:"function"`                     // 鍔熻兘
+	Result    string `gorm:"column:result" json:"result"`                         // 鎿嶄綔缁撴灉
+	Para      string `gorm:"column:para" json:"para"`                             // 鍙傛暟
+	Ip        string `gorm:"column:ip" json:"ip"`                                 // 璇︽儏
+}
+
+

--
Gitblit v1.8.0