Skip to content

Commit d9b742f

Browse files
authored
feat: avoid truncating debug output under OpenResty (#37)
1 parent d72146a commit d9b742f

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

lib/jsonschema.lua

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,17 +224,49 @@ function codectx_mt:as_string()
224224
return tab_concat(self._code_table)
225225
end
226226

227+
local function split(s, sep)
228+
local res = {}
229+
230+
if #s > 0 then
231+
local n, start = 1, 1
232+
local first, last = s:find(sep, start, true)
233+
while first do
234+
res[n] = s:sub(start, first - 1)
235+
n = n + 1
236+
start = last + 1
237+
first,last = s:find(sep, start, true)
238+
end
239+
240+
res[n] = s:sub(start)
241+
end
242+
243+
return res
244+
end
245+
227246
function codectx_mt:as_func(name, ...)
228247
self:_get_loader()
229248
local loader, err = loadstring(tab_concat(self._code_table, ""), 'jsonschema:' .. (name or 'anonymous'))
230249
if DEBUG then
231-
local line=1
232250
print('------------------------------')
233251
print('generated code:')
234-
print('0001: ' .. self:as_string():gsub('\n', function()
235-
line = line + 1
236-
return sformat('\n%04d: ', line)
237-
end))
252+
-- OpenResty limits its log size under 4096 (including the prefix/suffix),
253+
-- so we use a lower limit here.
254+
-- This should not make any difference for non-OpenResty users.
255+
local max_len = 3900
256+
local current_len = 0
257+
local buf = {}
258+
local lines = split(self:as_string(), "\n")
259+
for i, line in ipairs(lines) do
260+
local s = sformat('\n%04d: %s', i, line)
261+
if #s + current_len > max_len then
262+
print(table.concat(buf))
263+
buf = {}
264+
current_len = 0
265+
end
266+
table.insert(buf, s)
267+
current_len = current_len + #s
268+
end
269+
print(table.concat(buf))
238270
print('------------------------------')
239271
end
240272

0 commit comments

Comments
 (0)