2

how do I print all the methods in a file in order ?Just print once .

I want to print all the methods which is executed. Giving NSLog(@"%s",__func__), only prints the method name. I want to get a log like this :

2018-01-11 15:31:58.592319+0800 Test[9574:769688] -[ViewController viewDidLoad]
2018-01-11 15:31:58.597790+0800 Test[9574:769688] -[ViewController prepareTableView]
2018-01-11 15:31:58.599040+0800 Test[9574:769688] -[ViewController viewWillAppear:]
2018-01-11 15:31:58.805699+0800 Test[9574:769688] -[ViewController viewWillLayoutSubviews]
2018-01-11 15:31:58.805953+0800 Test[9574:769688] -[ViewController viewDidLayoutSubviews]
2018-01-11 15:31:58.806717+0800 Test[9574:769688] -[ReloadTest reloadData]
2018-01-11 15:31:58.808404+0800 Test[9574:769688] -[ReloadTest layoutSubviews]
2018-01-11 15:31:58.808745+0800 Test[9574:769688] -[ViewController viewWillLayoutSubviews]
2018-01-11 15:31:58.808970+0800 Test[9574:769688] -[ViewController viewDidLayoutSubviews]
2018-01-11 15:31:58.809384+0800 Test[9574:769688] -[ReloadTest layoutSubviews]
2018-01-11 15:31:58.814083+0800 Test[9574:769688] -[ViewController viewDidAppear:]
  • 2
    possible duplicate: https://stackoverflow.com/questions/2770307/nslog-the-method-name-with-objective-c-in-iphone – 3vangelos Jan 12 '18 at 07:59
  • 1
    Possible duplicate of [NSLog the method name with objective-C in iPhone](https://stackoverflow.com/questions/2770307/nslog-the-method-name-with-objective-c-in-iphone) – Paul R Jan 12 '18 at 08:01
  • 1
    Possible duplicate: https://stackoverflow.com/questions/969130/how-to-print-out-the-method-name-and-line-number-and-conditionally-disable-nslog – Paul R Jan 12 '18 at 08:02
  • Thanks . And how do I print all the methods in a file in order ?Just print once . –  Jan 12 '18 at 08:15
  • Try use `__PRETTY_FUNCTION__` instead `__func__` – Alexey Kudlay Jan 16 '18 at 13:26

1 Answers1

0

I'm not sure if this is what you mean (this will log methods called in the order they were called, not all methods in a class):

@interface Backtracer : NSObject
+ (void)logBacktrace;
@end
@implementation Backtracer
+ (void)logBacktrace {
    NSString *stackSymbols = [NSThread.callStackSymbols.reverseObjectEnumerator.allObjects componentsJoinedByString:@"\n"];
    NSMutableArray *methods = [NSMutableArray array];
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[\\+\\-]\\[[^\\]]+\\]" options:(NSRegularExpressionOptions)0 error:nil];
    NSArray *matches = [regex matchesInString:stackSymbols options:(NSMatchingOptions)0 range:NSMakeRange(0, stackSymbols.length)];
    for (NSTextCheckingResult *match in matches) {
        NSString *method = [stackSymbols substringWithRange:match.range];
        if ([method hasSuffix:@" logBacktrace]"]) break;
        [methods addObject:method];
    }
    NSLog(@"backtrace:\n\n%@", [methods componentsJoinedByString:@"\n"]);
}
@end

Using a class like this:

@interface MyClass : NSObject
- (void)step0;
- (void)step1:(int)x;
- (void)step2:(int)x;
@end

@implementation MyClass
- (instancetype)init {
    if (self = [super init]) {
        [self step0];
    }
    return self;
}
- (void)step0 {
    [self step1:1];
}
- (void)step1:(int)x {
    [self step2:(x + 1)];
}
- (void)step2:(int)x {
    //Here's how we got here:
    [Backtracer logBacktrace];
}
@end

Calling this contrived example like so:

(void)[MyClass new];

results in:

... backtrace:

-[MyClass init]
-[MyClass step0]
-[MyClass step1:]
-[MyClass step2:]
Jon
  • 1,469
  • 1
  • 14
  • 23