cgi-bin/tek/lib/debug.lua
author Timm S. Mueller <tmueller@neoscientists.org>
Sat, 24 Nov 2007 20:47:22 +0100
changeset 205 4b7e44d9cc88
parent 201 d52b05a9fe9c
child 226 f16ff668a409
permissions -rw-r--r--
More robust debugging facilities
     1 
     2 --
     3 --	tek.lib.debug
     4 --	Written by Timm S. Mueller <tmueller at neoscientists.org>
     5 --	See copyright notice in COPYRIGHT
     6 --
     7 
     8 local getinfo = require "debug".getinfo
     9 local date = require "os".date
    10 local stderr = require "io".stderr
    11 local tostring = tostring
    12 local ipairs = ipairs
    13 local type = type
    14 local unpack = unpack
    15 local select = select
    16 local time = os.time
    17 
    18 module "tek.lib.debug"
    19 
    20 level = 10 -- global default
    21 
    22 function print(lvl, msg, ...)
    23 	if level and lvl >= level then
    24 		local t = getinfo(3, "lS")
    25 -- 		stderr:write(("(%02d %d %s:%d) "):format(lvl,
    26 -- 			time(), t.short_src, t.currentline))
    27 		local arg = { }
    28 		for i = 1, select('#', ...) do
    29 			local v = select(i, ...)
    30 			if not v then
    31 				arg[i] = 0
    32 			else
    33 				arg[i] = type(v) ~= "number" and tostring(v) or v
    34 			end
    35 		end
    36 		stderr:write(("(%02d %d %s:%d) " .. msg):format(lvl, time(), t.short_src, t.currentline, unpack(arg)) .. "\n")
    37 	end
    38 end
    39 
    40 function execute(lvl, func, ...)
    41 	if level and lvl >= level then
    42 		func(...)
    43 	end
    44 end
    45 
    46 function trace(msg, ...) print(2, msg, ...) end
    47 function note(msg, ...) print(3, msg, ...) end
    48 function info(msg, ...) print(4, msg, ...) end
    49 function warn(msg, ...) print(5, msg, ...) end
    50 function error(msg, ...) print(10, msg, ...) end
    51 function fail(msg, ...) print(20, msg, ...) end
    52 
    53 function dotrace(msg, ...) execute(2, msg, ...) end
    54 function donote(msg, ...) execute(3, msg, ...) end
    55 function doinfo(msg, ...) execute(4, msg, ...) end
    56 function dowarn(msg, ...) execute(5, msg, ...) end
    57 function doerror(msg, ...) execute(10, msg, ...) end
    58 function dofail(msg, ...) execute(20, msg, ...) end