-
Notifications
You must be signed in to change notification settings - Fork 56
/
settings.lua
185 lines (148 loc) · 5.16 KB
/
settings.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
----------------------------------------
--
-- Copyright (c) 2015, 128 Technology, Inc.
--
-- author: Hadriel Kaplan <[email protected]>
--
-- This code is licensed under the MIT license.
--
-- Version: 1.0
--
------------------------------------------
-- prevent wireshark loading this file as a plugin
if not _G['protbuf_dissector'] then return end
local inspect = require "inspect"
local debug = require "debug"
local __DIR__ = protbuf_dissector.__DIR__
local __DIR_SEPARATOR__ = protbuf_dissector.__DIR_SEPARATOR__
--------------------------------------------------------------------------------
-- our Settings
local Settings = {
-- a table of directories to load - all proto files in these directories
-- will be loaded; NOTE: the directory names need to be a full absolute path
proto_dirs = {
__DIR__ .. __DIR_SEPARATOR__ .. "files",
-- __DIR__ .. __DIR_SEPARATOR__ .. "test",
},
-- a table of protobuf '.proto' files to load, if not in
-- the directories above
proto_files = {
-- example:
-- "foo.proto", "bar.proto"
},
-- debug levels
dlevel = {
DISABLED = 0,
LEVEL_1 = 1,
LEVEL_2 = 2
},
-- current debug level; default disabled
debug_level = 0,
-- debug printers for different debug levels, by default they
-- do nothing; but this will be updated later
dprint = function() end,
dprint2 = function() end,
-- to handle passing arguments to dprint/dassert/derror/etc., we need
-- something that would never naturally be in the variable list of
-- arguments; so we use these tables as those arguements, since a table
-- instance in Lua is a pointer and can be compared and will be unique
add_stacktrace = {},
}
----------------------------------------
local inspect_filter = inspect.makeFilter({ ".<metatable>", ".cursor" })
local function generateOutput(t)
local out = {}
for _, value in ipairs(t) do
local vt = type(value)
if vt == 'string' then
out[#out+1] = value
elseif vt == 'table' then
if value == Settings.add_stacktrace then
out[#out+1] = debug.traceback("", 3)
else
if type(value.getType) == 'function' then
vt = value:getType()
end
if vt == 'CURSOR' or vt == 'TOKEN' then
out[#out+1] = value:getDebugOutput()
else
out[#out+1] = "\n" .. inspect(value, { filter = inspect_filter })
end
end
else
out[#out+1] = tostring(value)
end
end
return table.concat(out, " ")
end
local function resetDebugLevel()
if Settings.debug_level > Settings.dlevel.DISABLED then
Settings.dprint = function(...)
info( generateOutput( { "Protobuf-Debug:", ... } ) )
end
if Settings.debug_level > Settings.dlevel.LEVEL_1 then
Settings.dprint2 = Settings.dprint
else
Settings.dprint2 = function() end
end
else
Settings.dprint = function() end
Settings.dprint2 = function() end
end
end
-- call it now
resetDebugLevel()
--------------------------------------------------------------------------------
-- the public functions of the module
function Settings:processCmdLine(args)
-- allow the command line to specify file names, debug level
for _, n in ipairs(args) do
if n:find("=") then
local level = n:match("debug%s*=%s*(%d)")
if not level then
error("Bad argument given to protobuf.lua: " .. n)
end
self.debug_level = tonumber(level)
else
self.proto_files[#self.proto_files + 1] = n
end
end
resetDebugLevel()
end
function Settings:getProtoFileNames()
local names = {}
local t = {}
-- get all proto files in the directories
for _, dir_name in ipairs(self.proto_dirs) do
assert(Dir.exists(dir_name), "Protobuf ERROR: could not find proto directory: " .. dir_name)
for filename in Dir.open(dir_name, ".proto") do
local fullname = dir_name .. __DIR_SEPARATOR__ .. filename
t[#t+1] = fullname
names[fullname] = true
end
end
-- and add all explicit files too
for _, filename in ipairs(self.proto_files) do
assert(file_exists(filename), "Protobuf ERROR: could not find proto file: " .. filename)
if not names[filename] then
t[#t+1] = filename
names[filename] = true
end
end
return t
end
function Settings:getDebugLevel()
return self.debug_level
end
-- like Lua's 'assert()', except it takes an arbitrary number of arguments
-- which it will concatenate into an error string if the first argument is
-- false; this way we avoid the performance penalty of generating strings
-- for non-false assertions
function Settings.dassert(check, ...)
if check then return check end
error( generateOutput({ "\nProtobuf ERROR:\n", ... }), 2 )
end
function Settings.derror(...)
error( generateOutput({ "\nProtobuf ERROR:\n", ... }), 2 )
end
return Settings