ANYMethodLog, 在 Objective C 中,记录对象的任何方法调用

分享于 

5分钟阅读

GitHub

  繁體 雙語
log any method call of object in Objective-C
  • 源代码名称:ANYMethodLog
  • 源代码网址:http://www.github.com/qhd/ANYMethodLog
  • ANYMethodLog源代码文档
  • ANYMethodLog源代码下载
  • Git URL:
    git://www.github.com/qhd/ANYMethodLog.git
    Git Clone代码到本地:
    git clone http://www.github.com/qhd/ANYMethodLog
    Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/qhd/ANYMethodLog
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
    
    ANYMethodLog - 记录 objective-c 中对象的任何方法调用

    running show

    调用说明:

    + (void)logMethodWithClass:(Class)aClass
     condition:(BOOL(^)(SEL sel)) condition
     before:(void(^)(id target, SEL sel, NSArray *args, int deep)) before
     after:(void(^)(id target, SEL sel, NSArray *args, NSTimeInterval interval, int deep, id retValue)) after;

    [ANYMethodLog logMethodWithClass:[UIViewController class] condition:^BOOL(SEL sel) {
     NSLog(@"method:%@", NSStringFromSelector(sel));
     returnNO;
    } before:nilafter:nil];
    [ANYMethodLog logMethodWithClass:[UIViewController class] condition:^BOOL(SEL sel) {
     returnYES;
    } before:^(id target, SEL sel, NSArray *args, int deep) {
     NSLog(@"target:%@ sel:%@", target, NSStringFromSelector(sel));
    } after:nil];
    [ANYMethodLog logMethodWithClass:[UIViewController class] condition:^BOOL(SEL sel) {
     NSArray *whiteList = @[@"loadView", @"viewWillAppear:", @"viewDidAppear:", @"viewWillDisappear:", @"viewDidDisappear:", @"viewWillLayoutSubviews", @"viewDidLayoutSubviews"];
     return [whiteList containsObject:NSStringFromSelector(sel)];
    } before:^(id target, SEL sel, NSArray *args, int deep) {
     NSLog(@"target:%@ sel:%@", target, NSStringFromSelector(sel));
    } after:nil];
    [ANYMethodLog logMethodWithClass:NSClassFromString(@"UIViewController") condition:^BOOL(SEL sel) {
     return [NSStringFromSelector(sel) isEqualToString:@"viewWillAppear:"];
    } before:^(id target, SEL sel, NSArray *args, int deep) {
     NSLog(@"before target:%@ sel:%@ args:%@", target, NSStringFromSelector(sel), args);
    } after:nil];
    [ANYMethodLog logMethodWithClass:NSClassFromString(@"ListController") condition:^BOOL(SEL sel) {
     return [NSStringFromSelector(sel) isEqualToString:@"changeBackground"];
    } before:^(id target, SEL sel, NSArray *args, int deep) {
     NSLog(@"before background color:%@", [(ListController *)target view].backgroundColor);
    } after:^(id target, SEL sel, NSArray *args, NSTimeInterval interval, int deep, id retValue) {
     NSLog(@"after background color:%@", [(ListController *)target view].backgroundColor);
    }];
    [ANYMethodLog logMethodWithClass:NSClassFromString(@"ListController") condition:^BOOL(SEL sel) {
     return [NSStringFromSelector(sel) isEqualToString:@"changeBackground"];
    } before:^(id target, SEL sel, NSArray *args, int deep) {
    } after:^(id target, SEL sel, NSArray *args, NSTimeInterval interval, int deep, id retValue) {
     NSLog(@"interval::%@", [@(interval) stringValue]);
    }];
    [ANYMethodLog logMethodWithClass:NSClassFromString(@"ListController") condition:^BOOL(SEL sel) {
     returnYES;
    } before:^(id target, SEL sel, NSArray *args, int deep) {
     NSString *selector = NSStringFromSelector(sel);
     NSArray *selectorArrary = [selector componentsSeparatedByString:@":"];
     selectorArrary = [selectorArrary filteredArrayUsingPredicate:[NSPredicatepredicateWithFormat:@"length> 0"]];
     NSMutableString *selectorString = [NSMutableStringnew];
     for (int i = 0; i <selectorArrary.count; i++) {
     [selectorString appendFormat:@"%@:%@", selectorArrary[i], args[i]];
     }
     NSMutableString *deepString = [NSMutableStringnew];
     for (int i = 0; i <deep; i++) {
     [deepString appendString:@"-"];
     }
     NSLog(@"%@[%@%@]", deepString, target, selectorString);
    } after:^(id target, SEL sel, NSArray *args, NSTimeInterval interval, int deep, id retValue) {
     NSMutableString *deepString = [NSMutableStringnew];
     for (int i = 0; i <deep; i++) {
     [deepString appendString:@"-"];
     }
     NSLog(@"%@ret:%@", deepString, retValue);
    }];

    • 解决真机上运行出现的问题。( 已完成)
    • 打印调用时的参数值。( 已完成)
    • 打印返回值。( 已完成)
    • 计算某个方法的耗时。( 已完成)

    欢迎提问题和请求请求


    对象  log  CAL  objective  调用  method