lua 写的游戏log逻辑(基本支持lua所有格式)

module(...,package.seeall)


gGameLog = nil


function getInstance( ... )
-- body
if gGameLog == nil then
gGameLog = create() 
end
return gGameLog
end




function create()
-- body
local self = {logFile="gameLog.log",bDebug=false,bWriteFile = false}
self.fileHandle = nil
setmetatable(self,{__index=getfenv()})
self:init()
return self
end


function init(self)
-- body
if self.bWriteFile then
self.fileHandle = io.open(self.logFile,"w")
self.fileHandle:write(os.date().."\n")
self.fileHandle:flush()
self.fileHandle:close()
end
end


function log(self,...)
-- body
if not self.bDebug and not self.bWriteFile then return end
local function toAddTStr(rSrc,index,flagStr)
for i=1,index do
rSrc = rSrc..flagStr
end
return rSrc
end 


local function parseTable(tempStr,srcTable,index)
tempStr = tempStr .. "{\n"
for _key,_v in pairs(srcTable) do
tempStr = toAddTStr(tempStr,index+1,"\t")
tempStr = tempStr.._key.." = "
if type(_v) == "table" then
tempStr = parseTable(tempStr,_v,index+1)
elseif type(_v) == "boolean" then
if _v == true then
tempStr = tempStr.."true\n"
else
tempStr = tempStr.."false\n"
end
else
tempStr = tempStr.._v.."\n"
end
end
tempStr = toAddTStr(tempStr,index,"\t")
tempStr = tempStr .. "} \n"
return tempStr
end


local str = ""
for i = 1 , select('#',...) do
if type(select(i, ...)) == "string" or type(select(i, ...)) == "number" then
str = str .. select(i, ...) .. " "
elseif type(select(i, ...)) == "nil" then
str = str .. " nil "
elseif type(select(i,...)) == "table" then
str = str .. "\n"
str = parseTable(str,select(i,...),0)
elseif type(select(i,...)) == "boolean" then
if select(i,...) == true then
str = str.." true "
else
str = str.." false "
end
end
end
str = str .. "\n"
local dis = 500
local start = 0
if string.len(str) > 1024 * 10 then
local tempStr = str
while 1 do
if tempStr == nil then break end
local startPos,endPos = string.find(tempStr,"\n")
if startPos == nil then break end
print(string.sub(tempStr,0,endPos))
tempStr = string.sub(tempStr,endPos+1)
end
else
print(str)
end
if not self.bWriteFile then return end
self.fileHandle = io.open(self.logFile,"a+")
self.fileHandle:write(str)
self.fileHandle:flush()
self.fileHandle:close()
end


function clear(self)
-- body
if not self.bDebug then return end 
-- self.fileHandle:close()
end


-- 只在控制台输出
function printLog(... )
-- body
if not gGameLog.bDebug then return end
print(...)
end


gGameLog = create()
_G["gGameLog"] = gGameLog



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值