2010-10-22 14:49:12 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- @author Uli Schlachter
|
|
|
|
-- @copyright 2010 Uli Schlachter
|
2019-06-06 09:40:17 +02:00
|
|
|
-- @utillib gears.debug
|
2010-10-22 14:49:12 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local tostring = tostring
|
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
|
|
|
|
2015-02-20 15:45:53 +01:00
|
|
|
--- Given a table (or any other data) return a string that contains its
|
2015-07-29 18:44:18 +02:00
|
|
|
-- tag, value and type. If data is a table then recursively call `dump_raw`
|
2012-03-16 21:03:23 +01:00
|
|
|
-- on each of its values.
|
|
|
|
-- @param data Value to inspect.
|
|
|
|
-- @param shift Spaces to indent lines with.
|
|
|
|
-- @param tag The name of the value.
|
2015-07-29 18:44:18 +02:00
|
|
|
-- @tparam[opt=10] int depth Depth of recursion.
|
2012-03-16 21:03:23 +01:00
|
|
|
-- @return a string which contains tag, value, value type and table key/value
|
|
|
|
-- pairs if data is a table.
|
2015-07-29 18:44:18 +02:00
|
|
|
local function dump_raw(data, shift, tag, depth)
|
|
|
|
depth = depth == nil and 10 or depth or 0
|
2012-03-16 21:03:23 +01:00
|
|
|
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
|
|
|
|
|
2015-07-29 18:44:18 +02:00
|
|
|
if type(data) == "table" and depth > 0 then
|
|
|
|
shift = (shift or "") .. " "
|
|
|
|
result = result .. tostring(data)
|
|
|
|
for k, v in pairs(data) do
|
|
|
|
result = result .. "\n" .. shift .. dump_raw(v, shift, k, depth - 1)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
result = result .. tostring(data) .. " (" .. type(data) .. ")"
|
|
|
|
if depth == 0 and type(data) == "table" then
|
|
|
|
result = result .. " […]"
|
|
|
|
end
|
2012-03-16 21:03:23 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Inspect the value in data.
|
|
|
|
-- @param data Value to inspect.
|
|
|
|
-- @param tag The name of the value.
|
2015-07-29 18:44:18 +02:00
|
|
|
-- @tparam[opt] int depth Depth of recursion.
|
|
|
|
-- @return string A string that contains the expanded value of data.
|
|
|
|
function debug.dump_return(data, tag, depth)
|
|
|
|
return dump_raw(data, nil, tag, depth)
|
2012-03-16 21:03:23 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Print the table (or any other value) to the console.
|
|
|
|
-- @param data Table to print.
|
|
|
|
-- @param tag The name of the table.
|
2015-07-29 18:44:18 +02:00
|
|
|
-- @tparam[opt] int depth Depth of recursion.
|
|
|
|
function debug.dump(data, tag, depth)
|
|
|
|
print(debug.dump_return(data, tag, depth))
|
2012-03-16 21:03:23 +01:00
|
|
|
end
|
|
|
|
|
2016-02-28 12:05:55 +01:00
|
|
|
--- Print an warning message
|
2015-12-22 19:28:05 +01:00
|
|
|
-- @tparam string message The warning message to print
|
|
|
|
function debug.print_warning(message)
|
2017-08-10 14:08:32 +02:00
|
|
|
io.stderr:write(os.date("%Y-%m-%d %T W: awesome: ") .. tostring(message) .. "\n")
|
2015-12-22 19:28:05 +01:00
|
|
|
end
|
|
|
|
|
2016-02-28 12:05:55 +01:00
|
|
|
--- Print an error message
|
2015-12-22 19:28:05 +01:00
|
|
|
-- @tparam string message The error message to print
|
|
|
|
function debug.print_error(message)
|
2017-08-10 14:08:32 +02:00
|
|
|
io.stderr:write(os.date("%Y-%m-%d %T E: awesome: ") .. tostring(message) .. "\n")
|
2015-12-22 19:28:05 +01:00
|
|
|
end
|
|
|
|
|
2017-03-15 06:08:40 +01:00
|
|
|
local displayed_deprecations = {}
|
|
|
|
--- Display a deprecation notice, but only once per traceback.
|
2017-06-21 02:06:32 +02:00
|
|
|
--
|
|
|
|
-- This function also emits the `debug::deprecate` signal on the `awesome`
|
|
|
|
-- global object.
|
|
|
|
--
|
2017-03-15 06:08:40 +01:00
|
|
|
-- @param[opt] see The message to a new method / function to use.
|
|
|
|
-- @tparam table args Extra arguments
|
|
|
|
-- @tparam boolean args.raw Print the message as-is without the automatic context
|
|
|
|
-- @tparam integer args.deprecated_in Print the message only when Awesome's
|
|
|
|
-- version is equal to or greater than deprecated_in.
|
|
|
|
function debug.deprecate(see, args)
|
|
|
|
args = args or {}
|
|
|
|
if args.deprecated_in then
|
|
|
|
local dep_ver = "v" .. tostring(args.deprecated_in)
|
|
|
|
if awesome.version < dep_ver then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
local tb = _G.debug.traceback()
|
|
|
|
if displayed_deprecations[tb] then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
displayed_deprecations[tb] = true
|
|
|
|
|
|
|
|
-- Get function name/desc from caller.
|
|
|
|
local info = _G.debug.getinfo(2, "n")
|
|
|
|
local funcname = info.name or "?"
|
|
|
|
local msg = "awful: function " .. funcname .. " is deprecated"
|
|
|
|
if see then
|
|
|
|
if args.raw then
|
|
|
|
msg = see
|
|
|
|
elseif string.sub(see, 1, 3) == 'Use' then
|
|
|
|
msg = msg .. ". " .. see
|
|
|
|
else
|
|
|
|
msg = msg .. ", see " .. see
|
|
|
|
end
|
|
|
|
end
|
|
|
|
debug.print_warning(msg .. ".\n" .. tb)
|
2017-06-21 02:06:32 +02:00
|
|
|
|
|
|
|
if awesome then
|
|
|
|
awesome.emit_signal("debug::deprecation", msg, see, args)
|
|
|
|
end
|
2017-03-15 06:08:40 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Create a class proxy with deprecation messages.
|
|
|
|
-- This is useful when a class has moved somewhere else.
|
|
|
|
-- @tparam table fallback The new class
|
|
|
|
-- @tparam string old_name The old class name
|
|
|
|
-- @tparam string new_name The new class name
|
|
|
|
-- @treturn table A proxy class.
|
|
|
|
function debug.deprecate_class(fallback, old_name, new_name)
|
|
|
|
local message = old_name.." has been renamed to "..new_name
|
|
|
|
|
|
|
|
local function call(_,...)
|
|
|
|
debug.deprecate(message, {raw = true})
|
|
|
|
|
|
|
|
return fallback(...)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function index(_, k)
|
|
|
|
debug.deprecate(message, {raw = true})
|
|
|
|
|
|
|
|
return fallback[k]
|
|
|
|
end
|
|
|
|
|
|
|
|
local function newindex(_, k, v)
|
|
|
|
debug.deprecate(message, {raw = true})
|
|
|
|
|
|
|
|
fallback[k] = v
|
|
|
|
end
|
|
|
|
|
|
|
|
return setmetatable({}, {__call = call, __index = index, __newindex = newindex})
|
|
|
|
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
|