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

转: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__]);

 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值