120 lines
2.6 KiB
Go
120 lines
2.6 KiB
Go
package f5
|
|
|
|
import "os"
|
|
import "fmt"
|
|
import "sync"
|
|
import "time"
|
|
import "q5"
|
|
|
|
const TGLOG_ROOT = "/data/logs/%s/upload/"
|
|
const POLY_TGLOG_ROOT = "/data/logs/%s/%s/upload/"
|
|
const TGLOG_FILENAME = "log_%d_%s.log"
|
|
|
|
type TGLogMsgNode struct {
|
|
gameId int32
|
|
jsonStr string
|
|
next *TGLogMsgNode
|
|
}
|
|
|
|
type TGLog_ struct {
|
|
isPolyLog bool
|
|
topNode *TGLogMsgNode
|
|
botNode *TGLogMsgNode
|
|
msgMutex sync.Mutex
|
|
chGoSaveExit chan int
|
|
}
|
|
|
|
func (this *TGLog_) Init() {
|
|
this.chGoSaveExit = make(chan int)
|
|
go this.goSaveToFile()
|
|
}
|
|
|
|
func (this *TGLog_) UnInit() {
|
|
this.chGoSaveExit <- 1
|
|
}
|
|
|
|
func (this *TGLog_) SetPolyLog(isPolyLog bool) {
|
|
this.isPolyLog = isPolyLog
|
|
}
|
|
|
|
func (this *TGLog_) AddTrackLog(
|
|
gameId int32,
|
|
accountId string,
|
|
remoteAddr string,
|
|
logClass1 int32,
|
|
logClass2 int32,
|
|
prop *q5.XObject) {
|
|
eventName := fmt.Sprintf("event_%d_%d", logClass1, logClass2)
|
|
this.AddTrackLogEx(gameId, accountId, remoteAddr, eventName, prop)
|
|
}
|
|
|
|
func (this *TGLog_) AddTrackLogEx(
|
|
gameId int32,
|
|
accountId string,
|
|
remoteAddr string,
|
|
eventName string,
|
|
prop *q5.XObject) {
|
|
var xobj *q5.MutableXObject
|
|
xobj.SetXValue("#account_id", new(q5.XValue).SetString(accountId))
|
|
xobj.SetXValue("#type", new(q5.XValue).SetString("track"))
|
|
xobj.SetXValue("#time", new(q5.XValue).SetString(time.Now().Format("2006-01-02 15:04:05")))
|
|
xobj.SetXValue("#event_name", new(q5.XValue).SetString("eventName"))
|
|
|
|
xobj.SetXObject("#properties", prop)
|
|
|
|
p := new(TGLogMsgNode)
|
|
p.gameId = gameId
|
|
p.jsonStr = xobj.ToJsonStr()
|
|
this.msgMutex.Lock()
|
|
defer this.msgMutex.Unlock()
|
|
if this.botNode != nil {
|
|
this.botNode.next = p
|
|
this.botNode = p
|
|
} else {
|
|
this.topNode = p
|
|
this.botNode = p
|
|
}
|
|
}
|
|
|
|
func (this *TGLog_) goSaveToFile() {
|
|
var workNode *TGLogMsgNode
|
|
for {
|
|
select {
|
|
case <-this.chGoSaveExit:
|
|
return
|
|
case <-time.After(time.Millisecond * 1000 * 10):
|
|
}
|
|
this.msgMutex.Lock()
|
|
workNode = this.topNode
|
|
this.topNode = nil
|
|
this.botNode = nil
|
|
this.msgMutex.Unlock()
|
|
if workNode != nil {
|
|
for workNode != nil {
|
|
f := this.getLogFile(workNode.gameId)
|
|
if f != nil {
|
|
f.Write([]byte(workNode.jsonStr + "\n"))
|
|
f.Close()
|
|
}
|
|
workNode = workNode.next
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (this *TGLog_) getLogFile(gameId int32) *os.File {
|
|
fileName := fmt.Sprintf(TGLOG_FILENAME, os.Getpid(), time.Now().Format("20060102"))
|
|
logDir := ""
|
|
if this.isPolyLog {
|
|
logDir = fmt.Sprintf(POLY_TGLOG_ROOT, App.GetPkgName(), gameId)
|
|
} else {
|
|
logDir = fmt.Sprintf(TGLOG_ROOT, App.GetPkgName())
|
|
}
|
|
q5.ForceCreateDir(logDir)
|
|
if f, err := os.OpenFile(logDir + fileName, os.O_WRONLY|os.O_APPEND, 0666); err == nil {
|
|
return f
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|