改写print

print fixed
简单实现改写print,将print内容写入文件生成日志。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
-------------------------------------------------------------------------------
-- Print 改写. @简律纯 @gexi
-------------------------------------------------------------------------------
writeToFile = function ( str )
local filename = "print.log"
if not fileLogOut then
fileLogOut = io.open(filename, "w")
else
fileLogOut = io.open(filename, "a")
end
fileLogOut:write(os.date("%H:%M:%S",os.time()).." "..str.."\n")
fileLogOut:close()
end

function babe_tostring(...)
local num = select("#", ...);
local args = { ... };
local outs = {};
for i = 1, num do
if i > 1 then
outs[#outs + 1] = "\t";
end
outs[#outs + 1] = tostring(args[i]);
end
return table.concat(outs);
end

local just_print = print;
local babe_output = function(...)
just_print(...);

local str = babe_tostring(...);
if writeToFile then writeToFile(str) end
end
print = babe_output