yanghui
2021-04-27 2c4f305069988a0b1a9a0ece466c6eef286dbfd7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package logc
 
import (
    "basic.com/valib/bhomebus.git"
    "encoding/json"
    "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 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      []bhomebus.NetNode
    Topic      string
    Payload     []byte
}
 
type LogReportCallback func(*LogRegister)
 
var (
    opChan chan *OperationLog
    logger *Log
)
 
 
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
 
    go saveLoop(flogWriter, wg, done)
 
    return true
}
 
func SaveOperationLog(log *OperationLog, timeout time.Duration) {
    if nil == log {
        return
    }
 
    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)
    }
}
 
func SaveScheduleLog(category, level int, msg string) {
 
}
 
 
//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"
)
 
func saveLoop(logCallback LogReportCallback, wg *sync.WaitGroup, done  chan struct{}) {
    defer wg.Done()
 
    if nil == logCallback {
        return
    }
 
    for {
        select {
        case <- done:
            return
        case data := <- opChan:
            payload, err := json.Marshal(data)
            if err != nil {
                fmt.Println("json.Marshal(operation data) error:", data)
            } else {
                var nodes []bhomebus.NetNode
                nodes = append(nodes, bhomebus.NetNode{})
 
                reg := &LogRegister {
                    nodes,
                    OperationLogTopic,
                    payload,
                }
 
                logCallback(reg)
            }
        default:
            time.Sleep(10*time.Millisecond)
        }
    }
}