package logc
|
|
import (
|
"basic.com/valib/c_bhomebus.git/proto/source/bhome_msg"
|
"fmt"
|
"sync"
|
"time"
|
)
|
|
const (
|
LevelFatal = iota + 1
|
LevelError
|
LevelWarn
|
LevelInfo
|
LevelDebug
|
)
|
|
const (
|
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"`
|
Timestamp string `gorm:"column:timestamp" json:"timestamp"` // 2020-12-03 14:39:41
|
ProcName string `gorm:"column:procName" json:"procName"`
|
ProcID string `gorm:"column:procID" json:"procID"`
|
Level int `gorm:"column:level" json:"level"` // 日志等级
|
Type int `gorm:"column:type" json:"type"` // 操作类型:人工操作,登录退出,轮循摄像机及对应算法,数据栈处理情况,异常情况等
|
UserID string `gorm:"column:userID" json:"userID"` // 用户id
|
UserName string `gorm:"column:userName" json:"userName"` // 用户名字
|
Info string `gorm:"column:info" json:"info"` // 详情
|
}
|
|
type LogRegister struct {
|
Nodes []bhome_msg.BHAddress
|
Topic string
|
Payload []byte
|
}
|
|
type LogReportCallback func(*LogRegister)
|
|
var (
|
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)
|
|
if nil != log {
|
logger = log
|
} else {
|
logger = &Log{}
|
}
|
|
ProcName = procName
|
ProcID = procId
|
|
go saveLoop(flogWriter, wg, done)
|
|
return true
|
}
|
|
func SaveOperationLog(log *OperationLog, timeout time.Duration) {
|
if nil == log {
|
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 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"
|
)
|
|
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)
|
}
|
}
|
}
|
}
|