yanghui
2021-06-16 584e19a2cd83a305c14ad7460e97bf4e49805fff
logc.go
@@ -2,7 +2,7 @@
import (
   "basic.com/valib/bhomebus.git"
   "encoding/json"
   "basic.com/valib/bhshmq.git/proto/source/bhome_msg"
   "fmt"
   "sync"
   "time"
@@ -26,6 +26,12 @@
   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"`
   Timestamp string `gorm:"column:timestamp" json:"timestamp"` // 2020-12-03 14:39:41
@@ -39,7 +45,7 @@
}
type LogRegister struct {
   Nodes      []bhomebus.NetNode
   Nodes      []bhome_msg.BHAddress
   Topic      string
   Payload     []byte
}
@@ -47,16 +53,14 @@
type LogReportCallback func(*LogRegister)
var (
   opChan chan *OperationLog
   schedualeChan chan *ScheduleLog
   logCh chan LogPrinter
   logger *Log
   ProcName string
   ProcID string
)
func Init(flogWriter LogReportCallback, log *Log, procId string, procName string, wg *sync.WaitGroup, done  chan struct{}) bool {
   opChan = make(chan *OperationLog, 100)
   schedualeChan = make(chan *ScheduleLog)
   logCh = make(chan LogPrinter, 300)
   if nil != log {
      logger = log
@@ -80,17 +84,7 @@
   log.ProcName = ProcName
   log.ProcID = ProcID
   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)
   }
   deliverLog(log, timeout)
}
func SaveScheduleLog(category, level int, timeout time.Duration, v ...interface{}) {
@@ -100,7 +94,7 @@
   }
   if msg == "" {
      retrun
      return
   }
   log := &ScheduleLog{
@@ -112,19 +106,22 @@
      Info:      msg,
   }
   deliverLog(log, timeout)
}
func deliverLog(l LogPrinter, timeout time.Duration) {
   select {
   case schedualeChan <- log:
   case logCh <- l:
      return
   case <-time.After(timeout):
      var info string
      b, err := json.Marshal(log)
      b, err := l.Marshal()
      if nil == err {
         info = string(b)
      }
      logger.Fatal("SaveScheduleLog failed to save log", info)
      logger.Fatal("SaveScheduleLog failed to save log", info, l.Topic())
   }
}
//func Save(level int, logType int, v ...interface{}) {
//   cache(level, logType, "", "", v)
@@ -165,6 +162,7 @@
const (
   OperationLogTopic = "operationLogSaveTopic"
   ScheduleLogTopic = "scheduleLogSaveTopic"
)
func saveLoop(logCallback LogReportCallback, wg *sync.WaitGroup, done  chan struct{}) {
@@ -178,24 +176,22 @@
      select {
      case <- done:
         return
      case data := <- opChan:
         payload, err := json.Marshal(data)
      case log := <- logCh:
         payload, err := log.Marshal()
         if err != nil {
            fmt.Println("json.Marshal(operation data) error:", data)
            logger.Error("failed to Marshal", log)
         } else {
            var nodes []bhomebus.NetNode
            nodes = append(nodes, bhomebus.NetNode{})
               var nodes []bhomebus.NetNode
               nodes = append(nodes, bhomebus.NetNode{})
            reg := &LogRegister {
               nodes,
               OperationLogTopic,
               payload,
            }
               reg := &LogRegister {
                  nodes,
                  log.Topic(),
                  payload,
               }
            logCallback(reg)
               logCallback(reg)
         }
      default:
         time.Sleep(10*time.Millisecond)
      }
   }
}