yanghui
2021-04-26 09a13020cc8810839af97afc28ab62cd83a0f344
logc.go
@@ -2,10 +2,10 @@
import (
   "basic.com/valib/bhomebus.git"
   "encoding/json"
   "fmt"
   uuid "github.com/satori/go.uuid"
   "sync"
   "time"
)
@@ -18,13 +18,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,38 +39,78 @@
   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
   //bhSock *bhomebus.Socket
   //pubFn func(nodes []bhomebus.NetNode, topic string, data []byte, milliseconds int) int
   ProcName string
   ProcID string
)
func Init(sock *bhomebus.Socket, procId string, procName string) {
func Init(flogWriter LogReportCallback, procId string, procName string, wg *sync.WaitGroup, done  chan struct{}) bool {
   msgChan = make(chan []byte, 100)
   ProcName = procName
   ProcID = procId
   bhSock = sock
   go saveLoop()
   go saveLoop(flogWriter, wg, done)
   return true
}
func Save(level int, logType int,template string, v ...interface{}) {
   cache(level, logType, "", "", template, v)
//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, template string, v ...interface{}) {
   cache(level, logType, userID, userName, template, 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, template string, fmtArgs []interface{}) {
func cache(level int, logType int, userID string, userName string, fmtArgs []interface{}) {
   // Format with Sprint, Sprintf, or neither.
   msg := template
   if msg == "" && len(fmtArgs) > 0 {
   msg := ""
   if len(fmtArgs) > 0 {
      msg = fmt.Sprint(fmtArgs...)
   } else if msg != "" && len(fmtArgs) > 0 {
      msg = fmt.Sprintf(template, fmtArgs...)
   }
   fmt.Println(msg)
   log := LogInfo {
      ID:        uuid.NewV4().String(),
@@ -97,19 +137,31 @@
   LogSaveTopic = "logSaveTopic"
)
func saveLoop() {
func saveLoop(logCallback LogReportCallback, wg *sync.WaitGroup, done  chan struct{}) {
   defer wg.Done()
   if nil == logCallback {
      return
   }
   for {
      select {
      case <- done:
         return
      case data := <- msgChan:
         var nodes []bhomebus.NetNode
         nodes = append(nodes, bhomebus.NetNode{
            Key: 8,
         })
         bhSock.PubTimeout(nodes, LogSaveTopic, data, 1000)
         nodes = append(nodes, bhomebus.NetNode{})
         reg := &LogRegister {
            nodes,
            LogSaveTopic,
            data,
         }
         logCallback(reg)
      default:
         time.Sleep(10*time.Millisecond)
      }
   }
}