Search code examples
objective-cios-simulatornslog

Show current class file


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.


Solution

  • 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