BattleLogManager = {} local this = BattleLogManager local isEditor = AppConst and AppConst.Platform == "EDITOR" local isMobile = AppConst and (AppConst.Platform == "ANDROID" or AppConst.Platform == "IOS") local isDebug = false function LogBattle(str) if isDebug then if isEditor then LogGreen(str) else print(str) end end end local isLog = (true and not isMobile) or isEditor local function pairsByKeys(t) local a = {} for n in pairs(t) do if n then a[#a+1] = n end end table.sort(a, function( op1, op2 ) local type1, type2 = type(op1), type(op2) local num1, num2 = tonumber(op1), tonumber(op2) if ( num1 ~= nil) and (num2 ~= nil) then return num1 < num2 elseif type1 ~= type2 then return type1 < type2 elseif type1 == "string" then return op1 < op2 elseif type1 == "boolean" then return op1 -- 以上处理: number, string, boolean else -- 处理剩下的: function, table, thread, userdata return tostring(op1) < tostring(op2) -- tostring后比较字符串 end end) local i = 0 return function() i = i + 1 return a[i], t[a[i]] end end function this.PrintBattleTable(tb) local indent_str = "{" local count = 0 for k,v in pairs(tb) do count = count + 1 end for k=1, #tb do local v = tb[k] if type(v) == "table" then indent_str = indent_str .. this.PrintBattleTable(v) elseif type(v) == "string" then indent_str = indent_str .. "\""..tostring(v) .. "\"" else indent_str = indent_str .. tostring(v) end if k < count then indent_str = indent_str.."," end end local index = 0 for k,v in pairsByKeys(tb) do index = index + 1 if type(k) ~= "number" then if type(v) == "table" then indent_str = string.format("%s%s=%s", indent_str, tostring(k), this.PrintBattleTable(v)) elseif type(v) == "string" then indent_str = string.format("%s%s=\"%s\"", indent_str, tostring(k), tostring(v)) else indent_str = string.format("%s%s=%s", indent_str, tostring(k), tostring(v)) end if index < count then indent_str = indent_str .. "," end end end indent_str = indent_str .. "}" return indent_str end -- function this.Init(fightdata, userdata) this.timestamp = Random.GetSeed() this.fightdata = fightdata if userdata then this.userId = userdata.uid end this.logList = {} this.WriteFightDataToFile() end function this.Log(...) if not isLog then return end local args = {...} local log = args[1] .. ":\n" log = log .. string.format("%s = %s, ", "frame", BattleLogic.CurFrame()) for i = 2, #args, 2 do local key = args[i] local value = args[i + 1] log = log .. string.format("%s = %s, ", key, value) end table.insert(this.logList, log) -- 如果log大于100条写一便日志到文件 if #this.logList > 100 then this.WriteLogToFile() end end function this.GetFile(wType) local time = string.format("%d-%d-%d-%d-%d-%d", os.date("%Y"), os.date("%m"), os.date("%d"), os.date("%H"), os.date("%M"), os.date("%S")) local file local platform -- linux if not file then file = io.open("../luafight/BattleRecord/log-"..(this.userId or time)..".txt", wType) platform = "Linux" end -- local window server if not file then file = io.open("luafight/BattleRecord/log-"..(this.userId or time)..".txt", wType) platform = "Local Windows Server" end -- local if not file then file = io.open("BattleRecord/log-"..(this.userId or time)..".txt", wType) platform = "Local" end return file, platform end -- 将战斗数据写入log function this.WriteFightDataToFile() if not isLog then return end local file, platform = this.GetFile("w") file:write(platform..":\n\n\n") file:write("fightData:\n") file:write(this.PrintBattleTable(this.fightdata)) file:write("\n\n") file:write("timeStamp: " .. this.timestamp) file:write("\n\n\n\n\n") io.close(file) end -- 将日志写入log function this.WriteLogToFile() if not isLog then return end local file, platform = this.GetFile("a") for i = 1, #this.logList do file:write(this.logList[i].."\n") end this.logList = {} io.close(file) end return BattleLogManager