local LogFileManager = {} LogFileManager.key = "LogFiles"; LogFileManager.logFiles = {}; LogFileManager.keyNew = "LogFilesNew"; LogFileManager.logFilesNew = {}; LogFileManager.romSetting = {} LogFileManager.maxFileNum = 200; LogFileManager.listWillUpload = {} LogFileManager.maxUploadingNum = 3; -- 允许同时上传的最大数量 LogFileManager.curUploadingNum = 0; -- 正在上传的文件数量 --[[ self.logFiles = { ["20190226"] = {[1] = "Game-20190226-001.log", [2] = "Game-20190226-002.log" = true,...}, ["20190227"] = {[1] = "Game-20190227-001.log", [2] = "Game-20190227-002.log" = true,...}, ["20190228"] = {[1] = "Game-20190228-001.log", [2] = "Game-20190228-002.log" = true,...}, } self.logFileNew = { ["20190529101103"] = {path="D:/WorkSpace/DingDing/Core/debug/src/storage/Game-20190529-101103.log",day="20190529",idx="101103"}, ["20190529101104"] = {path="D:/WorkSpace/DingDing/Core/debug/src/storage/Game-20190529-101104.log",day="20190529",idx="101104"}, ["20190529101105"] = {path="D:/WorkSpace/DingDing/Core/debug/src/storage/Game-20190529-101105.log",day="20190529",idx="101105"}, ["20190529101106"] = {path="D:/WorkSpace/DingDing/Core/debug/src/storage/Game-20190529-101106.log",day="20190529",idx="101106"}, ["20190529101107"] = {path="D:/WorkSpace/DingDing/Core/debug/src/storage/Game-20190529-101107.log",day="20190529",idx="101107"}, ["20190530101101"] = {path="D:/WorkSpace/DingDing/Core/debug/src/storage/Game-20190530-101101.log",day="20190530",idx="101101"}, } --]] function LogFileManager:init(romSetting) logD("LogFileManager:init()"); self.romSetting = romSetting; self:loadDatas(); self:addCurrentLogFile(); self:clearOldLogFiles(); self:printDatas(); end function LogFileManager:printDatas() logD("LogFileManager:printDatas()"); logD("--------------begin---------------") if self.logFilesNew then for k,v in pairsByKeys(self.logFilesNew) do logD(tostring(v.path)) end end logD("--------------end---------------") end function LogFileManager:loadDatas() local logFiles = cc.UserDefault:getInstance():getStringForKey(self.key, ""); local logFilesNew = cc.UserDefault:getInstance():getStringForKey(self.keyNew, ""); self.logFiles = json.decode(logFiles) or {}; self.logFilesNew = json.decode(logFilesNew) or {}; if not self.logFilesNew or table_nums(self.logFilesNew) == 0 then self.logFilesNew = {} if self.logFiles then for day,v in pairs(self.logFiles) do for idx,vv in pairs(v) do local key = tostring(day)..tostring(idx) local info = { path = vv, day = day, idx = idx, } self.logFilesNew[key] = info; end end end end end function LogFileManager:saveDatas() local logFilesNew = json.encode(self.logFilesNew) cc.UserDefault:getInstance():setStringForKey(self.key, "") cc.UserDefault:getInstance():setStringForKey(self.keyNew, logFilesNew) cc.UserDefault:getInstance():flush() end function LogFileManager:parseLogFileName(fileName) logD("LogFileManager:parseLogFileName() fileName = ", fileName); local arr = split(fileName, "%p"); local n = #arr; -- 旧版日志 if arr[n-1] == "game" then logD("LogFileManager:parseLogFileName() this log file is old format"); return nil, nil; end -- 新版日志 local day = arr[n-2] local idx = arr[n-1] return day,idx end -- 添加一个日志文件 -- fileName : 日志文件名字,不带路径 -- day : 日志文件对应的日志,格式为"20190101" function LogFileManager:addCurrentLogFile() logD("LogFileManager:addCurrentLogFile()"); local logFileName = cc.Logger:getLogFile(); logD("LogFileManager:addCurrentLogFile() ", logFileName); local day, idx = self:parseLogFileName(logFileName) if not day or not idx then logD("LogFileManager:addCurrentLogFile() this log file is old format"); return end local key = tostring(day)..tostring(idx) local info = { path = logFileName, day = day, idx = idx, } self.logFilesNew[key] = info; self:saveDatas(); end -- 清理太旧的日志文件 -- 只保留最近的10个 function LogFileManager:clearOldLogFiles() local tt = {} for k,v in pairs(self.logFilesNew) do table.insert(tt, v) end table.sort(tt, function(a,b ) if a.day ~= b.day then return tonumber(a.day) > tonumber(b.day) else return tonumber(a.idx) > tonumber(b.idx) end end) if #tt > self.maxFileNum then for i=self.maxFileNum+1,#tt do local info = tt[i] self:deleteFileByKey(info.day, info.idx); end end end function LogFileManager:deleteFileByKey(day, idx) logD("LogFileManager:deleteFileByKey()", day, idx); local key = tostring(day)..tostring(idx) if not self.logFilesNew[key] then return end local filePath = self.logFilesNew[key].path if cc.FileSystem:fileExists(filePath) then os.remove(filePath); end self.logFilesNew[key] = nil; self:saveDatas(); end -- 删除某一天的所有日志 function LogFileManager:deleteFileByDay(day) logD("LogFileManager:deleteFileByDay()", day); for k,v in pairs(self.logFilesNew) do if v.day == day then self:deleteFileByKey(v.day, v.idx) end end end function LogFileManager:doUplaod() if self.isUploading then return end if #self.listWillUpload <= 0 then return end logD("LogFileManager:doUplaod() curUploadingNum()", self.curUploadingNum); logD("LogFileManager:doUplaod() maxUploadingNum()", self.maxUploadingNum); if self.curUploadingNum >= self.maxUploadingNum then return end local info = table.remove(self.listWillUpload, 1); local actorId = loadUserId() or 0 local fileKey = tostring(info.day)..tostring(info.idx) local clientFileName = info.path if not cc.FileSystem:fileExists(clientFileName) then logD("LogFileManager:doUplaod() file not exist!", clientFileName); return; end local remoteFileName = tostring(cc.Application:getInstance():getTargetPlatform()) .. "_" .. "version" .. "_" .. tostring(actorId) .. "_" .. os.date("%Y%m%d%H%M%S", os.time()) .. "_" .. tostring(fileKey) .. "_" .. string.format("%08X.7z" , math.random(1 , 2147483647)); local function onUploadEnd(ret1, ret2) logD("LogFileManager:doUplaod() onUploadEnd()", ret1, ret2, clientFileName, remoteFileName); self.curUploadingNum = tonumber(self.curUploadingNum) - 1 self:doUplaod(); end converUrlToIp(self.romSetting.LogFTPIP, function(urlNew, header) self.curUploadingNum = tonumber(self.curUploadingNum) + 1 cc.CURLManager:getInstance():uploadLogWithFileName(urlNew,self.romSetting.LogFTPUserName, self.romSetting.LogFTPPassword, clientFileName, remoteFileName ,onUploadEnd); end) end -- 上传文件,一组接一组上传。有效避免上传失败的问题 function LogFileManager:uploadFileOneByOne(info) logD("LogFileManager:uploadFileOneByOne()", info.path); table.insert(self.listWillUpload, info) self:doUplaod(); end -- 上传文件 - 极大概率会有很多文件上传失败 -- info = {path="D:/WorkSpace/DingDing/Core/debug/src/storage/Game-20190529-101103.log",day="20190529",idx="101103"} function LogFileManager:uploadFile(info) logD("LogFileManager:uploadFile()", info.path); local actorId = loadUserId() or 0 local fileKey = tostring(info.day)..tostring(info.idx) local clientFileName = info.path if not cc.FileSystem:fileExists(clientFileName) then logD("LogFileManager:uploadFile() file not exist!", clientFileName); return; end local remoteFileName = tostring(cc.Application:getInstance():getTargetPlatform()) .. "_" .. "version" .. "_" .. tostring(actorId) .. "_" .. os.date("%Y%m%d%H%M%S", os.time()) .. "_" .. tostring(fileKey) .. "_" .. string.format("%08X.7z" , math.random(1 , 2147483647)); local function onUploadEnd(ret1, ret2) logD("LogFileManager:uploadFile() onUploadEnd()", ret1, ret2, clientFileName, remoteFileName); end converUrlToIp(self.romSetting.LogFTPIP, function(urlNew, header) cc.CURLManager:getInstance():uploadLogWithFileName(urlNew,self.romSetting.LogFTPUserName, self.romSetting.LogFTPPassword, clientFileName, remoteFileName ,onUploadEnd); end) end -- 上传某一天的所有日志 function LogFileManager:uploadFilesByDay(day) logD("LogFileManager:uploadFilesByDay()", day); local status = 2 for k,info in pairs(self.logFilesNew) do if info.day == day then status = 1 self:uploadFileOneByOne(info) end end self:responseUpload(day,status) end function LogFileManager:responseUpload(day, status) logD("LogFileManager:responseUpload()", day, status); local tt = { action = "system.logconfirm", uid = loadUserId(), appid = getAppId(), date = day, status = status, } for k,v in pairs(tt) do logD( k ..", "..v) end httpPost(self.romSetting.phpUrl, tt, function(status, response) logD("LogFileManager:responseUpload() status = ", status) logD("LogFileManager:responseUpload() response = ", response) end) end return LogFileManager