Blame view

src/models/RoleLog.lua 5.18 KB
4c5d72ab   zhouhaihai   高级pvp
1
  
df883a11   zhouhaihai   日志处理
2
  -- logType
4c5d72ab   zhouhaihai   高级pvp
3
  local LogType = {
df883a11   zhouhaihai   日志处理
4
5
6
  	create = "common",
  	login = "common",
  	logout = "common",
e3c5cc5e   zhouhaihai   跨服竞技场over
7
  	gm = "common",
4c5d72ab   zhouhaihai   高级pvp
8
9
  }
  
df883a11   zhouhaihai   日志处理
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  -- 如要修改 要提前修改 _template mapping -- 对应 mapping 为 gamelog-*
  local Mapping = {
  	-- 预留一些数据格式
  	common = {
  		desc = "keyword",--索引的短字符串
  		ucode = "keyword",--关联日志对应ucode
  		key1 = "keyword", --可索引的短字符串
  		key2 = "keyword", --可索引的短字符串
  		-- 几乎不用的长文本 
  		text1 = "text",  --长字符串不索引的类型
  		-- 五个不同类型的数字 基本上满足数量要求 尽量从低到高用
  		short1 = "short", 
  		int1 = "integer",
  		int2 = "integer",
  		long1 = "long",
  		float1 = "float",
  	}
  }
4c5d72ab   zhouhaihai   高级pvp
28
  
df883a11   zhouhaihai   日志处理
29
30
31
32
33
34
35
36
37
38
39
  -- 所有的日志都包括的部分 role 里面的信息  -- mapping 信息在 gamelog-role
  local commonRoleField = {
  	name = "keyword",
  	id = "integer",
  	uid = "keyword",
  	sid = "short",
  	device = "keyword",
  	ctime = "integer",
  	ltime = "integer",
  	level = "short",
  	rmbC = "integer",
4c5d72ab   zhouhaihai   高级pvp
40
41
  }
  
4c5d72ab   zhouhaihai   高级pvp
42
  
df883a11   zhouhaihai   日志处理
43
44
45
46
47
48
  local function checkType(logType, field, value, ctype)
  	local typecheckfunc = {
  		keyword = function()
  			--长度不超过256
  			if type(value) ~= "string" then
  				value = tostring(value)
e3c5cc5e   zhouhaihai   跨服竞技场over
49
  				print(string.format("LOG ERROR: logType [%s] field [%s] isn't [keyword], value : %s", logType, field, value))
df883a11   zhouhaihai   日志处理
50
51
  			else
  				if #value > 256 then
e3c5cc5e   zhouhaihai   跨服竞技场over
52
  					print(string.format("LOG ERROR: logType [%s] field [%s] [keyword] type to long. value : %s", logType, field, value))
df883a11   zhouhaihai   日志处理
53
54
55
56
57
58
59
  				end
  			end
  			return value
  		end,
  		text = function()
  			if type(value) ~= "string" then
  				value = tostring(value)
e3c5cc5e   zhouhaihai   跨服竞技场over
60
  				print(string.format("LOG ERROR: logType [%s] field [%s] isn't [text], value : %s", logType, field, value))
df883a11   zhouhaihai   日志处理
61
62
63
64
65
66
  			end
  			return value
  		end,
  		integer = function()
  			if type(value) ~= "number" then
  				value = tonumber(value)
e3c5cc5e   zhouhaihai   跨服竞技场over
67
  				print(string.format("LOG ERROR: logType [%s] field [%s] isn't [integer], value : %s", logType, field, value))
df883a11   zhouhaihai   日志处理
68
69
70
71
72
73
  			end
  			if value then
  				if math.type(value) ~= "integer" then
  					local oldValue = value
  					value = math.floor(value)
  					if value ~= oldValue then
e3c5cc5e   zhouhaihai   跨服竞技场over
74
  						print(string.format("LOG ERROR: logType [%s] field [%s] isn't [integer], is float, value : %s", logType, field, value))
df883a11   zhouhaihai   日志处理
75
76
77
78
  					end
  				end
  				if -2147483648 > value or value > 2147483647 then
  					value = nil
e3c5cc5e   zhouhaihai   跨服竞技场over
79
  					print(string.format("LOG ERROR: logType [%s] field [%s] isn't [integer], too big, value : %s", logType, field, value))
df883a11   zhouhaihai   日志处理
80
81
82
83
84
85
86
  				end
  			end 
  			return value
  		end,
  		short = function()
  			if type(value) ~= "number" then
  				value = tonumber(value)
e3c5cc5e   zhouhaihai   跨服竞技场over
87
  				print(string.format("LOG ERROR: logType [%s] field [%s] isn't [short], value : %s", logType, field, value))
df883a11   zhouhaihai   日志处理
88
89
90
91
92
93
  			end
  			if value then
  				if math.type(value) ~= "integer" then
  					local oldValue = value
  					value = math.floor(value)
  					if value ~= oldValue then
e3c5cc5e   zhouhaihai   跨服竞技场over
94
  						print(string.format("LOG ERROR: logType [%s] field [%s] isn't [short], is float, value : %s", logType, field, value))
df883a11   zhouhaihai   日志处理
95
96
97
98
99
  					end
  				end
  
  				if -32768 > value or value > 32768 then
  					value = nil
e3c5cc5e   zhouhaihai   跨服竞技场over
100
  					print(string.format("LOG ERROR: logType [%s] field [%s] isn't [short], too big, value : %s", logType, field, value))
df883a11   zhouhaihai   日志处理
101
102
103
104
105
106
107
  				end
  			end 
  			return value
  		end,
  		long = function()
  			if type(value) ~= "number" then
  				value = tonumber(value)
e3c5cc5e   zhouhaihai   跨服竞技场over
108
  				print(string.format("LOG ERROR: logType [%s] field [%s] isn't [long], value : %s", logType, field, value))
df883a11   zhouhaihai   日志处理
109
110
111
112
113
114
115
  			end
  			if value then
  				if math.type(value) ~= "integer" then
  					local oldValue = value
  					value = math.floor(value)
  					if type(value) ~= "integer" then
  						value = nil
e3c5cc5e   zhouhaihai   跨服竞技场over
116
  						print(string.format("LOG ERROR: logType [%s] field [%s] isn't [long], too big, value : %s", logType, field, value))
df883a11   zhouhaihai   日志处理
117
  					elseif value ~= oldValue then
e3c5cc5e   zhouhaihai   跨服竞技场over
118
  						print(string.format("LOG ERROR: logType [%s] field [%s] isn't [long], is float, value : %s", logType, field, value))
df883a11   zhouhaihai   日志处理
119
120
121
122
123
124
125
126
  					end
  				end
  			end 
  			return value
  		end,
  		float = function()
  			if type(value) ~= "number" then
  				value = tonumber(value)
e3c5cc5e   zhouhaihai   跨服竞技场over
127
  				print(string.format("LOG ERROR: logType [%s] field [%s] isn't [float], value : %s", logType, field, value))
df883a11   zhouhaihai   日志处理
128
129
130
131
132
133
134
135
136
137
138
139
  			end
  			return value
  		end,
  	}
  
  	if typecheckfunc[ctype] then
  		return typecheckfunc[ctype]()
  	else
  		print(string.format("LOG ERROR: logType [%s] field [%s] have a new type [%s] need add check.", logType, field, ctype))
  		return nil
  	end
  end
4c5d72ab   zhouhaihai   高级pvp
140
  
df883a11   zhouhaihai   日志处理
141
142
  local RoleLog = {}
  function RoleLog.bind(Role)
4c5d72ab   zhouhaihai   高级pvp
143
  	function Role:log(logType, contents)
4c5d72ab   zhouhaihai   高级pvp
144
  		contents = contents or {}
df883a11   zhouhaihai   日志处理
145
146
147
148
149
  		local _logType = LogType[logType]
  		if not _logType then 
  			print(string.format("LOG ERROR: new logType [%s] need Add Maping.", logType))
  			return 
  		end
4c5d72ab   zhouhaihai   高级pvp
150
  		local doc = {}
df883a11   zhouhaihai   日志处理
151
152
153
154
155
  		for field, ctype in pairs(commonRoleField) do
  			if contents[field] then
  				print(string.format("LOG ERROR: logType [%s] had field [%s] overwrite default.", logType, field))
  			end
  			doc[field] = checkType("commonRoleField", field, self:getProperty(field), ctype)
4c5d72ab   zhouhaihai   高级pvp
156
  		end
df883a11   zhouhaihai   日志处理
157
158
159
  
  		local mapping = Mapping[_logType]
  
4c5d72ab   zhouhaihai   高级pvp
160
  		for field, value in pairs(contents) do
df883a11   zhouhaihai   日志处理
161
162
163
  			local ftype = mapping[field]
  			if ftype then
  				doc[field] = checkType(logType, field, value, ftype)
4c5d72ab   zhouhaihai   高级pvp
164
  			else
df883a11   zhouhaihai   日志处理
165
  				print(string.format("LOG ERROR: logType [%s] have new field [%s] no type in mapping.", logType, field))
4c5d72ab   zhouhaihai   高级pvp
166
167
  			end
  		end
df883a11   zhouhaihai   日志处理
168
169
  		if not logd then return end
  		pcall(skynet.send, logd, "lua", "log", logType, doc, _logType)
4c5d72ab   zhouhaihai   高级pvp
170
  	end
4c5d72ab   zhouhaihai   高级pvp
171
  end
4c5d72ab   zhouhaihai   高级pvp
172
  return RoleLog