打印方法调用者的信息(方法反向追踪)

转:http://stackoverflow.com/questions/4046833/print-the-name-of-the-calling-function-to-the-debug-log

http://stackoverflow.com/questions/1451342/objective-c-find-caller-of-method

http://ios-blog.co.uk/tutorials/quick-tips/identify-calling-method-in-ios/

#include <execinfo.h>

void *addr[2];
int nframes = backtrace(addr, sizeof(addr)/sizeof(*addr));
if (nframes > 1) {
    char **syms = backtrace_symbols(addr, nframes);
    NSLog(@"%s: caller: %s", __func__, syms[1]);
    free(syms);
} else {
    NSLog(@"%s: *** Failed to generate backtrace.", __func__);
}

NSArray *syms = [NSThread  callStackSymbols]; 
if ([syms count] > 1) { 
    NSLog(@"<%@ %p> %@ - caller: %@ ", [self class], self, NSStringFromSelector(_cmd),[syms objectAtIndex:1]);
} else {
     NSLog(@"<%@ %p> %@", [self class], self, NSStringFromSelector(_cmd)); 
}

 或

NSString *sourceString = [[NSThread callStackSymbols] objectAtIndex:1];
    // Example: 1   UIKit                               0x00540c89 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1163
    NSCharacterSet *separatorSet = [NSCharacterSet characterSetWithCharactersInString:@" -[]+?.,"];
    NSMutableArray *array = [NSMutableArray arrayWithArray:[sourceString  componentsSeparatedByCharactersInSet:separatorSet]];
    [array removeObject:@""];

    NSLog(@"Stack = %@", [array objectAtIndex:0]);
    NSLog(@"Framework = %@", [array objectAtIndex:1]);
    NSLog(@"Memory address = %@", [array objectAtIndex:2]);
    NSLog(@"Class caller = %@", [array objectAtIndex:3]);
    NSLog(@"Function caller = %@", [array objectAtIndex:4]);
    NSLog(@"Line caller = %@", [array objectAtIndex:5]);

 或

    NSString *sourceString = [[NSThread callStackSymbols] objectAtIndex:1];
    NSCharacterSet *separatorSet = [NSCharacterSet characterSetWithCharactersInString:@" -[]+?.,"];
    NSMutableArray *array = [NSMutableArray arrayWithArray:[sourceString  componentsSeparatedByCharactersInSet:separatorSet]];
    [array removeObject:@""];
    NSString *stack          = [array objectAtIndex:0];
    NSString *framework      = [array objectAtIndex:1];
    NSString *memoryAddress  = [array objectAtIndex:2];
    NSString *classCaller    = [array objectAtIndex:3];
    NSString *functionCaller = [array objectAtIndex:4];
    NSString *lineCaller     = [array objectAtIndex:5];
    NSLog(@"[%@ %@]-%@",classCaller,functionCaller,[NSString stringWithUTF8String:__func__]);

#define PrintCaller    NSString *sourceString = [[NSThread callStackSymbols] objectAtIndex:1];
NSCharacterSet *separatorSet = [NSCharacterSet characterSetWithCharactersInString:@" -[]+?.,"];
NSMutableArray *array = [NSMutableArray arrayWithArray:[sourceString  componentsSeparatedByCharactersInSet:separatorSet]];
[array removeObject:@""];
NSString *stack          = [array objectAtIndex:0];
NSString *framework      = [array objectAtIndex:1];
NSString *memoryAddress  = [array objectAtIndex:2];
NSString *classCaller    = [array objectAtIndex:3];
NSString *functionCaller = [array objectAtIndex:4];
NSString *lineCaller     = [array objectAtIndex:5];
NSLog(@"[%@ %@]-%@",classCaller,functionCaller,[NSString stringWithUTF8String:__func__]);


原文地址:https://www.cnblogs.com/ygm900/p/4586725.html