Skip to content

Commit

Permalink
Add nether.debug() (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
Treer authored Jul 26, 2020
1 parent 5b3b56e commit e326a94
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 52 deletions.
49 changes: 49 additions & 0 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
]]--

-- Set DEBUG_FLAGS to determine the behavior of nether.debug():
-- 0 = off
-- 1 = print(...)
-- 2 = minetest.chat_send_all(...)
-- 4 = minetest.log("info", ...)
local DEBUG_FLAGS = 0

local S
if minetest.get_translator ~= nil then
S = minetest.get_translator("nether")
Expand Down Expand Up @@ -63,6 +70,48 @@ if nether.DEPTH_FLOOR + 1000 > nether.DEPTH_CEILING then
end
nether.DEPTH = nether.DEPTH_CEILING -- Deprecated, use nether.DEPTH_CEILING instead.


-- A debug-print function that understands vectors etc. and does not
-- evaluate when debugging is turned off.
-- Works like string.format(), treating the message as a format string.
-- nils, tables, and vectors passed as arguments to nether.debug() are
-- converted to strings and can be included inside the message with %s
function nether.debug(message, ...)

local args = {...}
local argCount = select("#", ...)

for i = 1, argCount do
local arg = args[i]
if arg == nil then
-- convert nils to strings
args[i] = '<nil>'
elseif type(arg) == "table" then
local tableCount = 0
for _,_ in pairs(arg) do tableCount = tableCount + 1 end
if tableCount == 3 and arg.x ~= nil and arg.y ~= nil and arg.z ~= nil then
-- convert vectors to strings
args[i] = minetest.pos_to_string(arg)
else
-- convert tables to strings
-- (calling function can use dump() if a multi-line listing is desired)
args[i] = string.gsub(dump(arg, ""), "\n", " ")
end
end
end

local composed_message = string.format(message, unpack(args))

if math.floor(DEBUG_FLAGS / 1) % 2 == 1 then print(composed_message) end
if math.floor(DEBUG_FLAGS / 2) % 2 == 1 then minetest.chat_send_all(composed_message) end
if math.floor(DEBUG_FLAGS / 4) % 2 == 1 then minetest.log("info", composed_message) end
end
if DEBUG_FLAGS == 0 then
-- do as little evaluation as possible
nether.debug = function() end
end


-- Load files
dofile(nether.path .. "/portal_api.lua")
dofile(nether.path .. "/nodes.lua")
Expand Down
Loading

0 comments on commit e326a94

Please sign in to comment.