BattleLogManager.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. BattleLogManager = {}
  2. local this = BattleLogManager
  3. local isLog = nil
  4. if AppConst and AppConst.isOpenBLog ~= nil then
  5. isLog = AppConst.isOpenBLog
  6. else
  7. isLog = true --< 服务端跑战斗开启
  8. end
  9. local function pairsByKeys(t)
  10. local a = {}
  11. for n in pairs(t) do
  12. if n then
  13. a[#a+1] = n
  14. end
  15. end
  16. table.sort(a, function( op1, op2 )
  17. local type1, type2 = type(op1), type(op2)
  18. local num1, num2 = tonumber(op1), tonumber(op2)
  19. if ( num1 ~= nil) and (num2 ~= nil) then
  20. return num1 < num2
  21. elseif type1 ~= type2 then
  22. return type1 < type2
  23. elseif type1 == "string" then
  24. return op1 < op2
  25. elseif type1 == "boolean" then
  26. return op1
  27. -- 以上处理: number, string, boolean
  28. else -- 处理剩下的: function, table, thread, userdata
  29. return tostring(op1) < tostring(op2) -- tostring后比较字符串
  30. end
  31. end)
  32. local i = 0
  33. return function()
  34. i = i + 1
  35. return a[i], t[a[i]]
  36. end
  37. end
  38. function this.PrintBattleTable(tb)
  39. local indent_str = "{"
  40. local count = 0
  41. for k,v in pairs(tb) do
  42. count = count + 1
  43. end
  44. for k=1, #tb do
  45. local v = tb[k]
  46. if type(v) == "table" then
  47. indent_str = indent_str .. this.PrintBattleTable(v)
  48. elseif type(v) == "string" then
  49. indent_str = indent_str .. "\""..tostring(v) .. "\""
  50. else
  51. indent_str = indent_str .. tostring(v)
  52. end
  53. if k < count then
  54. indent_str = indent_str..","
  55. end
  56. end
  57. local index = 0
  58. for k,v in pairsByKeys(tb) do
  59. index = index + 1
  60. if type(k) ~= "number" then
  61. if type(v) == "table" then
  62. indent_str = string.format("%s%s=%s", indent_str, tostring(k), this.PrintBattleTable(v))
  63. elseif type(v) == "string" then
  64. indent_str = string.format("%s%s=\"%s\"", indent_str, tostring(k), tostring(v))
  65. else
  66. indent_str = string.format("%s%s=%s", indent_str, tostring(k), tostring(v))
  67. end
  68. if index < count then
  69. indent_str = indent_str .. ","
  70. end
  71. end
  72. end
  73. indent_str = indent_str .. "}"
  74. return indent_str
  75. end
  76. --
  77. function this.Init(fightdata)
  78. this.timestamp = Random.GetSeed()
  79. this.fightdata = fightdata
  80. this.logList = {}
  81. this.logListCa = {}
  82. end
  83. function this.Log(...)
  84. -- Log(...)
  85. if not isLog then return end
  86. local args = {...}
  87. local log = args[1] .. ":\n"
  88. log = log .. string.format("%s = %s, ", "frame", BattleLogic.CurFrame())
  89. for i = 2, #args, 2 do
  90. local key = args[i]
  91. local value = args[i + 1]
  92. log = log .. string.format("%s = %s, ", key, value)
  93. end
  94. table.insert(this.logList, log)
  95. end
  96. function this.WriteFile()
  97. if not isLog then return end
  98. local time = string.format("%d-%d-%d-%d-%d-%d",
  99. os.date("%Y"),
  100. os.date("%m"),
  101. os.date("%d"),
  102. os.date("%H"),
  103. os.date("%M"),
  104. os.date("%S"))
  105. local file
  106. local platform
  107. -- linux
  108. if not file then
  109. file = io.open("../luafight/BattleRecord/log-ServerFightData-"..time..".txt", "a")
  110. platform = "Linux"
  111. end
  112. -- local window server
  113. if not file then
  114. file = io.open("luafight/BattleRecord/log-ServerFightData-"..time..".txt", "a")
  115. platform = "Local Windows Server"
  116. end
  117. -- local
  118. if not file then
  119. file = io.open("BattleRecord/log-ServerFightData-"..time..".txt", "a")
  120. platform = "Local"
  121. end
  122. file:write(platform..":\n\n\n")
  123. for i = 1, #this.logList do
  124. file:write(this.logList[i].."\n")
  125. end
  126. file:write("\n\n\n\n\n")
  127. file:write("fightData:\n")
  128. file:write(this.PrintBattleTable(this.fightdata))
  129. file:write("\n\n")
  130. -- this.PLog(file, "S ", this.fightdata)
  131. file:write("\n\n")
  132. file:write("timeStamp: " .. this.timestamp)
  133. io.close(file)
  134. end
  135. function this.logProtoPkg(file, LOGPrefix, prtPkg , deep)
  136. local tDeep = deep or 0
  137. local space = string.rep(' ', 4)
  138. local pkgStr = tostring(prtPkg)
  139. local splitTab = string.split(pkgStr,'\n')
  140. for i,v in ipairs(splitTab) do
  141. file:write(string.format(LOGPrefix .. "%s%s",string.rep(space, tDeep),v))
  142. file:write("\n")
  143. end
  144. end
  145. -- 判断是否是protoTable
  146. function this.isProtoTable( tab )
  147. local bbb = tostring(tab)
  148. local hasTable = string.find(bbb, "table:")
  149. if hasTable ~= nil and hasTable == 1 then
  150. return false
  151. end
  152. return true
  153. end
  154. function this.logTab(file, LOGPrefix, tab)
  155. if type(tab) ~= "table" then
  156. file:write(tostring(tab))
  157. file:write("\n")
  158. return
  159. end
  160. -- 防止proto死循环
  161. if this.isProtoTable(tab) then
  162. file:write(LOGPrefix .. "Proto-Table")
  163. file:write("\n")
  164. file:write(LOGPrefix .. "{")
  165. file:write("\n")
  166. this.logProtoPkg(file, LOGPrefix,tab,1)
  167. file:write(LOGPrefix .. "}")
  168. file:write("\n")
  169. return
  170. end
  171. local space, deep = string.rep(" ", 4), 1
  172. local function _dump(t)
  173. for k, v in pairs(t) do
  174. local key = tostring(k)
  175. if key == "class" or key == "_listener_for_children" or key == "_message_descriptor" or key == "_listener" then
  176. --todo
  177. elseif type(v) == "table" then
  178. -- 防止proto死循环
  179. local isProto = this.isProtoTable(v)
  180. if isProto then
  181. file:write(string.format(LOGPrefix .. "%s[%s] => Proto-Table",string.rep(space, deep),key))
  182. file:write("\n")
  183. file:write(string.format(LOGPrefix .. "%s{",string.rep(space, deep)))
  184. file:write("\n")
  185. this.logProtoPkg(file, LOGPrefix,v,deep+1)
  186. file:write(string.format(LOGPrefix .. "%s}", string.rep(space, deep)))
  187. file:write("\n")
  188. else
  189. file:write(string.format(LOGPrefix .. "%s[%s] => Table",string.rep(space, deep),key))
  190. file:write("\n")
  191. file:write(string.format(LOGPrefix .. "%s{",string.rep(space, deep)))
  192. file:write("\n")
  193. deep = deep + 1
  194. _dump(v)
  195. deep = deep - 1
  196. file:write(string.format(LOGPrefix .. "%s}", string.rep(space, deep)))
  197. file:write("\n")
  198. end
  199. else
  200. if type(v) ~= "string" then
  201. v = tostring(v)
  202. end
  203. file:write(string.format(LOGPrefix .. "%s[%s] => %s", string.rep(space, deep), key, v))
  204. file:write("\n")
  205. end
  206. end
  207. end
  208. file:write(string.format(LOGPrefix .. "Table"))
  209. file:write("\n")
  210. file:write(LOGPrefix .. "{")
  211. file:write("\n")
  212. _dump(tab)
  213. file:write(string.format(LOGPrefix .. "}\n"))
  214. file:write("\n")
  215. end
  216. function this.PLog(file, LOGPrefix, fmt, ...)
  217. if type(fmt) == "table" then
  218. this.logTab(file, LOGPrefix, fmt)
  219. elseif type(fmt) == "boolean" or type(fmt) == "nil" or type(fmt) == "function" or type(fmt) == "userdata" then
  220. file:write(LOGPrefix, fmt)
  221. file:write("\n")
  222. else
  223. local r, r1, r2 = pcall(string.format, fmt, ...)
  224. if r then
  225. file:write(LOGPrefix .. r1)
  226. file:write("\n")
  227. else
  228. file:write(LOGPrefix .. "ERROR FORMAT", r1, r2)
  229. file:write("\n")
  230. end
  231. end
  232. end
  233. --> 输出战斗log
  234. function this.WriteServerFightData(data, time, sign)
  235. -- if not isLog then return end
  236. -- local file
  237. -- local platform
  238. -- -- linux
  239. -- if not file then
  240. -- file = io.open("../luafight/BattleRecord/log-ServerFightData" .. time .. "_" .. sign .. ".txt", "a+")
  241. -- platform = "Linux"
  242. -- end
  243. -- -- local window server
  244. -- if not file then
  245. -- file = io.open("luafight/BattleRecord/log-ServerFightData" .. time .. "_" .. sign .. ".txt", "a+")
  246. -- platform = "Local Windows Server"
  247. -- end
  248. -- -- local
  249. -- if not file then
  250. -- file = io.open("BattleRecord/log-ServerFightData" .. time .. "_" .. sign .. ".txt", "a+")
  251. -- platform = "Local"
  252. -- end
  253. --
  254. -- file:write(platform..":\n\n\n")
  255. --
  256. -- file:write(this.PrintBattleTable(data))
  257. -- file:write("\n\n")
  258. -- file:write("timeStamp: " .. Random.GetSeed())
  259. -- io.close(file)
  260. end
  261. --> 用于战斗log标识
  262. function this.WriteServerFightDataB(time, sign)
  263. -- if not isLog then return end
  264. -- local file
  265. -- local platform
  266. -- -- linux
  267. -- if not file then
  268. -- file = io.open("../luafight/BattleRecord/log-ServerFightData" .. time .. "_" .. sign .. ".txt", "a+")
  269. -- platform = "Linux"
  270. -- end
  271. -- -- local window server
  272. -- if not file then
  273. -- file = io.open("luafight/BattleRecord/log-ServerFightData" .. time .."_" .. sign .. ".txt", "a+")
  274. -- platform = "Local Windows Server"
  275. -- end
  276. -- -- local
  277. -- if not file then
  278. -- file = io.open("BattleRecord/log-ServerFightData" .. time .."_" .. sign .. ".txt", "a+")
  279. -- platform = "Local"
  280. -- end
  281. --
  282. -- file:write(platform..":\n\n\n")
  283. --
  284. -- file:write(sign)
  285. -- file:write("\n\n")
  286. -- file:write("timeStamp: " .. Random.GetSeed())
  287. -- io.close(file)
  288. end
  289. function this.LogCa(...)
  290. if not isLog then return end
  291. local args = {...}
  292. local log = args[1] .. ":\n"
  293. log = log .. string.format("%s = %s, ", "frame", BattleLogic.CurFrame())
  294. for i = 2, #args, 2 do
  295. local key = args[i]
  296. local value = args[i + 1]
  297. log = log .. string.format("%s = %s, ", key, value)
  298. end
  299. table.insert(this.logListCa, log)
  300. end
  301. return BattleLogManager