1.1 --- a/cgi-bin/tek/lib/debug.lua Sat Nov 24 20:47:22 2007 +0100
1.2 +++ b/cgi-bin/tek/lib/debug.lua Mon Dec 17 04:31:32 2007 +0100
1.3 @@ -1,57 +1,102 @@
1.4 -
1.5 +-------------------------------------------------------------------------------
1.6 --
1.7 -- tek.lib.debug
1.8 -- Written by Timm S. Mueller <tmueller at neoscientists.org>
1.9 -- See copyright notice in COPYRIGHT
1.10 --
1.11 +-- OVERVIEW::
1.12 +-- Debug library - implements debug output and debug levels:
1.13 +--
1.14 +-- 2 || trace || used for tracking bugs
1.15 +-- 4 || info || informational messages during development
1.16 +-- 5 || warn || something unexpected happened
1.17 +-- 10 || error || something went wrong, e.g. allocation of a resource
1.18 +-- 20 || fail || something went wrong that the program can't cope with
1.19 +--
1.20 +-- The default debug level is 10 ''error''. To set the debug level
1.21 +-- globally, e.g.:
1.22 +-- db = require "tek.lib.debug"
1.23 +-- db.level = 4
1.24 +--
1.25 +-- The default debug output stream is {{stderr}}.
1.26 +-- To override it globally, e.g.:
1.27 +-- db = require "tek.lib.debug"
1.28 +-- f = io.open("logfile", "w")
1.29 +-- db.out = function(...) f:write(...) end
1.30 +--
1.31 +-------------------------------------------------------------------------------
1.32
1.33 local getinfo = require "debug".getinfo
1.34 -local date = require "os".date
1.35 local stderr = require "io".stderr
1.36 local tostring = tostring
1.37 -local ipairs = ipairs
1.38 +local tonumber = tonumber
1.39 local type = type
1.40 local unpack = unpack
1.41 local select = select
1.42 local time = os.time
1.43
1.44 module "tek.lib.debug"
1.45 +_VERSION = "Debug 1.1"
1.46
1.47 -level = 10 -- global default
1.48 +-- global defaults:
1.49 +level = 10
1.50 +out = function(...) stderr:write(...) end
1.51 +
1.52 +-------------------------------------------------------------------------------
1.53 +-- print(lvl, msg, ...): prints formatted text if the global debug level
1.54 +-- is less or equal the specified level.
1.55 +-------------------------------------------------------------------------------
1.56
1.57 function print(lvl, msg, ...)
1.58 if level and lvl >= level then
1.59 local t = getinfo(3, "lS")
1.60 --- stderr:write(("(%02d %d %s:%d) "):format(lvl,
1.61 --- time(), t.short_src, t.currentline))
1.62 local arg = { }
1.63 for i = 1, select('#', ...) do
1.64 local v = select(i, ...)
1.65 - if not v then
1.66 - arg[i] = 0
1.67 - else
1.68 - arg[i] = type(v) ~= "number" and tostring(v) or v
1.69 - end
1.70 + arg[i] = v and type(v) ~= "number" and tostring(v) or v or 0
1.71 end
1.72 - stderr:write(("(%02d %d %s:%d) " .. msg):format(lvl, time(), t.short_src, t.currentline, unpack(arg)) .. "\n")
1.73 + out(("(%02d %d %s:%d) " .. msg):format(lvl,
1.74 + time(), t.short_src, t.currentline, unpack(arg)) .. "\n")
1.75 end
1.76 end
1.77
1.78 +-------------------------------------------------------------------------------
1.79 +-- execute(lvl, func, ...): executes the specified function if the global
1.80 +-- debug library is less or equal the specified level.
1.81 +-------------------------------------------------------------------------------
1.82 +
1.83 function execute(lvl, func, ...)
1.84 if level and lvl >= level then
1.85 - func(...)
1.86 + return func(...)
1.87 end
1.88 end
1.89
1.90 +-------------------------------------------------------------------------------
1.91 +-- trace(msg, ...): prints formatted debug info with ''trace'' level
1.92 +-------------------------------------------------------------------------------
1.93 function trace(msg, ...) print(2, msg, ...) end
1.94 -function note(msg, ...) print(3, msg, ...) end
1.95 +
1.96 +-------------------------------------------------------------------------------
1.97 +-- info(msg, ...): prints formatted debug info with ''info'' level
1.98 +-------------------------------------------------------------------------------
1.99 function info(msg, ...) print(4, msg, ...) end
1.100 +
1.101 +-------------------------------------------------------------------------------
1.102 +-- warn(msg, ...): prints formatted debug info with ''warn'' level
1.103 +-------------------------------------------------------------------------------
1.104 function warn(msg, ...) print(5, msg, ...) end
1.105 +
1.106 +-------------------------------------------------------------------------------
1.107 +-- error(msg, ...): prints formatted debug info with ''error'' level
1.108 +-------------------------------------------------------------------------------
1.109 function error(msg, ...) print(10, msg, ...) end
1.110 +
1.111 +-------------------------------------------------------------------------------
1.112 +-- fail(msg, ...): prints formatted debug info with ''fail'' level
1.113 +-------------------------------------------------------------------------------
1.114 function fail(msg, ...) print(20, msg, ...) end
1.115
1.116 function dotrace(msg, ...) execute(2, msg, ...) end
1.117 -function donote(msg, ...) execute(3, msg, ...) end
1.118 function doinfo(msg, ...) execute(4, msg, ...) end
1.119 function dowarn(msg, ...) execute(5, msg, ...) end
1.120 function doerror(msg, ...) execute(10, msg, ...) end