0

I would like to NSLog the current class file that the simulator loads, in order to make sure the class file loaded currently. What parameter should I code?

Is it possible?

Thanks.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
K.W.
  • 141
  • 1
  • 15
  • You seem confused about how compiled code works. Perhaps you can explain in more detail what you are trying to accomplish and we can help you work it out. It isn't generally possible to list the contents of a source file in your built application because the source file has been converted into machine code and the application has no record of the original text that you typed at that point. – user1118321 Sep 05 '13 at 04:07

1 Answers1

2

You can use the preprocessor directive __FILE__ to access the file path of the currently executing code. This flag is set at compile-time, but for usage in an NSLog, this shouldn't matter.

You can use it like this:

NSLog("Log called from file %s", __FILE__);
==> Log called from file /Developer Projects/Objective-C/Mac/test/test/AppDelegate.m

There are also other preprocessor variables you can use, such as __LINE__ and __PRETTY_FUNCTION__

You can define it in a preprocessor macro, and use it as such:

#define NSFileLog(format, ...) NSLog(@"%s:%d :: " format, __FILE__, __LINE__, ##__VA_ARGS__)

...

NSFileLog(@"Test Log");
==> /Developer Projects/Objective-C/Mac/test/test/AppDelegate.m:20 :: Test Log
Glenn Smith
  • 912
  • 1
  • 17
  • 37
  • Thanks for that, one last question, can `__FILE__` be shorten only the filename not the whole path? – K.W. Sep 05 '13 at 04:16
  • 1
    Here's a thread on how you can do just that: http://stackoverflow.com/questions/8487986/file-macro-shows-full-path – Glenn Smith Sep 05 '13 at 04:19