关闭

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

170人阅读 评论(1) 收藏 举报
分类:
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
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:7946次
    • 积分:183
    • 等级:
    • 排名:千里之外
    • 原创:11篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1条
    文章分类