zhangzengfei
2022-01-22 5413b950ac6693e24ac1cd7d8974ab8aa270b644
logc.go
@@ -1,11 +1,9 @@
package logc
import (
   "basic.com/valib/bhomebus.git"
   "encoding/json"
   "basic.com/valib/c_bhomebus.git/proto/source/bhome_msg"
   "fmt"
   uuid "github.com/satori/go.uuid"
   "sync"
   "time"
)
@@ -18,14 +16,19 @@
)
const (
   TypeManual = iota + 1  //人工操作日志,系统升级,摄像机修改,算法修改...
   TypeLoginOut //登录登出日志
   TypePollInfo //轮巡摄像机及其对应的算法
   TypeStackInfo //数据栈处理情况
   TypeWarnInfo //故障信息
   TypeRunInfo //运行情况,gpu,mem,cpu
   TypeSysInfo //系统参数变更,ip,server name,开关机信息
   TypeManual    = iota + 1 //人工操作日志,系统升级,摄像机修改,算法修改...
   TypeLoginOut             //登录登出日志
   TypePollInfo             //轮巡摄像机及其对应的算法
   TypeStackInfo            //数据栈处理情况
   TypeWarnInfo             //故障信息
   TypeRunInfo              //运行情况,gpu,mem,cpu
   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"`
@@ -39,76 +42,160 @@
   Info      string `gorm:"column:info" json:"info"`         // 详情
}
type LogRegister struct {
   Nodes   []bhome_msg.BHAddress
   Topic   string
   Payload []byte
}
type LogReportCallback func(*LogRegister)
var (
   msgChan chan []byte
   bhSock *bhomebus.Socket
   logCh    chan LogPrinter
   logger   *Log
   ProcName string
   ProcID string
   ProcID   string
)
func Init(sock *bhomebus.Socket, procId string, procName string) {
   msgChan = make(chan []byte, 100)
func Init(flogWriter LogReportCallback, log *Log, procId string, procName string, wg *sync.WaitGroup, done chan struct{}) bool {
   logCh = make(chan LogPrinter, 300)
   if nil != log {
      logger = log
   } else {
      logger = &Log{}
   }
   ProcName = procName
   ProcID = procId
   bhSock = sock
   go saveLoop()
   go saveLoop(flogWriter, wg, done)
   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)
}
const (
   LogSaveTopic = "logSaveTopic"
)
func SaveScheduleLog(category, level int, timeout time.Duration, v ...interface{}) {
   msg := ""
   if len(v) > 0 {
      msg = fmt.Sprint(v...)
   }
func saveLoop() {
   for {
      select {
      case data := <- msgChan:
         var nodes []bhomebus.NetNode
         nodes = append(nodes, bhomebus.NetNode{
            Key: 8,
         })
         bhSock.PubTimeout(nodes, LogSaveTopic, data, 1000)
      default:
         time.Sleep(10*time.Millisecond)
   if msg == "" {
      return
   }
   log := &ScheduleLog{
      Timestamp: time.Now().Unix(),
      ProcName:  ProcName,
      ProcID:    ProcID,
      Level:     level,
      Type:      category,
      Info:      msg,
   }
   deliverLog(log, timeout)
}
func SaveRuleServerLog(ruleServerPushLog RuleServerPushLog, timeout time.Duration) {
   deliverLog(&ruleServerPushLog, 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 (
   OperationLogTopic  = "operationLogSaveTopic"
   ScheduleLogTopic   = "scheduleLogSaveTopic"
   RuleServerLogTopic = "ruleServerLogSaveTopic"
   VaSystemLogTopic   = "VaSystemLogSaveTopic"
)
func saveLoop(logCallback LogReportCallback, wg *sync.WaitGroup, done chan struct{}) {
   defer wg.Done()
   if nil == logCallback {
      return
   }
   for {
      select {
      case <-done:
         return
      case log := <-logCh:
         payload, err := log.Marshal()
         if err != nil {
            logger.Error("failed to Marshal", log)
         } else {
            var nodes []bhome_msg.BHAddress
            nodes = append(nodes, bhome_msg.BHAddress{})
            reg := &LogRegister{
               nodes,
               log.Topic(),
               payload,
            }
            logCallback(reg)
         }
      }
   }
}