在实际开发过程中,代码日志是必不可少的系统,尤其是在线上版本中,对于查找问题相当有帮助。但是cocos2dx lua的代码日志是基于lua的print打印,只写在console中,不会长久保存。怎么办,那么我们来设计一个简单的日志系统,让日志保存下来。
话不多说,直接上代码:(添加了注释)
local LogManager = class('LogManager')
local socketOk = nil
local socket = nil
local logfilepath = nil
function LogManager:ctor()
-- 存储日志的文件地址
logfilepath = cc.FileUtils:getInstance():getWritablePath() .. 'log.txt'
-- 检查是否使用了socket库(该库主要用于获取精确时间)
socketOk, socket = pcall(function()
return require("socket")
end)
end
function LogManager.writefile(...)
local time = nil
if socketOk then
time = socket.gettime() -- 取更加准确的时间(socket时间精确到毫秒)
else
time = os.time() -- lua提供的时间精确到秒
end
-- 标记日志打印的时间
local timestr = string.format("[%s %s]", os.date("%Y-%m-%d %H:%M:%S", time), math.floor(time*1000)%1000)
-- 写
io.writefile(logfilepath, timestr .. tostring(...) .. "\n", "a")
end
function LogManager.log( ... )
local temp = ...
if not temp then
LogManager.log('nil')
return
end
print(tostring(...))
LogManager.writefile(temp)
end
-- 全局记录单文本方法
cc.exports.blog = function( tag, ... )
if tag and ... then
LogManager.log('[' .. tag .. ']' .. string.format(...))
else
LogManager.log(tag)
end
end
-- 全局记录表结构数据日志方法(需要用到cjson库)
cc.exports.bdump = function( tag, data )
local strData = cjson.encode(data)
log(tag, strData)
end
cc.exports.LogManager = cc.exports.LogManager or LogManager:create()
return cc.exports.LogManager
未做的事情:
1、如果是在android手机上,可以在Android底层提供一个接口,将写日志的文件转移到手机可查看的目录中,这样就方便调取手机日志,如:(IOS没有这个真香功能。)
public static String getSdCardPath() {
String sdDir = "";
boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
if (sdCardExist) {
sdDir = Environment.getExternalStorageDirectory().getPath();
File file = new File(sdDir + "/123");
if (!file.exists()) {
file.mkdir();
}
return file.getAbsolutePath();
}
return "";
}
2、日志文件大小控制,每次在启动APP时,查询下当前日志文件的大小:(function.lua提供),如果大于设定的值就清除日志
function io.filesize(path)
local size = false
local file = io.open(path, "r")
if file then
local current = file:seek()
size = file:seek("end")
file:seek("set", current)
io.close(file)
end
return size
end
3、动态上传日志:
架设一组接收日志文件的服务器,在游戏内提供一个反馈接口,在有需要的时候,让玩家将日志文件发送上来。
function LogManager.commitLog(logname)
local logurl = "http://www.baidu.com/xxx"
local xhr = cc.XMLHttpRequest:new()
xhr.responseType = cc.XMLHTTPREQUEST_RESPONSE_JSON --设置返回数据格式为字符串
xhr:open("POST", logurl) --设置请求方式 GET 或者 POST
xhr:setRequestHeader("Content-Type","application/octet-stream")
xhr:setRequestHeader("Accept-Encoding"," ")
local function onReadyStateChange() --请求响应函数
if xhr.readyState == 4 and (xhr.status >= 200 and xhr.status < 207) then --请求状态已完并且请求已成功
blog("日志上传成功。")
else
blog("日志上传失败。")
end
xhr:unregisterScriptHandler()
end
xhr:registerScriptHandler(onReadyStateChange) --注册请求响应函数
local filesize = io.filesize(logFilePath)
local file = io.open(logFilePath, "rb")
local logFileData = file:read("*a")
io.close(file)
xhr:setRequestHeader("Content-Length",tostring(filesize))
xhr:setRequestHeader("ContentLength",tostring(filesize))
xhr:send(logFileData) --最后发送请求
end
4、日志分级:要给日志系统设定一些等级,根据不同的等级选择保留哪些日志,在线上版本中应当尽量减少日志的读写,IO操作还是非常耗时间的,特别是对于table的处理。当然本份代码中没有体现这一功能。需要各位自己去摸索实现。
以上这些代码还需要整合起来,就形成了一个功能比较简单的日志系统了,在实际使用过程中,对开发和后期维护的作用还是非常巨大的。希望对大家的学习和使用帮助。