pet_home_server/src/server/game/LogMgr.go
2024-11-21 19:36:12 +08:00

78 lines
1.5 KiB
Go

package game
import (
"server/db"
"server/game/mod/item"
"server/pkg/github.com/name5566/leaf/log"
"sync"
"github.com/robfig/cron/v3"
)
const (
Login_log = 1
LoginOut_log = 2
Event_log = 3
)
type LogMgr struct {
L []*Log
McronSave *cron.Cron
Lock sync.Mutex
}
type Log struct {
Uid int32
Type int32
Event string
Items []*item.Item
TimeStamp int64
}
func (L *LogMgr) InitManager() {
L.McronSave = cron.New()
L.L = make([]*Log, 0, 10)
L.McronSave.AddFunc("@every 10s", func() {
L.Lock.Lock()
defer L.Lock.Unlock()
for _, v := range L.L {
switch v.Type {
case Login_log:
v.InsertLoginLog()
case LoginOut_log:
v.InsertLoginLog()
case Event_log:
v.InsertEventLog()
default:
log.Debug("unknown log type %d", v.Type)
continue
}
}
L.L = L.L[:0]
})
L.McronSave.Start()
}
func (L *LogMgr) AddLog(logs *Log) {
return
L.Lock.Lock()
defer L.Lock.Unlock()
L.L = append(L.L, logs)
}
func (Log *Log) InsertEventLog() {
sqlStr := "INSERT INTO t_log_event (dwUin, type, event, timestamp) VALUES (?, ?, ?, ?)"
_, err := db.SqlDb.Exec(sqlStr, Log.Uid, Log.Type, Log.Event, Log.TimeStamp)
if err != nil {
log.Debug("log insert event log error %s", err.Error())
}
}
func (Log *Log) InsertLoginLog() {
sqlStr := "INSERT INTO t_log_login (dwUin, type, event, timestamp) VALUES (?, ?, ?, ?)"
_, err := db.SqlDb.Exec(sqlStr, Log.Uid, Log.Type, Log.Event, Log.TimeStamp)
if err != nil {
log.Debug("log insert event log error %s", err.Error())
}
}