| | |
| | | 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 |
| | |
| | | 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 |
| | |
| | | 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{}) { |
| | |
| | | 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) |
| | |
| | | |
| | | const ( |
| | | OperationLogTopic = "operationLogSaveTopic" |
| | | ScheduleLogTopic = "scheduleLogSaveTopic" |
| | | ) |
| | | |
| | | func saveLoop(logCallback LogReportCallback, wg *sync.WaitGroup, done chan struct{}) { |
| | |
| | | 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{}) |
| | | |
| | | reg := &LogRegister { |
| | | nodes, |
| | | OperationLogTopic, |
| | | log.Topic(), |
| | | payload, |
| | | } |
| | | |
| | | logCallback(reg) |
| | | } |
| | | default: |
| | | time.Sleep(10*time.Millisecond) |
| | | } |
| | | } |
| | | } |