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