2010-10-22 14:49:12 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- @author Uli Schlachter
|
|
|
|
-- @copyright 2010 Uli Schlachter
|
|
|
|
-- @release @AWESOME_VERSION@
|
2014-05-19 15:15:39 +02:00
|
|
|
-- @module gears.debug
|
2010-10-22 14:49:12 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local error = error
|
|
|
|
local tostring = tostring
|
|
|
|
local traceback = debug.traceback
|
2012-03-16 21:03:23 +01:00
|
|
|
local print = print
|
|
|
|
local type = type
|
|
|
|
local pairs = pairs
|
2010-10-22 14:49:12 +02:00
|
|
|
|
2012-06-12 10:13:46 +02:00
|
|
|
local debug = {}
|
2010-10-22 14:49:12 +02:00
|
|
|
|
|
|
|
--- Check that the given condition holds true, else throw an error
|
2014-05-19 15:15:39 +02:00
|
|
|
--
|
2010-10-22 14:49:12 +02:00
|
|
|
-- @param cond If this is false, throw a lua error with a backtrace.
|
2015-02-26 22:06:34 +01:00
|
|
|
-- @param[opt] message Message to print in the error.
|
2012-06-12 10:13:46 +02:00
|
|
|
function debug.assert(cond, message)
|
2010-10-22 14:49:12 +02:00
|
|
|
local message = message or cond
|
|
|
|
if not cond then
|
|
|
|
error(traceback("Assertion failed: '" .. tostring(message) .. "'"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-20 15:45:53 +01:00
|
|
|
--- Given a table (or any other data) return a string that contains its
|
2012-03-16 21:03:23 +01:00
|
|
|
-- tag, value and type. If data is a table then recursively call d_raw
|
|
|
|
-- on each of its values.
|
|
|
|
-- @param data Value to inspect.
|
|
|
|
-- @param shift Spaces to indent lines with.
|
|
|
|
-- @param tag The name of the value.
|
|
|
|
-- @return a string which contains tag, value, value type and table key/value
|
|
|
|
-- pairs if data is a table.
|
|
|
|
local function dump_raw(data, shift, tag)
|
|
|
|
local result = ""
|
|
|
|
|
|
|
|
if tag then
|
2012-04-19 14:59:37 +02:00
|
|
|
result = result .. tostring(tag) .. " : "
|
2012-03-16 21:03:23 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
if type(data) ~= "table" then
|
2012-04-19 14:59:37 +02:00
|
|
|
return result .. tostring(data) .. " (" .. type(data) .. ")"
|
2012-03-16 21:03:23 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
shift = (shift or "") .. " "
|
|
|
|
for k, v in pairs(data) do
|
|
|
|
result = result .. "\n" .. shift .. dump_raw(v, shift, k)
|
|
|
|
end
|
|
|
|
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Inspect the value in data.
|
|
|
|
-- @param data Value to inspect.
|
|
|
|
-- @param tag The name of the value.
|
|
|
|
-- @return a string that contains the expanded value of data.
|
2012-06-12 10:13:46 +02:00
|
|
|
function debug.dump_return(data, tag)
|
2012-03-16 21:03:23 +01:00
|
|
|
return dump_raw(data, nil, tag)
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Print the table (or any other value) to the console.
|
|
|
|
-- @param data Table to print.
|
|
|
|
-- @param tag The name of the table.
|
2012-06-12 10:13:46 +02:00
|
|
|
function debug.dump(data, tag)
|
2012-08-31 13:07:04 +02:00
|
|
|
print(debug.dump_return(data, tag))
|
2012-03-16 21:03:23 +01:00
|
|
|
end
|
|
|
|
|
2012-06-12 10:13:46 +02:00
|
|
|
return debug
|
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|