| | |
| | | |
| | | import ( |
| | | "basic.com/valib/bhomebus.git" |
| | | "bytes" |
| | | "net/url" |
| | | |
| | | "encoding/json" |
| | | "fmt" |
| | | uuid "github.com/satori/go.uuid" |
| | | "sync" |
| | | "time" |
| | | ) |
| | | |
| | |
| | | Info string `gorm:"column:info" json:"info"` // 详情 |
| | | } |
| | | |
| | | type LogRegister struct { |
| | | Nodes []bhomebus.NetNode |
| | | Topic string |
| | | Payload []byte |
| | | } |
| | | |
| | | type LogReportCallback func(*LogRegister) |
| | | |
| | | var ( |
| | | msgChan chan []byte |
| | | bhSock *bhomebus.Socket |
| | | pubFn func(nodes []bhomebus.NetNode, topic string, data []byte, milliseconds int) int |
| | | ProcName string |
| | | ProcID string |
| | | opChan chan *OperationLog |
| | | logger *Log |
| | | ) |
| | | |
| | | func init() { |
| | | |
| | | } |
| | | |
| | | func InitBySock(sock *bhomebus.Socket, procId string, procName string) bool { |
| | | msgChan = make(chan []byte, 100) |
| | | ProcName = procName |
| | | ProcID = procId |
| | | bhSock = sock |
| | | if bhSock == nil { |
| | | return false |
| | | 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{} |
| | | } |
| | | |
| | | go saveLoop() |
| | | //ProcName = procName |
| | | //ProcID = procId |
| | | |
| | | go saveLoop(flogWriter, wg, done) |
| | | |
| | | return true |
| | | } |
| | | |
| | | func InitByPubFn(fn func(nodes []bhomebus.NetNode, topic string, data []byte, milliseconds int) int, |
| | | procId string, procName string) bool { |
| | | msgChan = make(chan []byte, 100) |
| | | ProcName = procName |
| | | ProcID = procId |
| | | pubFn = fn |
| | | if pubFn == nil { |
| | | return false |
| | | } |
| | | |
| | | go saveLoop() |
| | | |
| | | return true |
| | | } |
| | | |
| | | 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) |
| | | func SaveOperationLog(log *OperationLog, timeout time.Duration) { |
| | | if nil == log { |
| | | return |
| | | } |
| | | |
| | | msgChan <- data |
| | | 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 ( |
| | | LogSaveTopic = "logSaveTopic" |
| | | OperationLogTopic = "operationLogSaveTopic" |
| | | ) |
| | | |
| | | func saveLoop() { |
| | | func saveLoop(logCallback LogReportCallback, wg *sync.WaitGroup, done chan struct{}) { |
| | | defer wg.Done() |
| | | |
| | | if nil == logCallback { |
| | | return |
| | | } |
| | | |
| | | for { |
| | | select { |
| | | case data := <- msgChan: |
| | | var nodes []bhomebus.NetNode |
| | | nodes = append(nodes, bhomebus.NetNode{}) |
| | | if bhSock != nil { |
| | | bhSock.PubTimeout(nodes, LogSaveTopic, data, 1000) |
| | | } else if pubFn != nil { |
| | | pubFn(nodes, LogSaveTopic, data, 1000) |
| | | case <- done: |
| | | return |
| | | case data := <- opChan: |
| | | payload, err := json.Marshal(data) |
| | | if err != nil { |
| | | fmt.Println("json.Marshal(operation data) error:", data) |
| | | } else { |
| | | fmt.Println("bhSock nil and pubFn nil") |
| | | } |
| | | var nodes []bhomebus.NetNode |
| | | nodes = append(nodes, bhomebus.NetNode{}) |
| | | |
| | | reg := &LogRegister { |
| | | nodes, |
| | | OperationLogTopic, |
| | | payload, |
| | | } |
| | | |
| | | logCallback(reg) |
| | | } |
| | | default: |
| | | time.Sleep(10*time.Millisecond) |
| | | } |
| | | } |
| | | } |
| | | |
| | | func Log(userName, method, path, contentType, module string, body *bytes.Buffer, values url.Values) { |
| | | |
| | | } |