diff -r f16ff668a409 -r 3a4291950c9f cgi-bin/tek/lib/debug.lua --- a/cgi-bin/tek/lib/debug.lua Mon Dec 17 04:31:32 2007 +0100 +++ b/cgi-bin/tek/lib/debug.lua Fri Sep 12 22:00:42 2008 +0200 @@ -1,49 +1,73 @@ ------------------------------------------------------------------------------- -- -- tek.lib.debug --- Written by Timm S. Mueller +-- Written by Timm S. Mueller -- See copyright notice in COPYRIGHT -- -- OVERVIEW:: -- Debug library - implements debug output and debug levels: -- --- 2 || trace || used for tracking bugs --- 4 || info || informational messages during development --- 5 || warn || something unexpected happened --- 10 || error || something went wrong, e.g. allocation of a resource --- 20 || fail || something went wrong that the program can't cope with +-- 2 || TRACE || used for tracking bugs +-- 4 || INFO || informational messages +-- 5 || WARN || something unexpected happened +-- 10 || ERROR || something went wrong, e.g. resource unavailable +-- 20 || FAIL || something went wrong that can't be coped with -- --- The default debug level is 10 ''error''. To set the debug level +-- The default debug level is 10 {{ERROR}}. To set the debug level -- globally, e.g.: -- db = require "tek.lib.debug" --- db.level = 4 +-- db.level = db.INFO -- -- The default debug output stream is {{stderr}}. -- To override it globally, e.g.: -- db = require "tek.lib.debug" --- f = io.open("logfile", "w") --- db.out = function(...) f:write(...) end +-- db.out = io.open("logfile", "w") +-- +-- FUNCTIONS:: +-- - debug.console() - Enter debug console +-- - debug.dump() - Dump a table recursively +-- - debug.error() - Print a text in the {{ERROR}} debug level +-- - debug.execute() - Execute a function in the specified debug level +-- - debug.fail() - Print a text in the {{FAIL}} debug level +-- - debug.info() - Print a text in the {{INFO}} debug level +-- - debug.print() - Print a text in the specified debug level +-- - debug.stacktrace() - Print a stacktrace in the specified debug level +-- - debug.trace() - Print a text in the {{TRACE}} debug level +-- - debug.warn() - Print a text in the {{WARN}} debug level -- ------------------------------------------------------------------------------- -local getinfo = require "debug".getinfo -local stderr = require "io".stderr +local debug = require "debug" +local getinfo = debug.getinfo +local stderr = io.stderr +local pairs = pairs +local select = select +local time = os.time +local tonumber = tonumber local tostring = tostring -local tonumber = tonumber +local traceback = debug.traceback local type = type local unpack = unpack -local select = select -local time = os.time module "tek.lib.debug" -_VERSION = "Debug 1.1" +_VERSION = "Debug 4.0" + +-- symbolic: + +TRACE = 2 +INFO = 4 +WARN = 5 +ERROR = 10 +FAIL = 20 -- global defaults: -level = 10 -out = function(...) stderr:write(...) end + +level = ERROR +out = stderr +wrout = function(...) out:write(...) end ------------------------------------------------------------------------------- --- print(lvl, msg, ...): prints formatted text if the global debug level +-- print(lvl, msg, ...): Prints formatted text if the global debug level -- is less or equal the specified level. ------------------------------------------------------------------------------- @@ -55,13 +79,13 @@ local v = select(i, ...) arg[i] = v and type(v) ~= "number" and tostring(v) or v or 0 end - out(("(%02d %d %s:%d) " .. msg):format(lvl, + wrout(("(%02d %d %s:%d) " .. msg):format(lvl, time(), t.short_src, t.currentline, unpack(arg)) .. "\n") end end ------------------------------------------------------------------------------- --- execute(lvl, func, ...): executes the specified function if the global +-- execute(lvl, func, ...): Executes the specified function if the global -- debug library is less or equal the specified level. ------------------------------------------------------------------------------- @@ -72,32 +96,84 @@ end ------------------------------------------------------------------------------- --- trace(msg, ...): prints formatted debug info with ''trace'' level +-- trace(msg, ...): Prints formatted debug info with {{TRACE}} level ------------------------------------------------------------------------------- function trace(msg, ...) print(2, msg, ...) end ------------------------------------------------------------------------------- --- info(msg, ...): prints formatted debug info with ''info'' level +-- info(msg, ...): Prints formatted debug info with {{INFO}} level ------------------------------------------------------------------------------- function info(msg, ...) print(4, msg, ...) end ------------------------------------------------------------------------------- --- warn(msg, ...): prints formatted debug info with ''warn'' level +-- warn(msg, ...): Prints formatted debug info with {{WARN}} level ------------------------------------------------------------------------------- function warn(msg, ...) print(5, msg, ...) end ------------------------------------------------------------------------------- --- error(msg, ...): prints formatted debug info with ''error'' level +-- error(msg, ...): Prints formatted debug info with {{ERROR}} level ------------------------------------------------------------------------------- function error(msg, ...) print(10, msg, ...) end ------------------------------------------------------------------------------- --- fail(msg, ...): prints formatted debug info with ''fail'' level +-- fail(msg, ...): Prints formatted debug info with {{FAIL}} level ------------------------------------------------------------------------------- function fail(msg, ...) print(20, msg, ...) end -function dotrace(msg, ...) execute(2, msg, ...) end -function doinfo(msg, ...) execute(4, msg, ...) end -function dowarn(msg, ...) execute(5, msg, ...) end -function doerror(msg, ...) execute(10, msg, ...) end -function dofail(msg, ...) execute(20, msg, ...) end +------------------------------------------------------------------------------- +-- stacktrace(debuglevel, stacklevel): Prints a stacktrace starting at +-- the function of the given {{level}} on the stack (excluding the +-- {{stracktrace}} function itself) if the global debug level is less +-- or equal the specified {{debuglevel}}. +------------------------------------------------------------------------------- + +function stacktrace(lvl, level) + print(lvl, traceback("", level or 1 + 1)) +end + +------------------------------------------------------------------------------- +-- console(): Enter the debug console. +------------------------------------------------------------------------------- + +function console() + stderr:write('Entering the debug console.\n') + stderr:write('To redirect the output, e.g.:\n') + stderr:write(' tek.lib.debug.out = io.open("logfile", "w")\n') + stderr:write('To dump a table, e.g.:\n') + stderr:write(' tek.lib.debug.dump("app", app)\n') + stderr:write('Use "cont" to continue.\n') + debug.debug() +end + +------------------------------------------------------------------------------- +-- dump(name, table): Dump a table +------------------------------------------------------------------------------- + +local function basicSerialize(o) + if type(o) == "string" then + return ("%q"):format(o) + elseif type(o) == "userdata" then + return "" + end + return tostring(o) +end + +function dump(name, value, saved) + saved = saved or {} -- initial value + wrout(name, " = ") + local t = type(value) + if t == "table" then + if saved[value] then -- value already saved? + wrout(saved[value], "\n") + else + saved[value] = name -- save name for next time + wrout("{}\n") + for k,v in pairs(value) do -- save its fields + local fieldname = ("%s[%s]"):format(name, basicSerialize(k)) + dump(fieldname, v, saved) + end + end + else + wrout(basicSerialize(value), "\n") + end +end