Class and Debug updated to latest versions
authorTimm S. Mueller <tmueller@neoscientists.org>
Mon, 17 Dec 2007 04:31:32 +0100
changeset 226f16ff668a409
parent 225 67a7baf150c1
child 227 cf52620e1530
Class and Debug updated to latest versions
cgi-bin/tek/class.lua
cgi-bin/tek/lib/debug.lua
     1.1 --- a/cgi-bin/tek/class.lua	Sat Dec 15 17:34:15 2007 +0100
     1.2 +++ b/cgi-bin/tek/class.lua	Mon Dec 17 04:31:32 2007 +0100
     1.3 @@ -1,38 +1,81 @@
     1.4 -
     1.5 +-------------------------------------------------------------------------------
     1.6  --
     1.7 ---	tek.class - Implements inheritance
     1.8 ---	Written by Timm S. Mueller <tmueller at neoscientists.org>
     1.9 +--	tek.class
    1.10 +--	Written by Timm S. Mueller <tmueller@neoscientists.org>
    1.11  --	See copyright notice in COPYRIGHT
    1.12  --
    1.13 +--	OVERVIEW::
    1.14 +--	Class implements class inheritance and the creation of objects.
    1.15 +--
    1.16 +-------------------------------------------------------------------------------
    1.17  
    1.18  local tostring = tostring
    1.19  local getmetatable = getmetatable
    1.20  local setmetatable = setmetatable
    1.21  
    1.22  module "tek.class"
    1.23 -_VERSION = "TEK Class 2.0"
    1.24 +_VERSION = "Class 2.5"
    1.25  
    1.26 -__index = _M
    1.27 +local Class = _M
    1.28  
    1.29 -function new(class, self)
    1.30 +Class.__index = _M
    1.31 +
    1.32 +-------------------------------------------------------------------------------
    1.33 +--	object = new(class[, object]):
    1.34 +--	Creates and returns a new object of the given class. Optionally, it just
    1.35 +--	prepares the specified table {{object}} for inheritance and attaches the
    1.36 +--	class methods and data.
    1.37 +-------------------------------------------------------------------------------
    1.38 +
    1.39 +function Class.new(class, self)
    1.40  	return setmetatable(self or { }, class)
    1.41  end
    1.42  
    1.43 -newClass = function(baseclass, class)
    1.44 -	class = class or { }
    1.45 +-------------------------------------------------------------------------------
    1.46 +--	class = newClass(superclass[, class]):
    1.47 +--	Derives a new class from the specified superclass. Optionally, an existing
    1.48 +--	table can be used for {{class}}. In this case, if a {{_NAME}} attribute
    1.49 +--	exists in the class table, it will be used. Otherwise, or if a new class
    1.50 +--	is created, {{class._NAME}} will be composed from {{superclass._NAME}}
    1.51 +--	and an unique identifier. The same functionality can be achieved by
    1.52 +--	calling a class like a function, so these two invocations are equivalent:
    1.53 +--			class = newClass(superclass)
    1.54 +--			class = superclass()
    1.55 +--	The second notation allows a superclass to be passed (as the second
    1.56 +--	argument) to Lua's {{module}} function, which will set up a child class
    1.57 +--	in the module table inheriting from {{superclass}}.
    1.58 +-------------------------------------------------------------------------------
    1.59 +
    1.60 +function Class.newClass(superclass, class)
    1.61 +	local class = class or { }
    1.62  	class.__index = class
    1.63  	class.__call = newClass
    1.64 -	class._NAME = class._NAME or baseclass._NAME ..
    1.65 +	class._NAME = class._NAME or superclass._NAME ..
    1.66  		tostring(class):gsub("^table: 0x(.*)$", ".%1")
    1.67 -	return setmetatable(class, baseclass)
    1.68 +	return setmetatable(class, superclass)
    1.69  end
    1.70  
    1.71 -__call = newClass
    1.72 +-------------------------------------------------------------------------------
    1.73 +--	class = object:getClass():
    1.74 +--	This function returns the class of the specified object.
    1.75 +-------------------------------------------------------------------------------
    1.76  
    1.77 -getClass = getmetatable
    1.78 +function Class:getClass()
    1.79 +	return getmetatable(self)
    1.80 +end
    1.81  
    1.82 -function getClassName(self)
    1.83 +-------------------------------------------------------------------------------
    1.84 +--	class = object:getClassName(): This function returns the {{_NAME}}
    1.85 +--	attribute of the specified object's class.
    1.86 +-------------------------------------------------------------------------------
    1.87 +
    1.88 +function Class:getClassName()
    1.89  	return self._NAME
    1.90  end
    1.91  
    1.92 +-------------------------------------------------------------------------------
    1.93 +--	make Class and its descendants functors:
    1.94 +-------------------------------------------------------------------------------
    1.95 +
    1.96 +__call = Class.newClass
    1.97  setmetatable(_M, { __call = newClass })
     2.1 --- a/cgi-bin/tek/lib/debug.lua	Sat Dec 15 17:34:15 2007 +0100
     2.2 +++ b/cgi-bin/tek/lib/debug.lua	Mon Dec 17 04:31:32 2007 +0100
     2.3 @@ -1,57 +1,102 @@
     2.4 -
     2.5 +-------------------------------------------------------------------------------
     2.6  --
     2.7  --	tek.lib.debug
     2.8  --	Written by Timm S. Mueller <tmueller at neoscientists.org>
     2.9  --	See copyright notice in COPYRIGHT
    2.10  --
    2.11 +--	OVERVIEW::
    2.12 +--	Debug library - implements debug output and debug levels:
    2.13 +--
    2.14 +--	2  || trace || used for tracking bugs
    2.15 +--	4  || info  || informational messages during development
    2.16 +--	5  || warn  || something unexpected happened
    2.17 +--	10 || error || something went wrong, e.g. allocation of a resource
    2.18 +--	20 || fail  || something went wrong that the program can't cope with
    2.19 +--
    2.20 +--	The default debug level is 10 ''error''. To set the debug level
    2.21 +--	globally, e.g.:
    2.22 +--			db = require "tek.lib.debug"
    2.23 +--			db.level = 4
    2.24 +--
    2.25 +--	The default debug output stream is {{stderr}}.
    2.26 +--	To override it globally, e.g.:
    2.27 +--			db = require "tek.lib.debug"
    2.28 +--			f = io.open("logfile", "w")
    2.29 +--			db.out = function(...) f:write(...) end
    2.30 +--
    2.31 +-------------------------------------------------------------------------------
    2.32  
    2.33  local getinfo = require "debug".getinfo
    2.34 -local date = require "os".date
    2.35  local stderr = require "io".stderr
    2.36  local tostring = tostring
    2.37 -local ipairs = ipairs
    2.38 +local tonumber = tonumber
    2.39  local type = type
    2.40  local unpack = unpack
    2.41  local select = select
    2.42  local time = os.time
    2.43  
    2.44  module "tek.lib.debug"
    2.45 +_VERSION = "Debug 1.1"
    2.46  
    2.47 -level = 10 -- global default
    2.48 +-- global defaults:
    2.49 +level = 10
    2.50 +out = function(...) stderr:write(...) end
    2.51 +
    2.52 +-------------------------------------------------------------------------------
    2.53 +--	print(lvl, msg, ...): prints formatted text if the global debug level
    2.54 +--	is less or equal the specified level.
    2.55 +-------------------------------------------------------------------------------
    2.56  
    2.57  function print(lvl, msg, ...)
    2.58  	if level and lvl >= level then
    2.59  		local t = getinfo(3, "lS")
    2.60 --- 		stderr:write(("(%02d %d %s:%d) "):format(lvl,
    2.61 --- 			time(), t.short_src, t.currentline))
    2.62  		local arg = { }
    2.63  		for i = 1, select('#', ...) do
    2.64  			local v = select(i, ...)
    2.65 -			if not v then
    2.66 -				arg[i] = 0
    2.67 -			else
    2.68 -				arg[i] = type(v) ~= "number" and tostring(v) or v
    2.69 -			end
    2.70 +			arg[i] = v and type(v) ~= "number" and tostring(v) or v or 0
    2.71  		end
    2.72 -		stderr:write(("(%02d %d %s:%d) " .. msg):format(lvl, time(), t.short_src, t.currentline, unpack(arg)) .. "\n")
    2.73 +		out(("(%02d %d %s:%d) " .. msg):format(lvl,
    2.74 +			time(), t.short_src, t.currentline, unpack(arg)) .. "\n")
    2.75  	end
    2.76  end
    2.77  
    2.78 +-------------------------------------------------------------------------------
    2.79 +--	execute(lvl, func, ...): executes the specified function if the global
    2.80 +--	debug library is less or equal the specified level.
    2.81 +-------------------------------------------------------------------------------
    2.82 +
    2.83  function execute(lvl, func, ...)
    2.84  	if level and lvl >= level then
    2.85 -		func(...)
    2.86 +		return func(...)
    2.87  	end
    2.88  end
    2.89  
    2.90 +-------------------------------------------------------------------------------
    2.91 +--	trace(msg, ...): prints formatted debug info with ''trace'' level
    2.92 +-------------------------------------------------------------------------------
    2.93  function trace(msg, ...) print(2, msg, ...) end
    2.94 -function note(msg, ...) print(3, msg, ...) end
    2.95 +
    2.96 +-------------------------------------------------------------------------------
    2.97 +--	info(msg, ...): prints formatted debug info with ''info'' level
    2.98 +-------------------------------------------------------------------------------
    2.99  function info(msg, ...) print(4, msg, ...) end
   2.100 +
   2.101 +-------------------------------------------------------------------------------
   2.102 +--	warn(msg, ...): prints formatted debug info with ''warn'' level
   2.103 +-------------------------------------------------------------------------------
   2.104  function warn(msg, ...) print(5, msg, ...) end
   2.105 +
   2.106 +-------------------------------------------------------------------------------
   2.107 +--	error(msg, ...): prints formatted debug info with ''error'' level
   2.108 +-------------------------------------------------------------------------------
   2.109  function error(msg, ...) print(10, msg, ...) end
   2.110 +
   2.111 +-------------------------------------------------------------------------------
   2.112 +--	fail(msg, ...): prints formatted debug info with ''fail'' level
   2.113 +-------------------------------------------------------------------------------
   2.114  function fail(msg, ...) print(20, msg, ...) end
   2.115  
   2.116  function dotrace(msg, ...) execute(2, msg, ...) end
   2.117 -function donote(msg, ...) execute(3, msg, ...) end
   2.118  function doinfo(msg, ...) execute(4, msg, ...) end
   2.119  function dowarn(msg, ...) execute(5, msg, ...) end
   2.120  function doerror(msg, ...) execute(10, msg, ...) end