From eb4a9d53d2ec2a21c1f1088573ef8287bdca572b Mon Sep 17 00:00:00 2001
From: yanghui <yanghui@aiotlink.com>
Date: 星期一, 26 四月 2021 18:18:13 +0800
Subject: [PATCH] rename
---
logc.go | 173 ++++++++++++++++++++++++++++++++-------------------------
1 files changed, 98 insertions(+), 75 deletions(-)
diff --git a/logc.go b/logc.go
index 79462d9..e660ed7 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"
)
@@ -39,102 +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 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"
+ OperationLogTopic = "operationLogSaveTopic"
)
-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,
+ OperationLogTopic,
+ payload,
+ }
+
+ logCallback(reg)
+ }
default:
time.Sleep(10*time.Millisecond)
}
}
}
-
-
--
Gitblit v1.8.0