Debug_Tips.lua 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. --日志开启规则
  2. --Editor or 非Editor:
  3. --1 默认日志关闭,GM面板打开或者关闭,走PlayerPref
  4. --2 GM面板上面的日志,默认关闭,不走PlayerPref
  5. --3 ResourceManager 下载失败SetLogError,编辑器下始终打开,移动端和LogEnable一致
  6. local Debug = CS.UnityEngine.Debug
  7. isDebugBuild = Debug.isDebugBuild
  8. local logEnable = isDebugBuild
  9. local logErrorEnable = true
  10. local isLuaByteMode = CS.Main.IsEditorLuaByteMode()
  11. local tracebacks = {}
  12. function enableLog(b)
  13. logEnable = b
  14. KmlManager.LogEnable(b)
  15. end
  16. local function concat(...)
  17. local args = spack(...)
  18. for i = 1, args.n do
  19. args[i] = tostring(args[i])
  20. end
  21. return table.concat(args, "\t")
  22. end
  23. local function logPrint(content,logType)
  24. content = '(frameCount='..tostring(Time.frameCount)..') '..content
  25. if logType==ELogType.Log or logType==ELogType.Message then
  26. Debug.Log(content);
  27. elseif logType==ELogType.Warning then
  28. Debug.LogWarning(content);
  29. elseif logType==ELogType.Error then
  30. Debug.LogError(content);
  31. end
  32. EventManager.Dispatch(Event.Show_LogType,logType,content)
  33. --Debug.Log()
  34. end
  35. function log(...)
  36. if logEnable then
  37. --Debug.Log(concat(...) .. "\n" ..redirectStaceback())
  38. logPrint(concat(...) .. "\n" ..redirectStaceback(),ELogType.Log)
  39. --redirectStaceback()
  40. end
  41. end
  42. ---@return void @不会被AnyAssetPostProcessor自动加上DebugFlag.LogEnable。比如连接服务器或者其他重要的日志,希望一直开着日志
  43. function logNoFlag(...)
  44. --Debug.Log(concat(...) .. "\n" ..redirectStaceback())
  45. logPrint(concat(...) .. "\n" ..redirectStaceback(),ELogType.Log)
  46. --redirectStaceback()
  47. end
  48. function logNoFlagNoEnable(...)
  49. logPrint(concat(...) .. "\n" ..redirectStaceback(),ELogType.Log)
  50. end
  51. function logWarning(...)
  52. if logEnable then
  53. logPrint(concat(...) .. "\n" ..redirectStaceback(),ELogType.Warning)
  54. --Debug.LogWarning(concat(...) .. "\n" .. redirectStaceback())
  55. end
  56. end
  57. function logError(...)
  58. if logErrorEnable then
  59. Debug.LogError('(frameCount='..tostring(Time.frameCount)..') '.. concat(...) .. "\n" .. redirectStaceback())
  60. --logPrint('(frameCount='..tostring(Time.frameCount)..') '.. concat(...) .. "\n" .. redirectStaceback(),ELogType.Error)
  61. end
  62. end
  63. function logColorError(...)
  64. if logEnable then
  65. Debug.Log('<color=red>logColorError(frameCount='..tostring(Time.frameCount)..') '.. concat(...) .. "</color>\n" .. redirectStaceback())
  66. --logPrint('(frameCount='..tostring(Time.frameCount)..') '.. concat(...) .. "\n" .. redirectStaceback(),ELogType.Error)
  67. end
  68. end
  69. ---@return void @为了同样的打印能够显示在一行
  70. function logNoFrameCount(...)
  71. if logEnable then
  72. Debug.Log( concat(...) .. "\n" .. redirectStaceback())
  73. --logPrint('(frameCount='..tostring(Time.frameCount)..') '.. concat(...) .. "\n" .. redirectStaceback(),ELogType.Error)
  74. end
  75. end
  76. ---@return void @为了同样的打印能够显示在一行
  77. function logErrorNoFrameCount(...)
  78. if logErrorEnable then
  79. Debug.LogError( concat(...) .. "\n" .. redirectStaceback())
  80. --logPrint('(frameCount='..tostring(Time.frameCount)..') '.. concat(...) .. "\n" .. redirectStaceback(),ELogType.Error)
  81. end
  82. end
  83. function logOnEditor(...)
  84. if Main and Main.isEditor then
  85. if logEnable then
  86. Debug.LogError('(仅编辑器)(frameCount='..tostring(Time.frameCount)..') '.. concat(...) .. "\n" .. redirectStaceback())
  87. --logPrint('(frameCount='..tostring(Time.frameCount)..') '.. concat(...) .. "\n" .. redirectStaceback(),ELogType.Error)
  88. end
  89. end
  90. end
  91. function logErrorOnEditor(...)
  92. if Main and Main.isEditor then
  93. if logErrorEnable then
  94. Debug.LogError('(仅编辑器)(frameCount='..tostring(Time.frameCount)..') '.. concat(...) .. "\n" .. redirectStaceback())
  95. --logPrint('(frameCount='..tostring(Time.frameCount)..') '.. concat(...) .. "\n" .. redirectStaceback(),ELogType.Error)
  96. end
  97. end
  98. end
  99. --#ff8400
  100. function logColor(...)
  101. if logEnable then
  102. local args = spack(...)
  103. local color = table.remove(args,1)
  104. args.n = args.n-1
  105. for i = 1, args.n do
  106. args[i] = tostring(args[i])
  107. end
  108. Debug.Log(string.format("<color=%s>%s</color>%s",color,table.concat(args, "\t"),"\n" .. redirectStaceback()))
  109. end
  110. end
  111. function logNetColor(...)
  112. if logEnable then
  113. local args = spack(...)
  114. local color = table.remove(args,1)
  115. local first = table.remove(args,1)
  116. args.n = args.n-2
  117. for i = 1, args.n do
  118. args[i] = tostring(args[i])
  119. end
  120. --Debug.Log(string.format("<color=%s>%s</color>%s%s",color,first,table.concat(args, "\t"),"\n" .. redirectStaceback()))
  121. logPrint(string.format("<color=%s>%s</color>%s%s",color,first,table.concat(args, "\t"),"\n" .. redirectStaceback()),ELogType.Message)
  122. end
  123. end
  124. function logNetColorSend(...)
  125. if logEnable then
  126. local args = spack(...)
  127. local color = table.remove(args,1)
  128. local first = table.remove(args,1)
  129. args.n = args.n-2
  130. for i = 1, args.n do
  131. args[i] = tostring(args[i])
  132. end
  133. --Debug.Log(string.format("<color=%s>%s</color>%s%s",color,first,table.concat(args, "\t"),"\n" .. redirectStaceback()))
  134. logPrint(string.format("<color=%s>%s</color>%s",color,first,table.concat(args, "\t")),ELogType.Message)
  135. end
  136. end
  137. function redirectStaceback()
  138. if (Main and not Main.isEditor) or isLuaByteMode then
  139. --debug.traceback消耗巨大,导致使用二进制或者移动端打印一个堆栈较深的日志都巨卡
  140. --return debug.traceback()
  141. table.clear(tracebacks)
  142. tracebacks[#tracebacks+1] = 'stack traceback:\n'
  143. local space = ' '
  144. for level=3,math.huge do
  145. ---@type DebugInfo
  146. local fInfo = debug.getinfo(level, 'nSl')
  147. if not fInfo then
  148. break
  149. end
  150. tracebacks[#tracebacks+1] = space
  151. tracebacks[#tracebacks+1] = fInfo.short_src
  152. tracebacks[#tracebacks+1] = '.lua:'
  153. tracebacks[#tracebacks+1] = fInfo.currentline
  154. if fInfo.name then
  155. tracebacks[#tracebacks+1] = ':'
  156. tracebacks[#tracebacks+1] = fInfo.name
  157. end
  158. tracebacks[#tracebacks+1] = '\n'
  159. end
  160. return table.concat(tracebacks)
  161. else
  162. --<a href=\"Assets/Scripts/MovablePlatform.cs\" line=\"7\">Assets/Scripts/MovablePlatform.cs:7</a>
  163. local link = "<a href=\"%s\" line=\"%s\">%s:%s</a>"
  164. local content = 'stack traceback:\n';
  165. local space = ' '
  166. for level=3,math.huge do
  167. ---@type DebugInfo
  168. local fInfo = debug.getinfo(level, 'nSl')
  169. if not fInfo then
  170. break
  171. end
  172. content = content..space..fInfo.short_src..':'..fInfo.currentline
  173. if fInfo.name then
  174. local path = fInfo.short_src
  175. if not CS.TCFramework.Platform.isMobile then
  176. path = string.replace( path,"[string \"","")
  177. path = string .replace(path ,"\"]","")
  178. end
  179. --local clickPath ='(in '..fInfo.namewhat..' \''..fInfo.name..'\') (at Assets/Lua/'..path..'.lua:'..fInfo.currentline..')'
  180. local p0 = 'Assets/Lua/'..path..'.lua'
  181. local p1 = fInfo.currentline
  182. local clickPath ='(in '..fInfo.namewhat..' \''..fInfo.name..'\') (at '..string.format(link,p0,p1,p0,p1)..')'
  183. content = content..clickPath..'\n'
  184. else
  185. local path = fInfo.short_src
  186. if not CS.TCFramework.Platform.isMobile then
  187. path = string.replace( path,"[string \"","")
  188. path = string .replace(path ,"\"]","")
  189. end
  190. local p0 = 'Assets/Lua/'..path..'.lua'
  191. local p1 = fInfo.currentline
  192. local clickPath ='\') (at '..string.format(link,p0,p1,p0,p1)..')'
  193. content = content..clickPath..'\n'
  194. end
  195. end
  196. return content
  197. end
  198. end
  199. local infoPatten = '([^:]+):'
  200. function redirectInfo(content)
  201. if not Main.isEditor or isLuaByteMode then
  202. return content
  203. end
  204. local lines = string.split(content,'\n')
  205. content = ''
  206. local info = {}
  207. for k, v in pairs(lines) do
  208. local line = v..':'
  209. local index = 1
  210. for k2, v2 in string.gmatch(line,infoPatten) do
  211. info[index] = k2
  212. index = index+1
  213. end
  214. if index - 1 == 3 then
  215. line = line..') (at Assets/Lua/'..string.trimLeft(info[1],'\t')..'.lua:'..info[2]..')'
  216. end
  217. content = content..line..'\n'
  218. end
  219. --CS.UnityEngine.Debug.LogError(content)
  220. return content
  221. end
  222. ---@return string @调试用的
  223. function getTableName(table)
  224. if not Main.isEditor then
  225. return string.empty
  226. end
  227. if type(table) ~= 'table' then
  228. logError('传入的参数不是table类型')
  229. return
  230. end
  231. for k, v in pairs(_G) do
  232. if k == 'Body' then
  233. local s= ''
  234. end
  235. if IsSubTypeOfTryCatch(table,v) then
  236. return k
  237. end
  238. end
  239. end
  240. ---@return any @仅支持全局table或者基类是全局表的table
  241. function logTableName(table)
  242. log(getTableName(table))
  243. end
  244. function logErrorTableName(table)
  245. logError(getTableName(table))
  246. end
  247. print = log