forked from openwrt/openwrt
-
Notifications
You must be signed in to change notification settings - Fork 21
/
log.lua
85 lines (73 loc) · 2.05 KB
/
log.lua
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
local M = {}
local tconcat = table.concat
local tinsert = table.insert
local srep = string.rep
local outputfile = "/tmp/luci.log"
local function local_print(str)
local dbg = io.open(outputfile, "a+")
local str = str or ""
if dbg then
dbg:write(str..'\n')
dbg:close()
end
end
function M.print(...)
local dbg = io.open(outputfile, "a+")
local last = false
if dbg then
dbg:write(os.date("[%H:%M:%S]: "))
for _, o in ipairs({...}) do
if last then dbg:write("\t") end
last = true
dbg:write(tostring(o))
end
dbg:write("\n")
dbg:close()
end
end
function M.print_r(data, depth)
local depth = depth or 3
local cstring = "";
local top_flag = true
local function table_len(t)
local i = 0
for k, v in pairs(t) do
i = i + 1
end
return i
end
local function tableprint(data,cstring, local_depth)
if data == nil then
local_print("core.print data is nil");
end
local cs = cstring .. "\t";
if top_flag then
local_print(cstring .."{");
top_flag = false
end
if(type(data)=="table") then
for k, v in pairs(data) do
if type(v) ~= "table" then
if type(v) == "string" then
local_print(cs..tostring(k).." = ".."'"..tostring(v).."'");
else
local_print(cs..tostring(k).." = "..tostring(v));
end
elseif table_len(v) == 0 then
local_print(cs..tostring(k).." = ".."{}")
elseif local_depth < depth then
local_print(cs..tostring(k).." = {");
tableprint(v,cs,local_depth+1);
else
local_print(cs..tostring(k).." = ".."{*}")
end
end
else
local_print(cs..tostring(data));
end
local_print(cstring .."}");
end
local_print(os.date("[%H:%M:%S]: "))
tableprint(data,cstring,0);
end
return M