You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

293 lines
8.7 KiB

  1. local LogFileManager = {}
  2. LogFileManager.key = "LogFiles";
  3. LogFileManager.logFiles = {};
  4. LogFileManager.keyNew = "LogFilesNew";
  5. LogFileManager.logFilesNew = {};
  6. LogFileManager.romSetting = {}
  7. LogFileManager.maxFileNum = 200;
  8. LogFileManager.listWillUpload = {}
  9. LogFileManager.maxUploadingNum = 3; -- 允许同时上传的最大数量
  10. LogFileManager.curUploadingNum = 0; -- 正在上传的文件数量
  11. --[[
  12. self.logFiles =
  13. {
  14. ["20190226"] = {[1] = "Game-20190226-001.log", [2] = "Game-20190226-002.log" = true,...},
  15. ["20190227"] = {[1] = "Game-20190227-001.log", [2] = "Game-20190227-002.log" = true,...},
  16. ["20190228"] = {[1] = "Game-20190228-001.log", [2] = "Game-20190228-002.log" = true,...},
  17. }
  18. self.logFileNew =
  19. {
  20. ["20190529101103"] = {path="D:/WorkSpace/DingDing/Core/debug/src/storage/Game-20190529-101103.log",day="20190529",idx="101103"},
  21. ["20190529101104"] = {path="D:/WorkSpace/DingDing/Core/debug/src/storage/Game-20190529-101104.log",day="20190529",idx="101104"},
  22. ["20190529101105"] = {path="D:/WorkSpace/DingDing/Core/debug/src/storage/Game-20190529-101105.log",day="20190529",idx="101105"},
  23. ["20190529101106"] = {path="D:/WorkSpace/DingDing/Core/debug/src/storage/Game-20190529-101106.log",day="20190529",idx="101106"},
  24. ["20190529101107"] = {path="D:/WorkSpace/DingDing/Core/debug/src/storage/Game-20190529-101107.log",day="20190529",idx="101107"},
  25. ["20190530101101"] = {path="D:/WorkSpace/DingDing/Core/debug/src/storage/Game-20190530-101101.log",day="20190530",idx="101101"},
  26. }
  27. --]]
  28. function LogFileManager:init(romSetting)
  29. logD("LogFileManager:init()");
  30. self.romSetting = romSetting;
  31. self:loadDatas();
  32. self:addCurrentLogFile();
  33. self:clearOldLogFiles();
  34. self:printDatas();
  35. end
  36. function LogFileManager:printDatas()
  37. logD("LogFileManager:printDatas()");
  38. logD("--------------begin---------------")
  39. if self.logFilesNew then
  40. for k,v in pairsByKeys(self.logFilesNew) do
  41. logD(tostring(v.path))
  42. end
  43. end
  44. logD("--------------end---------------")
  45. end
  46. function LogFileManager:loadDatas()
  47. local logFiles = cc.UserDefault:getInstance():getStringForKey(self.key, "");
  48. local logFilesNew = cc.UserDefault:getInstance():getStringForKey(self.keyNew, "");
  49. self.logFiles = json.decode(logFiles) or {};
  50. self.logFilesNew = json.decode(logFilesNew) or {};
  51. if not self.logFilesNew or table_nums(self.logFilesNew) == 0 then
  52. self.logFilesNew = {}
  53. if self.logFiles then
  54. for day,v in pairs(self.logFiles) do
  55. for idx,vv in pairs(v) do
  56. local key = tostring(day)..tostring(idx)
  57. local info = {
  58. path = vv,
  59. day = day,
  60. idx = idx,
  61. }
  62. self.logFilesNew[key] = info;
  63. end
  64. end
  65. end
  66. end
  67. end
  68. function LogFileManager:saveDatas()
  69. local logFilesNew = json.encode(self.logFilesNew)
  70. cc.UserDefault:getInstance():setStringForKey(self.key, "")
  71. cc.UserDefault:getInstance():setStringForKey(self.keyNew, logFilesNew)
  72. cc.UserDefault:getInstance():flush()
  73. end
  74. function LogFileManager:parseLogFileName(fileName)
  75. logD("LogFileManager:parseLogFileName() fileName = ", fileName);
  76. local arr = split(fileName, "%p");
  77. local n = #arr;
  78. -- 旧版日志
  79. if arr[n-1] == "game" then
  80. logD("LogFileManager:parseLogFileName() this log file is old format");
  81. return nil, nil;
  82. end
  83. -- 新版日志
  84. local day = arr[n-2]
  85. local idx = arr[n-1]
  86. return day,idx
  87. end
  88. -- 添加一个日志文件
  89. -- fileName : 日志文件名字,不带路径
  90. -- day : 日志文件对应的日志,格式为"20190101"
  91. function LogFileManager:addCurrentLogFile()
  92. logD("LogFileManager:addCurrentLogFile()");
  93. local logFileName = cc.Logger:getLogFile();
  94. logD("LogFileManager:addCurrentLogFile() ", logFileName);
  95. local day, idx = self:parseLogFileName(logFileName)
  96. if not day or not idx then
  97. logD("LogFileManager:addCurrentLogFile() this log file is old format");
  98. return
  99. end
  100. local key = tostring(day)..tostring(idx)
  101. local info = {
  102. path = logFileName,
  103. day = day,
  104. idx = idx,
  105. }
  106. self.logFilesNew[key] = info;
  107. self:saveDatas();
  108. end
  109. -- 清理太旧的日志文件
  110. -- 只保留最近的10个
  111. function LogFileManager:clearOldLogFiles()
  112. local tt = {}
  113. for k,v in pairs(self.logFilesNew) do
  114. table.insert(tt, v)
  115. end
  116. table.sort(tt, function(a,b )
  117. if a.day ~= b.day then
  118. return tonumber(a.day) > tonumber(b.day)
  119. else
  120. return tonumber(a.idx) > tonumber(b.idx)
  121. end
  122. end)
  123. if #tt > self.maxFileNum then
  124. for i=self.maxFileNum+1,#tt do
  125. local info = tt[i]
  126. self:deleteFileByKey(info.day, info.idx);
  127. end
  128. end
  129. end
  130. function LogFileManager:deleteFileByKey(day, idx)
  131. logD("LogFileManager:deleteFileByKey()", day, idx);
  132. local key = tostring(day)..tostring(idx)
  133. if not self.logFilesNew[key] then
  134. return
  135. end
  136. local filePath = self.logFilesNew[key].path
  137. if cc.FileSystem:fileExists(filePath) then
  138. os.remove(filePath);
  139. end
  140. self.logFilesNew[key] = nil;
  141. self:saveDatas();
  142. end
  143. -- 删除某一天的所有日志
  144. function LogFileManager:deleteFileByDay(day)
  145. logD("LogFileManager:deleteFileByDay()", day);
  146. for k,v in pairs(self.logFilesNew) do
  147. if v.day == day then
  148. self:deleteFileByKey(v.day, v.idx)
  149. end
  150. end
  151. end
  152. function LogFileManager:doUplaod()
  153. if self.isUploading then
  154. return
  155. end
  156. if #self.listWillUpload <= 0 then
  157. return
  158. end
  159. logD("LogFileManager:doUplaod() curUploadingNum()", self.curUploadingNum);
  160. logD("LogFileManager:doUplaod() maxUploadingNum()", self.maxUploadingNum);
  161. if self.curUploadingNum >= self.maxUploadingNum then
  162. return
  163. end
  164. local info = table.remove(self.listWillUpload, 1);
  165. local actorId = loadUserId() or 0
  166. local fileKey = tostring(info.day)..tostring(info.idx)
  167. local clientFileName = info.path
  168. if not cc.FileSystem:fileExists(clientFileName) then
  169. logD("LogFileManager:doUplaod() file not exist!", clientFileName);
  170. return;
  171. end
  172. local remoteFileName = tostring(cc.Application:getInstance():getTargetPlatform())
  173. .. "_" .. "version"
  174. .. "_" .. tostring(actorId)
  175. .. "_" .. os.date("%Y%m%d%H%M%S", os.time())
  176. .. "_" .. tostring(fileKey)
  177. .. "_" .. string.format("%08X.7z" , math.random(1 , 2147483647));
  178. local function onUploadEnd(ret1, ret2)
  179. logD("LogFileManager:doUplaod() onUploadEnd()", ret1, ret2, clientFileName, remoteFileName);
  180. self.curUploadingNum = tonumber(self.curUploadingNum) - 1
  181. self:doUplaod();
  182. end
  183. converUrlToIp(self.romSetting.LogFTPIP, function(urlNew, header)
  184. self.curUploadingNum = tonumber(self.curUploadingNum) + 1
  185. cc.CURLManager:getInstance():uploadLogWithFileName(urlNew,self.romSetting.LogFTPUserName, self.romSetting.LogFTPPassword, clientFileName, remoteFileName ,onUploadEnd);
  186. end)
  187. end
  188. -- 上传文件,一组接一组上传。有效避免上传失败的问题
  189. function LogFileManager:uploadFileOneByOne(info)
  190. logD("LogFileManager:uploadFileOneByOne()", info.path);
  191. table.insert(self.listWillUpload, info)
  192. self:doUplaod();
  193. end
  194. -- 上传文件 - 极大概率会有很多文件上传失败
  195. -- info = {path="D:/WorkSpace/DingDing/Core/debug/src/storage/Game-20190529-101103.log",day="20190529",idx="101103"}
  196. function LogFileManager:uploadFile(info)
  197. logD("LogFileManager:uploadFile()", info.path);
  198. local actorId = loadUserId() or 0
  199. local fileKey = tostring(info.day)..tostring(info.idx)
  200. local clientFileName = info.path
  201. if not cc.FileSystem:fileExists(clientFileName) then
  202. logD("LogFileManager:uploadFile() file not exist!", clientFileName);
  203. return;
  204. end
  205. local remoteFileName = tostring(cc.Application:getInstance():getTargetPlatform())
  206. .. "_" .. "version"
  207. .. "_" .. tostring(actorId)
  208. .. "_" .. os.date("%Y%m%d%H%M%S", os.time())
  209. .. "_" .. tostring(fileKey)
  210. .. "_" .. string.format("%08X.7z" , math.random(1 , 2147483647));
  211. local function onUploadEnd(ret1, ret2)
  212. logD("LogFileManager:uploadFile() onUploadEnd()", ret1, ret2, clientFileName, remoteFileName);
  213. end
  214. converUrlToIp(self.romSetting.LogFTPIP, function(urlNew, header)
  215. cc.CURLManager:getInstance():uploadLogWithFileName(urlNew,self.romSetting.LogFTPUserName, self.romSetting.LogFTPPassword, clientFileName, remoteFileName ,onUploadEnd);
  216. end)
  217. end
  218. -- 上传某一天的所有日志
  219. function LogFileManager:uploadFilesByDay(day)
  220. logD("LogFileManager:uploadFilesByDay()", day);
  221. local status = 2
  222. for k,info in pairs(self.logFilesNew) do
  223. if info.day == day then
  224. status = 1
  225. self:uploadFileOneByOne(info)
  226. end
  227. end
  228. self:responseUpload(day,status)
  229. end
  230. function LogFileManager:responseUpload(day, status)
  231. logD("LogFileManager:responseUpload()", day, status);
  232. local tt =
  233. {
  234. action = "system.logconfirm",
  235. uid = loadUserId(),
  236. appid = getAppId(),
  237. date = day,
  238. status = status,
  239. }
  240. for k,v in pairs(tt) do
  241. logD( k ..", "..v)
  242. end
  243. httpPost(self.romSetting.phpUrl, tt, function(status, response)
  244. logD("LogFileManager:responseUpload() status = ", status)
  245. logD("LogFileManager:responseUpload() response = ", response)
  246. end)
  247. end
  248. return LogFileManager