Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Commit 8301017

Browse files
committed
Enable debug log under an environment variable
1 parent 9d22560 commit 8301017

File tree

3 files changed

+45
-27
lines changed

3 files changed

+45
-27
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,14 @@ TEST_RUN_TESTS=common/mutation make test
404404
* For building apidoc (additionally to 'for use'):
405405
* ldoc.
406406

407+
## Hacking
408+
409+
Enable debug log:
410+
411+
```sh
412+
export TARANTOOL_GRAPHQL_DEBUG=1
413+
```
414+
407415
## License
408416

409417
Consider LICENSE file for details. In brief:

graphql/bfs_executor.lua

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,6 @@ end
858858

859859
-- Debugging {{{
860860

861-
--[[
862861
local function prepared_object_digest(prepared_object)
863862
local json = require('json')
864863
local digest = {
@@ -883,29 +882,30 @@ local function prepared_object_digest(prepared_object)
883882
return json.encode(digest)
884883
end
885884

886-
local function print_open_set(open_set)
885+
local function open_set_tostring(open_set, name)
886+
local res = ('\n==== %s ====\n'):format(name)
887887
for _, item in ipairs(open_set) do
888888
if item.prepared_object ~= nil then
889889
local digest = prepared_object_digest(item.prepared_object)
890-
print('prepared_object: ' .. digest)
890+
res = res .. '\nprepared_object: ' .. digest
891891
elseif item.prepared_object_list ~= nil then
892-
print('prepared_object_list:')
892+
res = res .. '\nprepared_object_list:'
893893
for _, prepared_object in ipairs(item.prepared_object_list) do
894894
local digest = prepared_object_digest(prepared_object)
895-
print(' ' .. digest)
895+
res = res .. '\n ' .. digest
896896
end
897897
elseif item.squash_marker ~= nil then
898898
if item.fetch_id ~= nil then
899-
print('squash marker: ' .. tostring(item.fetch_id))
899+
res = res .. '\nsquash marker: ' .. tostring(item.fetch_id)
900900
else
901-
print('squash marker')
901+
res = res .. '\nsquash marker'
902902
end
903903
else
904-
print('unknown open_set item')
904+
res = res .. '\nunknown open_set item'
905905
end
906906
end
907+
return res
907908
end
908-
]]--
909909

910910
-- }}}
911911

@@ -980,14 +980,9 @@ function bfs_executor.execute(schema, query_ast, variables, operation_name, opts
980980
table.insert(open_set, 1, {squash_marker = true})
981981

982982
while true do
983-
--[[
984-
print('')
985-
print('==== cache only open set ====')
986-
print_open_set(cache_only_open_set)
987-
print('')
988-
print('==== open set ====')
989-
print_open_set(open_set)
990-
]]--
983+
utils.debug(open_set_tostring, cache_only_open_set,
984+
'cache only open set')
985+
utils.debug(open_set_tostring, open_set, 'open set')
991986

992987
local item
993988
local is_item_cache_only = next(cache_only_open_set) ~= nil
@@ -997,11 +992,7 @@ function bfs_executor.execute(schema, query_ast, variables, operation_name, opts
997992
item = table.remove(open_set, 1)
998993
end
999994

1000-
--[[
1001-
print('')
1002-
print('==== item (before) ====')
1003-
print_open_set({item})
1004-
]]--
995+
utils.debug(open_set_tostring, {item}, 'item (before)')
1005996

1006997
if item == nil then break end
1007998
if item.prepared_object ~= nil then
@@ -1035,11 +1026,7 @@ function bfs_executor.execute(schema, query_ast, variables, operation_name, opts
10351026
assert(false, 'unknown open_set item format')
10361027
end
10371028

1038-
--[[
1039-
print('')
1040-
print('==== item (after) ====')
1041-
print_open_set({item})
1042-
]]--
1029+
utils.debug(open_set_tostring, {item}, 'item (after)')
10431030
end
10441031

10451032
-- XXX: support several parallel graphql instances (use

graphql/utils.lua

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,4 +286,27 @@ function utils.expand_list(list, tail)
286286
end
287287
end
288288

289+
--- Add a debug print to the log enabled by an environment variable.
290+
---
291+
--- @param data_or_func (string or function) data to print or a function that
292+
--- will be return the data to print; the function called only if the
293+
--- environment variable toogle is enabled
294+
---
295+
--- @param ... parameters of the function `data_or_func`
296+
---
297+
--- @return nothing
298+
function utils.debug(data_or_func, ...)
299+
if (os.getenv('TARANTOOL_GRAPHQL_DEBUG') or ''):len() > 0 then
300+
local data
301+
if type(data_or_func) == 'function' then
302+
data = data_or_func(...)
303+
else
304+
data = data_or_func
305+
assert(select('#', ...) == 0)
306+
end
307+
assert(type(data) == 'string')
308+
log.info('DEBUG: %s', data)
309+
end
310+
end
311+
289312
return utils

0 commit comments

Comments
 (0)