| | |
| | | package logc |
| | | |
| | | import ( |
| | | "basic.com/valib/bhomebus.git" |
| | | "encoding/json" |
| | | "basic.com/valib/c_bhomebus.git/proto/source/bhome_msg" |
| | | "fmt" |
| | | "sync" |
| | | "time" |
| | |
| | | ) |
| | | |
| | | 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"` |
| | |
| | | } |
| | | |
| | | type LogRegister struct { |
| | | Nodes []bhomebus.NetNode |
| | | Topic string |
| | | Payload []byte |
| | | Nodes []bhome_msg.BHAddress |
| | | Topic string |
| | | Payload []byte |
| | | } |
| | | |
| | | type LogReportCallback func(*LogRegister) |
| | | |
| | | var ( |
| | | opChan chan *OperationLog |
| | | logger *Log |
| | | 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 { |
| | | logCh = make(chan LogPrinter, 300) |
| | | |
| | | 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{} |
| | | } |
| | | |
| | | //ProcName = procName |
| | | //ProcID = procId |
| | | ProcName = procName |
| | | ProcID = procId |
| | | |
| | | go saveLoop(flogWriter, wg, done) |
| | | |
| | |
| | | return |
| | | } |
| | | |
| | | log.ProcName = ProcName |
| | | log.ProcID = ProcID |
| | | |
| | | deliverLog(log, timeout) |
| | | } |
| | | |
| | | func SaveScheduleLog(category, level int, timeout time.Duration, v ...interface{}) { |
| | | msg := "" |
| | | if len(v) > 0 { |
| | | msg = fmt.Sprint(v...) |
| | | } |
| | | |
| | | 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 opChan <- 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("SaveOperationLog 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" |
| | | OperationLogTopic = "operationLogSaveTopic" |
| | | ScheduleLogTopic = "scheduleLogSaveTopic" |
| | | RuleServerLogTopic = "ruleServerLogSaveTopic" |
| | | VaSystemLogTopic = "VaSystemLogSaveTopic" |
| | | ) |
| | | |
| | | func saveLoop(logCallback LogReportCallback, wg *sync.WaitGroup, done chan struct{}) { |
| | | func saveLoop(logCallback LogReportCallback, wg *sync.WaitGroup, done chan struct{}) { |
| | | defer wg.Done() |
| | | |
| | | if nil == logCallback { |
| | |
| | | |
| | | for { |
| | | select { |
| | | case <- done: |
| | | 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 []bhome_msg.BHAddress |
| | | nodes = append(nodes, bhome_msg.BHAddress{}) |
| | | |
| | | reg := &LogRegister { |
| | | reg := &LogRegister{ |
| | | nodes, |
| | | OperationLogTopic, |
| | | log.Topic(), |
| | | payload, |
| | | } |
| | | |
| | | logCallback(reg) |
| | | } |
| | | default: |
| | | time.Sleep(10*time.Millisecond) |
| | | } |
| | | } |
| | | } |