lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


If you don't want to meddle with the debug interface you can use something 
like this:

function query() -- do stuff
  return 'query'
end

function insert()
  return 'insert'
end

print'pre-wrap'
print( query() )
print( insert() )

--% Wraps functions with LOG messages.
--@ list (table) List of function _names_
--@ [table] (table) Table containing functions (global by default)
function wrap( list, table )
        table = table or _G
        for _,fn_name in ipairs( list ) do
                local name = fn_name
                local old_fn = table[name]
                table[name] = function( ... )
                        print( 'LOG: function '..name.. ' in table '
                                ..tostring(table)..' called' )
                        return old_fn( unpack( arg ))
                end
        end
end

print'---------'
local list = { 'query', 'insert' }
wrap( list )

print'post-wrap'
print( query() )
print( insert() )


-- Method example
local object = {}
function object:method( i )
        return 'in method: '..i
end

print '==============='
print 'pre-wrap'
print( object:method( 42 ))
wrap( {'method'}, object )
print 'post-wrap'
print( object:method( 42 ))


It's not as advanced and fulfilling (since one still needs to write down 
function names as strings), but may be simpler to understand (one can always 
hope). Note that you could very simply add a 'custom log' function parameter 
to wrap. You could also handle the parameters being passed in said log 
function, etc.

Regards,

	Pedro.

On Wednesday 22 October 2003 16:05, Fabrício de Alexandria wrote:
> Thanks for your help but I think I didn't understand your example or this
> is not what I want to do.
>
> Let me try to explain my situation in a better way.
>
> I have these three functions:
>
> function query()
> -- Function code for query a register
> end
>
> function insert()
> -- Function code for insert a register
> end
>
> function remove()
> -- Function code for remove a register
> end
>
> -- Execute just two functions
> query()
> remove()
>
>
> In another different file I should have one list (I still don't know how
> to make this list. It has to be in a generic way to be used for any
> program just changing the list) of the functions that I want to intercept
> (for example, the query and remove). Before or after the execution of
> them, it must execute the function logging and then return to execute the
> next function.
>
> function logging()
> -- Function code for logging
> end
>
>
> I don't want to call the logging function inside the query or remove
> functions, but intercept them, execute logging() and return to the normal
> execution of the program. Is that possible?
>
> > Roughly....
> >
> > q = x
> > function x()
> > 	q()
> > 	z()
> > 	x = q -- auto "unhook" x
> > end
> >
> > -----------
> > With some parameter/return handling....
> >
> > q = x
> > function x(...)
> > 	Rets = {q(unpack(arg))}
> > 	z()
> > 	x = q -- auto "unhook" x
> > 	return unpack(Rets)
> > end
> >
> > -------
> > Of course this doesn't catch other references to x, such as....
> >
> > b = x
> > .....
> > q = x
> > function x()
> > 	q()
> > 	z()
> > 	x = q -- auto "unhook" x
> > end
> > ...
> > b() -- Does not call "hook"
> > x() -- Does call "hook"
> >
> > -------
> > If this isn't in the class of what you wanted, then you should probably
> > spend some quality time with the Debug interface.
> >
> > -----Original Message-----
> > From: lua-bounces@bazar2.conectiva.com.br
> > [mailto:lua-bounces@bazar2.conectiva.com.br]On Behalf Of Fabrício de
> > Alexandria
> > Sent: Wednesday, October 22, 2003 11:05 AM
> > To: lua@bazar2.conectiva.com.br
> > Subject: Intercept function call
> >
> >
> >
> >
> > Here is my situation. Suppose that I have three functions:
> >
> > function x()
> >   print "x"
> > end
> >
> > function y()
> >   print "y"
> > end
> >
> > function z()
> >   print "z"
> > end
> >
> > x()
> > y()
> >
> >
> > I want to execute x() and y() but before or after the execution of x() I
> > want to execute z(). Something like during the execution of the program
> > I want to intercept the x() function call, execute z() and then return
> > to the normal execution of the program. How can I do that?
> >
> > I searched something like this in the archive but I didn't find
> > anything.
> >
> >
> >
> >
> > Fabrício de Alexandria Fernandes
> > HomePage: http://www.ppgsc.ufrn.br/~fabricio
> > E-mail: fabricio@ppgsc.ufrn.br
> > UFRN - CCET - DIMAp
> > Natal - RN - Brasil
>
> Fabrício de Alexandria Fernandes
> HomePage: http://www.ppgsc.ufrn.br/~fabricio
> E-mail: fabricio@ppgsc.ufrn.br
> UFRN - CCET - DIMAp
> Natal - RN - Brasil