转: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]);
时间: 2024-10-25 00:04:39