开发者

How to log arguments in runtime?

I created a new function to log every method from a class in runtime.

The problem that I have is in this line :

id value = va_arg(stackFrame, id);

Doesn't convert the type of object inside the arguments.

Any idea on what I'm doing wrong? Is there another way to do this?

void Debug开发者_开发技巧Arguments ( id self, SEL _cmd,...)

{

id receiver = self;
SEL receiverSelector = _cmd;

va_list stackFrame;

va_start(stackFrame, _cmd);

NSMethodSignature *signature
= [receiver methodSignatureForSelector:receiverSelector];

NSUInteger count = [signature numberOfArguments];

NSUInteger index = 2;

for (; index < count; index++)
{

    id value = va_arg(stackFrame, id);
        if (!value)
        {
            NSLog(@"Arguments: %@",value);    
        }
} 
va_end(stackFrame);

}


I call the function InitDebug from a class like this : - (void)MyTest:(NSString *)string {
InitDebug(self, _cmd); } I hope to log the Argument string from the method MyTest.

In the future, it is helpful to show all the code.

In any case, you can't do that; when you call InitDebug(...), you are pushing a new frame onto the stack and va_arg() will decode in the context of that frame, not the surrounding frame. Nor can you "go up the stack" and start grubbing about in the arguments of the calling frame as there is no guarantee that the calling frame is even preserved at that point.

If you really want to do something like this, you would probably want to subclass NSProxy and use it is a proxy between the caller and whatever object you want to log. You could then leverage the built-in forwarding mechanism of Objective-C to muck about with the arguments.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜