cgi-bin/tek/lib/debug.lua
author Timm S. Mueller <tmueller@neoscientists.org>
Mon, 17 Dec 2007 04:31:32 +0100
changeset 226 f16ff668a409
parent 205 4b7e44d9cc88
child 244 3a4291950c9f
permissions -rw-r--r--
Class and Debug updated to latest versions
     1 -------------------------------------------------------------------------------
     2 --
     3 --	tek.lib.debug
     4 --	Written by Timm S. Mueller <tmueller at neoscientists.org>
     5 --	See copyright notice in COPYRIGHT
     6 --
     7 --	OVERVIEW::
     8 --	Debug library - implements debug output and debug levels:
     9 --
    10 --	2  || trace || used for tracking bugs
    11 --	4  || info  || informational messages during development
    12 --	5  || warn  || something unexpected happened
    13 --	10 || error || something went wrong, e.g. allocation of a resource
    14 --	20 || fail  || something went wrong that the program can't cope with
    15 --
    16 --	The default debug level is 10 ''error''. To set the debug level
    17 --	globally, e.g.:
    18 --			db = require "tek.lib.debug"
    19 --			db.level = 4
    20 --
    21 --	The default debug output stream is {{stderr}}.
    22 --	To override it globally, e.g.:
    23 --			db = require "tek.lib.debug"
    24 --			f = io.open("logfile", "w")
    25 --			db.out = function(...) f:write(...) end
    26 --
    27 -------------------------------------------------------------------------------
    28 
    29 local getinfo = require "debug".getinfo
    30 local stderr = require "io".stderr
    31 local tostring = tostring
    32 local tonumber = tonumber
    33 local type = type
    34 local unpack = unpack
    35 local select = select
    36 local time = os.time
    37 
    38 module "tek.lib.debug"
    39 _VERSION = "Debug 1.1"
    40 
    41 -- global defaults:
    42 level = 10
    43 out = function(...) stderr:write(...) end
    44 
    45 -------------------------------------------------------------------------------
    46 --	print(lvl, msg, ...): prints formatted text if the global debug level
    47 --	is less or equal the specified level.
    48 -------------------------------------------------------------------------------
    49 
    50 function print(lvl, msg, ...)
    51 	if level and lvl >= level then
    52 		local t = getinfo(3, "lS")
    53 		local arg = { }
    54 		for i = 1, select('#', ...) do
    55 			local v = select(i, ...)
    56 			arg[i] = v and type(v) ~= "number" and tostring(v) or v or 0
    57 		end
    58 		out(("(%02d %d %s:%d) " .. msg):format(lvl,
    59 			time(), t.short_src, t.currentline, unpack(arg)) .. "\n")
    60 	end
    61 end
    62 
    63 -------------------------------------------------------------------------------
    64 --	execute(lvl, func, ...): executes the specified function if the global
    65 --	debug library is less or equal the specified level.
    66 -------------------------------------------------------------------------------
    67 
    68 function execute(lvl, func, ...)
    69 	if level and lvl >= level then
    70 		return func(...)
    71 	end
    72 end
    73 
    74 -------------------------------------------------------------------------------
    75 --	trace(msg, ...): prints formatted debug info with ''trace'' level
    76 -------------------------------------------------------------------------------
    77 function trace(msg, ...) print(2, msg, ...) end
    78 
    79 -------------------------------------------------------------------------------
    80 --	info(msg, ...): prints formatted debug info with ''info'' level
    81 -------------------------------------------------------------------------------
    82 function info(msg, ...) print(4, msg, ...) end
    83 
    84 -------------------------------------------------------------------------------
    85 --	warn(msg, ...): prints formatted debug info with ''warn'' level
    86 -------------------------------------------------------------------------------
    87 function warn(msg, ...) print(5, msg, ...) end
    88 
    89 -------------------------------------------------------------------------------
    90 --	error(msg, ...): prints formatted debug info with ''error'' level
    91 -------------------------------------------------------------------------------
    92 function error(msg, ...) print(10, msg, ...) end
    93 
    94 -------------------------------------------------------------------------------
    95 --	fail(msg, ...): prints formatted debug info with ''fail'' level
    96 -------------------------------------------------------------------------------
    97 function fail(msg, ...) print(20, msg, ...) end
    98 
    99 function dotrace(msg, ...) execute(2, msg, ...) end
   100 function doinfo(msg, ...) execute(4, msg, ...) end
   101 function dowarn(msg, ...) execute(5, msg, ...) end
   102 function doerror(msg, ...) execute(10, msg, ...) end
   103 function dofail(msg, ...) execute(20, msg, ...) end