سؤال

I am looking to develop a framework, for which I dont want get into details.

Suppose if I am having 100+ classes with 1000+ methods in my iPhone application. In this scenario I want to add NSLog in each method(at start or end or both) of each class.

Manually adding NSLog is possible but is there any better solution? Like building application in such a way that I can add this Log without me having to do manual work.

Thanks and Regards,


Denis,

Your answer was most useful throughout many forums. Thanks a lot.

The reason I'm looking for something like this is, we have many client projects and many time it happens that we get some crashing or other issues while QA and UAT. ~80% of them are not reproducible or require some particular scenario. We were using .crash and dsym to track such issues. But it is not that useful in such scenario.

What I'm looking for is providing add hoc build which will log the steps which user has followed so that it will make easy to reproduce such issues.

Currently I am using precompiled headers and first method you have mentioned (searching for the first opening brace then replace it with macro).

I will look into DTrace and objc_msgSend as you mentioned. I will google out these meanwhile if you have any preferred tutorials it will be great.

Thanks and Regards, :D

هل كانت مفيدة؟

المحلول

So you want to add a trace facility to your code?

Apple doesn't provide anything like this. You'll have to add your trace facility yourself. If your source code style is consistent, this might be relatively easy to do automatically, something like searching for the first opening brace following a line starting with a minus (or plus) sign...

Alternatively, you might want to use the public Objective-C runtime functions to enumerate all classes and all their methods, then method-swizzle each of them with another one that NSLog before jumping to the original.

Alternatively, you can take the open source implementation of objc_msgSend and insert a call to NSLog at the beginning. Note that obj_msgSend tail calls into the method, so that will prevent you from adding a call upon return. Be prepared to humongous output of course. You might want to condition your call to NSLog to the value of the selector parameter to objc_msgSend (such as a common prefix).

Finally, the best way to trace is probably to attach a DTrace probe to the entry to objc_msgSend. For the same tail-call reason mentioned above, you won't be able to attach a DTrace probe to its return though.

But the better question is why do you want to do that.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top