--日志开启规则
--Editor or 非Editor:
--1 默认日志关闭,GM面板打开或者关闭,走PlayerPref
--2 GM面板上面的日志,默认关闭,不走PlayerPref
--3 ResourceManager 下载失败SetLogError,编辑器下始终打开,移动端和LogEnable一致
local Debug = CS.UnityEngine.Debug
isDebugBuild = Debug.isDebugBuild
local logEnable = isDebugBuild
local logErrorEnable = true
local isLuaByteMode = CS.Main.IsEditorLuaByteMode()
local tracebacks = {}
function enableLog(b)
logEnable = b
KmlManager.LogEnable(b)
end
local function concat(...)
local args = spack(...)
for i = 1, args.n do
args[i] = tostring(args[i])
end
return table.concat(args, "\t")
end
local function logPrint(content,logType)
content = '(frameCount='..tostring(Time.frameCount)..') '..content
if logType==ELogType.Log or logType==ELogType.Message then
Debug.Log(content);
elseif logType==ELogType.Warning then
Debug.LogWarning(content);
elseif logType==ELogType.Error then
Debug.LogError(content);
end
EventManager.Dispatch(Event.Show_LogType,logType,content)
--Debug.Log()
end
function log(...)
if logEnable then
--Debug.Log(concat(...) .. "\n" ..redirectStaceback())
logPrint(concat(...) .. "\n" ..redirectStaceback(),ELogType.Log)
--redirectStaceback()
end
end
---@return void @不会被AnyAssetPostProcessor自动加上DebugFlag.LogEnable。比如连接服务器或者其他重要的日志,希望一直开着日志
function logNoFlag(...)
--Debug.Log(concat(...) .. "\n" ..redirectStaceback())
logPrint(concat(...) .. "\n" ..redirectStaceback(),ELogType.Log)
--redirectStaceback()
end
function logNoFlagNoEnable(...)
logPrint(concat(...) .. "\n" ..redirectStaceback(),ELogType.Log)
end
function logWarning(...)
if logEnable then
logPrint(concat(...) .. "\n" ..redirectStaceback(),ELogType.Warning)
--Debug.LogWarning(concat(...) .. "\n" .. redirectStaceback())
end
end
function logError(...)
if logErrorEnable then
Debug.LogError('(frameCount='..tostring(Time.frameCount)..') '.. concat(...) .. "\n" .. redirectStaceback())
--logPrint('(frameCount='..tostring(Time.frameCount)..') '.. concat(...) .. "\n" .. redirectStaceback(),ELogType.Error)
end
end
function logColorError(...)
if logEnable then
Debug.Log('logColorError(frameCount='..tostring(Time.frameCount)..') '.. concat(...) .. "\n" .. redirectStaceback())
--logPrint('(frameCount='..tostring(Time.frameCount)..') '.. concat(...) .. "\n" .. redirectStaceback(),ELogType.Error)
end
end
---@return void @为了同样的打印能够显示在一行
function logNoFrameCount(...)
if logEnable then
Debug.Log( concat(...) .. "\n" .. redirectStaceback())
--logPrint('(frameCount='..tostring(Time.frameCount)..') '.. concat(...) .. "\n" .. redirectStaceback(),ELogType.Error)
end
end
---@return void @为了同样的打印能够显示在一行
function logErrorNoFrameCount(...)
if logErrorEnable then
Debug.LogError( concat(...) .. "\n" .. redirectStaceback())
--logPrint('(frameCount='..tostring(Time.frameCount)..') '.. concat(...) .. "\n" .. redirectStaceback(),ELogType.Error)
end
end
function logOnEditor(...)
if Main and Main.isEditor then
if logEnable then
Debug.LogError('(仅编辑器)(frameCount='..tostring(Time.frameCount)..') '.. concat(...) .. "\n" .. redirectStaceback())
--logPrint('(frameCount='..tostring(Time.frameCount)..') '.. concat(...) .. "\n" .. redirectStaceback(),ELogType.Error)
end
end
end
function logErrorOnEditor(...)
if Main and Main.isEditor then
if logErrorEnable then
Debug.LogError('(仅编辑器)(frameCount='..tostring(Time.frameCount)..') '.. concat(...) .. "\n" .. redirectStaceback())
--logPrint('(frameCount='..tostring(Time.frameCount)..') '.. concat(...) .. "\n" .. redirectStaceback(),ELogType.Error)
end
end
end
--#ff8400
function logColor(...)
if logEnable then
local args = spack(...)
local color = table.remove(args,1)
args.n = args.n-1
for i = 1, args.n do
args[i] = tostring(args[i])
end
Debug.Log(string.format("%s%s",color,table.concat(args, "\t"),"\n" .. redirectStaceback()))
end
end
function logNetColor(...)
if logEnable then
local args = spack(...)
local color = table.remove(args,1)
local first = table.remove(args,1)
args.n = args.n-2
for i = 1, args.n do
args[i] = tostring(args[i])
end
--Debug.Log(string.format("%s%s%s",color,first,table.concat(args, "\t"),"\n" .. redirectStaceback()))
logPrint(string.format("%s%s%s",color,first,table.concat(args, "\t"),"\n" .. redirectStaceback()),ELogType.Message)
end
end
function logNetColorSend(...)
if logEnable then
local args = spack(...)
local color = table.remove(args,1)
local first = table.remove(args,1)
args.n = args.n-2
for i = 1, args.n do
args[i] = tostring(args[i])
end
--Debug.Log(string.format("%s%s%s",color,first,table.concat(args, "\t"),"\n" .. redirectStaceback()))
logPrint(string.format("%s%s",color,first,table.concat(args, "\t")),ELogType.Message)
end
end
function redirectStaceback()
if (Main and not Main.isEditor) or isLuaByteMode then
--debug.traceback消耗巨大,导致使用二进制或者移动端打印一个堆栈较深的日志都巨卡
--return debug.traceback()
table.clear(tracebacks)
tracebacks[#tracebacks+1] = 'stack traceback:\n'
local space = ' '
for level=3,math.huge do
---@type DebugInfo
local fInfo = debug.getinfo(level, 'nSl')
if not fInfo then
break
end
tracebacks[#tracebacks+1] = space
tracebacks[#tracebacks+1] = fInfo.short_src
tracebacks[#tracebacks+1] = '.lua:'
tracebacks[#tracebacks+1] = fInfo.currentline
if fInfo.name then
tracebacks[#tracebacks+1] = ':'
tracebacks[#tracebacks+1] = fInfo.name
end
tracebacks[#tracebacks+1] = '\n'
end
return table.concat(tracebacks)
else
--Assets/Scripts/MovablePlatform.cs:7
local link = "%s:%s"
local content = 'stack traceback:\n';
local space = ' '
for level=3,math.huge do
---@type DebugInfo
local fInfo = debug.getinfo(level, 'nSl')
if not fInfo then
break
end
content = content..space..fInfo.short_src..':'..fInfo.currentline
if fInfo.name then
local path = fInfo.short_src
if not CS.TCFramework.Platform.isMobile then
path = string.replace( path,"[string \"","")
path = string .replace(path ,"\"]","")
end
--local clickPath ='(in '..fInfo.namewhat..' \''..fInfo.name..'\') (at Assets/Lua/'..path..'.lua:'..fInfo.currentline..')'
local p0 = 'Assets/Lua/'..path..'.lua'
local p1 = fInfo.currentline
local clickPath ='(in '..fInfo.namewhat..' \''..fInfo.name..'\') (at '..string.format(link,p0,p1,p0,p1)..')'
content = content..clickPath..'\n'
else
local path = fInfo.short_src
if not CS.TCFramework.Platform.isMobile then
path = string.replace( path,"[string \"","")
path = string .replace(path ,"\"]","")
end
local p0 = 'Assets/Lua/'..path..'.lua'
local p1 = fInfo.currentline
local clickPath ='\') (at '..string.format(link,p0,p1,p0,p1)..')'
content = content..clickPath..'\n'
end
end
return content
end
end
local infoPatten = '([^:]+):'
function redirectInfo(content)
if not Main.isEditor or isLuaByteMode then
return content
end
local lines = string.split(content,'\n')
content = ''
local info = {}
for k, v in pairs(lines) do
local line = v..':'
local index = 1
for k2, v2 in string.gmatch(line,infoPatten) do
info[index] = k2
index = index+1
end
if index - 1 == 3 then
line = line..') (at Assets/Lua/'..string.trimLeft(info[1],'\t')..'.lua:'..info[2]..')'
end
content = content..line..'\n'
end
--CS.UnityEngine.Debug.LogError(content)
return content
end
---@return string @调试用的
function getTableName(table)
if not Main.isEditor then
return string.empty
end
if type(table) ~= 'table' then
logError('传入的参数不是table类型')
return
end
for k, v in pairs(_G) do
if k == 'Body' then
local s= ''
end
if IsSubTypeOfTryCatch(table,v) then
return k
end
end
end
---@return any @仅支持全局table或者基类是全局表的table
function logTableName(table)
log(getTableName(table))
end
function logErrorTableName(table)
logError(getTableName(table))
end
print = log