RoleLog.go 1.41 KB
package models

import (
	"encoding/json"
	"fmt"
	"pro2d/common"
	"pro2d/common/logger"
	"sync/atomic"
)

var LogType = map[string]string{
	"in_item":   "common",
	"out_item":  "common",
	"in_hero":   "common",
	"out_hero":  "common",
	"in_equip":  "common",
	"out_equip": "common",

	"role_action": "common",
}

var commonRoleField = []string{
	"id",
	"uid",
	"name",
	"device",
	"level",
}

func (m *RoleModel) MyLog(logType string, content map[string]interface{}) {
	_, ok := LogType[logType]
	if !ok {
		logger.Error("LOG ERROR: new logType [%s] need Add Maping.", logType)
		return
	}

	doc := make(map[string]interface{})
	for _, field := range commonRoleField {
		if content[field] != nil {
			logger.Error("LOG ERROR: logType [%s] had field [%s] overwrite default.", logType, field)
		}

		v := m.GetProperty(field)
		if v != nil {
			doc[field] = v
		}
	}
	content["ucode"] = m.GetActionUnicode()
	for field, value := range content {
		doc[field] = value
	}
	doc["@type"] = logType
	j, err := json.Marshal(doc)
	if err != nil {
		logger.Error("LOG ERROR: logType [%s]  json.Marshal", logType)
		return
	}
	logger.Debug(j)
}

func (m *RoleModel) StartActionUnicode() {
	uniqueCount := atomic.AddInt64(&m.uniqueCount, 1)
	m.actionUcode = fmt.Sprintf("%s-%d-%d", m.Data.Id, common.Timex(), uniqueCount)
}

func (m *RoleModel) EndActionUnicode() {
	m.actionUcode = ""
}

func (m *RoleModel) GetActionUnicode() string {
	return m.actionUcode
}