├── .gitignore ├── ANYMethodLog ├── ANYMethodLog.h └── ANYMethodLog.m ├── ANYMethodLogDemo ├── ANYMethodLogDemo.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ └── contents.xcworkspacedata ├── ANYMethodLogDemo │ ├── AppDelegate.h │ ├── AppDelegate.m │ ├── Assets.xcassets │ │ └── AppIcon.appiconset │ │ │ └── Contents.json │ ├── Base.lproj │ │ ├── LaunchScreen.storyboard │ │ └── Main.storyboard │ ├── DetailController.h │ ├── DetailController.m │ ├── DetailController.xib │ ├── HomeController.h │ ├── HomeController.m │ ├── Info.plist │ ├── ListController.h │ ├── ListController.m │ ├── ListController.xib │ └── main.m └── ANYMethodLogDemoTests │ ├── ANYMethodLogDemoTests.m │ ├── ClassMethodTests.m │ └── Info.plist ├── Documentation └── Images │ └── running_show.gif ├── LICENSE ├── README.md └── readme-en.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xcuserstate 23 | 24 | ## Obj-C/Swift specific 25 | *.hmap 26 | *.ipa 27 | *.dSYM.zip 28 | *.dSYM 29 | 30 | # CocoaPods 31 | # 32 | # We recommend against adding the Pods directory to your .gitignore. However 33 | # you should judge for yourself, the pros and cons are mentioned at: 34 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 35 | # 36 | # Pods/ 37 | 38 | # Carthage 39 | # 40 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 41 | # Carthage/Checkouts 42 | 43 | Carthage/Build 44 | 45 | # fastlane 46 | # 47 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 48 | # screenshots whenever they are needed. 49 | # For more information about the recommended setup visit: 50 | # https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Gitignore.md 51 | 52 | fastlane/report.xml 53 | fastlane/screenshots 54 | 55 | #Code Injection 56 | # 57 | # After new code Injection tools there's a generated folder /iOSInjectionProject 58 | # https://github.com/johnno1962/injectionforxcode 59 | 60 | iOSInjectionProject/ 61 | -------------------------------------------------------------------------------- /ANYMethodLog/ANYMethodLog.h: -------------------------------------------------------------------------------- 1 | // 2 | // ANYMethodLog.h 3 | // ANYMethodLog 4 | // 5 | // Created by qiuhaodong on 2017/1/14. 6 | // Copyright © 2017年 qiuhaodong. All rights reserved. 7 | // 8 | // https://github.com/qhd/ANYMethodLog.git 9 | // 10 | 11 | #import 12 | 13 | typedef BOOL (^ConditionBlock)(SEL sel); 14 | typedef void (^BeforeBlock)(id target, SEL sel, NSArray *args, int deep); 15 | typedef void (^AfterBlock)(id target, SEL sel, NSArray *args, NSTimeInterval interval, int deep, id retValue); 16 | 17 | @interface ANYMethodLog : NSObject 18 | 19 | /** 20 | 打印对象的方法调用 21 | 22 | @param aClass 要打印的类 23 | @param condition 根据此 block 来决定是否追踪方法(sel 是方法名) 24 | @param before 方法调用前会调用该 block(target 是检测的对象,sel 是方法名,args 是参数列表,deep 是调用层级) 25 | @param after 方法调用后会调用该 block(interval 是执行方法的耗时,retValue 是返回值) 26 | */ 27 | + (void)logMethodWithClass:(Class)aClass 28 | condition:(ConditionBlock) condition 29 | before:(BeforeBlock) before 30 | after:(AfterBlock) after; 31 | 32 | @end 33 | -------------------------------------------------------------------------------- /ANYMethodLog/ANYMethodLog.m: -------------------------------------------------------------------------------- 1 | // 2 | // ANYMethodLog.m 3 | // ANYMethodLog 4 | // 5 | // Created by qiuhaodong on 2017/1/14. 6 | // Copyright © 2017年 qiuhaodong. All rights reserved. 7 | // 8 | // https://github.com/qhd/ANYMethodLog.git 9 | // 10 | 11 | #import "ANYMethodLog.h" 12 | #import 13 | #import 14 | #import 15 | 16 | #pragma mark - deep 17 | 18 | //调用层次 19 | static int deep = -1; 20 | 21 | #pragma mark - Func Define 22 | 23 | BOOL qhd_isInBlackList(NSString *methodName); 24 | NSDictionary *qhd_canHandleTypeDic(void); 25 | BOOL qhd_isCanHandle(NSString *typeEncode); 26 | SEL qhd_createNewSelector(SEL originalSelector); 27 | BOOL qhd_isStructType(const char *argumentType); 28 | NSString *qhd_structName(const char *argumentType); 29 | BOOL isCGRect (const char *type); 30 | BOOL isCGPoint (const char *type); 31 | BOOL isCGSize (const char *type); 32 | BOOL isCGVector (const char *type); 33 | BOOL isUIOffset (const char *type); 34 | BOOL isUIEdgeInsets (const char *type); 35 | BOOL isCGAffineTransform(const char *type); 36 | BOOL qhd_isCanHook(Method method, const char *returnType); 37 | id getReturnValue(NSInvocation *invocation); 38 | NSArray *qhd_method_arguments(NSInvocation *invocation); 39 | void qhd_forwardInvocation(id target, SEL selector, NSInvocation *invocation); 40 | BOOL qhd_replaceMethod(Class cls, SEL originSelector, char *returnType); 41 | void qhd_logMethod(Class aClass, BOOL(^condition)(SEL sel)); 42 | 43 | #pragma mark - AMLBlock 44 | 45 | @interface AMLBlock : NSObject 46 | 47 | @property (strong, nonatomic) NSString *targetClassName; 48 | @property (copy, nonatomic) ConditionBlock condition; 49 | @property (copy, nonatomic) BeforeBlock before; 50 | @property (copy, nonatomic) AfterBlock after; 51 | 52 | @end 53 | 54 | @implementation AMLBlock 55 | 56 | - (BOOL)runCondition:(SEL)sel { 57 | if (self.condition) { 58 | return self.condition(sel); 59 | } else { 60 | return YES; 61 | } 62 | } 63 | 64 | - (void)rundBefore:(id)target sel:(SEL)sel args:(NSArray *)args deep:(int) deep { 65 | if (self.before) { 66 | self.before(target, sel, args, deep); 67 | } 68 | } 69 | 70 | - (void)rundAfter:(id)target sel:(SEL)sel args:(NSArray *)args interval:(NSTimeInterval)interval deep:(int)deep retValue:(id)retValue{ 71 | if (self.after) { 72 | self.after(target, sel, args, interval, deep, retValue); 73 | } 74 | } 75 | 76 | @end 77 | 78 | 79 | #pragma mark - ANYMethodLog private interface 80 | 81 | @interface ANYMethodLog() 82 | 83 | @property (strong, nonatomic) NSMutableDictionary *blockCache; 84 | 85 | + (instancetype)sharedANYMethodLog; 86 | 87 | - (void)setAMLBlock:(AMLBlock *)block forKey:(NSString *)aKey; 88 | 89 | - (AMLBlock *)blockWithTarget:(id)target; 90 | 91 | @end 92 | 93 | 94 | #pragma mark - C function 95 | 96 | #define SHARED_ANYMETHODLOG [ANYMethodLog sharedANYMethodLog] 97 | 98 | //#define OPEN_TARGET_LOG 99 | 100 | #ifdef OPEN_TARGET_LOG 101 | #define TARGET_LOG(format, ...) NSLog(format, ## __VA_ARGS__) 102 | #else 103 | #define TARGET_LOG(format, ...) 104 | #endif 105 | 106 | 107 | //#define OPEN_DEV_LOG 108 | 109 | #ifdef OPEN_DEV_LOG 110 | #define DEV_LOG(format, ...) NSLog(format, ## __VA_ARGS__) 111 | #else 112 | #define DEV_LOG(format, ...) 113 | #endif 114 | 115 | //是否在默认的黑名单中 116 | BOOL qhd_isInBlackList(NSString *methodName) { 117 | static NSArray *defaultBlackList = nil; 118 | static dispatch_once_t onceToken; 119 | dispatch_once(&onceToken, ^{ 120 | defaultBlackList = @[/*UIViewController的:*/@".cxx_destruct", @"dealloc", @"_isDeallocating", @"release", @"autorelease", @"retain", @"Retain", @"_tryRetain", @"copy", /*UIView的:*/ @"nsis_descriptionOfVariable:", /*NSObject的:*/@"respondsToSelector:", @"class", @"methodSignatureForSelector:", @"allowsWeakReference", @"retainWeakReference", @"init", @"forwardInvocation:"]; 121 | }); 122 | return ([defaultBlackList containsObject:methodName]); 123 | } 124 | 125 | /*reference: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html#//apple_ref/doc/uid/TP40008048-CH100-SW1 126 | 经实践发现与文档有差别 127 | 1.在64位时@encode(long)跟@encode(long long)的值一样; 128 | 2.在64位时@encode(unsigned long)跟@encode(unsigned long long)的值一样; 129 | 3.在32位时@encode(BOOL)跟@encode(char)一样。 130 | +--------------------+-----------+-----------+ 131 | | type |code(32bit)|code(64bit)| 132 | |--------------------|-----------|-----------| 133 | | BOOL | c | B | 134 | |--------------------|-----------|-----------| 135 | | char | c | c | 136 | |--------------------|-----------|-----------| 137 | | long | l | q | 138 | |--------------------|-----------|-----------| 139 | | long long | q | q | 140 | |--------------------|-----------|-----------| 141 | | unsigned long | L | Q | 142 | |--------------------|-----------|-----------| 143 | | unsigned long long | Q | Q | 144 | +--------------------+-----------+-----------+ 145 | */ 146 | NSDictionary *qhd_canHandleTypeDic() { 147 | static NSDictionary *dic = nil; 148 | static dispatch_once_t onceToken; 149 | dispatch_once(&onceToken, ^{ 150 | dic = @{[NSString stringWithUTF8String:@encode(char)] : @"(char)", 151 | [NSString stringWithUTF8String:@encode(int)] : @"(int)", 152 | [NSString stringWithUTF8String:@encode(short)] : @"(short)", 153 | [NSString stringWithUTF8String:@encode(long)] : @"(long)", 154 | [NSString stringWithUTF8String:@encode(long long)] : @"(long long)", 155 | [NSString stringWithUTF8String:@encode(unsigned char)] : @"(unsigned char))", 156 | [NSString stringWithUTF8String:@encode(unsigned int)] : @"(unsigned int)", 157 | [NSString stringWithUTF8String:@encode(unsigned short)] : @"(unsigned short)", 158 | [NSString stringWithUTF8String:@encode(unsigned long)] : @"(unsigned long)", 159 | [NSString stringWithUTF8String:@encode(unsigned long long)] : @"(unsigned long long)", 160 | [NSString stringWithUTF8String:@encode(float)] : @"(float)", 161 | [NSString stringWithUTF8String:@encode(double)] : @"(double)", 162 | [NSString stringWithUTF8String:@encode(BOOL)] : @"(BOOL)", 163 | [NSString stringWithUTF8String:@encode(void)] : @"(void)", 164 | [NSString stringWithUTF8String:@encode(char *)] : @"(char *)", 165 | [NSString stringWithUTF8String:@encode(id)] : @"(id)", 166 | [NSString stringWithUTF8String:@encode(Class)] : @"(Class)", 167 | [NSString stringWithUTF8String:@encode(SEL)] : @"(SEL)", 168 | [NSString stringWithUTF8String:@encode(CGRect)] : @"(CGRect)", 169 | [NSString stringWithUTF8String:@encode(CGPoint)] : @"(CGPoint)", 170 | [NSString stringWithUTF8String:@encode(CGSize)] : @"(CGSize)", 171 | [NSString stringWithUTF8String:@encode(CGVector)] : @"(CGVector)", 172 | [NSString stringWithUTF8String:@encode(CGAffineTransform)] : @"(CGAffineTransform)", 173 | [NSString stringWithUTF8String:@encode(UIOffset)] : @"(UIOffset)", 174 | [NSString stringWithUTF8String:@encode(UIEdgeInsets)] : @"(UIEdgeInsets)", 175 | @"o^@" : @"(id*)", 176 | @"@?":@"(block)" // block类型 177 | };//TODO:添加其他类型 178 | 179 | DEV_LOG(@"arg types:%@", dic); 180 | }); 181 | return dic; 182 | } 183 | 184 | //根据定义的类型的判断是否能处理 185 | BOOL qhd_isCanHandle(NSString *typeEncode) { 186 | return [qhd_canHandleTypeDic().allKeys containsObject:typeEncode]; 187 | } 188 | 189 | //创建一个新的selector 190 | SEL qhd_createNewSelector(SEL originalSelector) { 191 | NSString *oldSelectorName = NSStringFromSelector(originalSelector); 192 | NSString *newSelectorName = [NSString stringWithFormat:@"qhd_%@", oldSelectorName]; 193 | SEL newSelector = NSSelectorFromString(newSelectorName); 194 | return newSelector; 195 | } 196 | 197 | //是否struct类型 198 | BOOL qhd_isStructType(const char *argumentType) { 199 | NSString *typeString = [NSString stringWithUTF8String:argumentType]; 200 | return ([typeString hasPrefix:@"{"] && [typeString hasSuffix:@"}"]); 201 | } 202 | 203 | //struct类型名 204 | NSString *qhd_structName(const char *argumentType) { 205 | NSString *typeString = [NSString stringWithUTF8String:argumentType]; 206 | NSUInteger start = [typeString rangeOfString:@"{"].location; 207 | NSUInteger end = [typeString rangeOfString:@"="].location; 208 | if (end > start) { 209 | return [typeString substringWithRange:NSMakeRange(start + 1, end - start - 1)]; 210 | } else { 211 | return nil; 212 | } 213 | } 214 | 215 | BOOL isCGRect (const char *type) {return [qhd_structName(type) isEqualToString:@"CGRect"];} 216 | BOOL isCGPoint (const char *type) {return [qhd_structName(type) isEqualToString:@"CGPoint"];} 217 | BOOL isCGSize (const char *type) {return [qhd_structName(type) isEqualToString:@"CGSize"];} 218 | BOOL isCGVector (const char *type) {return [qhd_structName(type) isEqualToString:@"CGVector"];} 219 | BOOL isUIOffset (const char *type) {return [qhd_structName(type) isEqualToString:@"UIOffset"];} 220 | BOOL isUIEdgeInsets (const char *type) {return [qhd_structName(type) isEqualToString:@"UIEdgeInsets"];} 221 | BOOL isCGAffineTransform(const char *type) {return [qhd_structName(type) isEqualToString:@"CGAffineTransform"];} 222 | 223 | //检查是否能处理 224 | BOOL qhd_isCanHook(Method method, const char *returnType) { 225 | 226 | //若在黑名单中则不处理 227 | NSString *selectorName = NSStringFromSelector(method_getName(method)); 228 | if (qhd_isInBlackList(selectorName)) { 229 | return NO; 230 | } 231 | 232 | if ([selectorName rangeOfString:@"qhd_"].location != NSNotFound) { 233 | return NO; 234 | } 235 | 236 | NSString *returnTypeString = [NSString stringWithUTF8String:returnType]; 237 | 238 | BOOL isCanHook = YES; 239 | if (!qhd_isCanHandle(returnTypeString)) { 240 | isCanHook = NO; 241 | } 242 | for(int k = 2 ; k < method_getNumberOfArguments(method); k ++) { 243 | char argument[250]; 244 | memset(argument, 0, sizeof(argument)); 245 | method_getArgumentType(method, k, argument, sizeof(argument)); 246 | NSString *argumentString = [NSString stringWithUTF8String:argument]; 247 | if (!qhd_isCanHandle(argumentString)) { 248 | DEV_LOG(@"unknow arg type:%@", argumentString); 249 | isCanHook = NO; 250 | break; 251 | } 252 | } 253 | return isCanHook; 254 | } 255 | 256 | //获取方法返回值 257 | id getReturnValue(NSInvocation *invocation){ 258 | const char *returnType = invocation.methodSignature.methodReturnType; 259 | if (returnType[0] == 'r') { 260 | returnType++; 261 | } 262 | #define WRAP_GET_VALUE(type) \ 263 | do { \ 264 | type val = 0; \ 265 | [invocation getReturnValue:&val]; \ 266 | return @(val); \ 267 | } while (0) 268 | if (strcmp(returnType, @encode(id)) == 0 || strcmp(returnType, @encode(Class)) == 0 || strcmp(returnType, @encode(void (^)(void))) == 0) { 269 | __autoreleasing id returnObj; 270 | [invocation getReturnValue:&returnObj]; 271 | return returnObj; 272 | } else if (strcmp(returnType, @encode(char)) == 0) { 273 | WRAP_GET_VALUE(char); 274 | } else if (strcmp(returnType, @encode(int)) == 0) { 275 | WRAP_GET_VALUE(int); 276 | } else if (strcmp(returnType, @encode(short)) == 0) { 277 | WRAP_GET_VALUE(short); 278 | } else if (strcmp(returnType, @encode(long)) == 0) { 279 | WRAP_GET_VALUE(long); 280 | } else if (strcmp(returnType, @encode(long long)) == 0) { 281 | WRAP_GET_VALUE(long long); 282 | } else if (strcmp(returnType, @encode(unsigned char)) == 0) { 283 | WRAP_GET_VALUE(unsigned char); 284 | } else if (strcmp(returnType, @encode(unsigned int)) == 0) { 285 | WRAP_GET_VALUE(unsigned int); 286 | } else if (strcmp(returnType, @encode(unsigned short)) == 0) { 287 | WRAP_GET_VALUE(unsigned short); 288 | } else if (strcmp(returnType, @encode(unsigned long)) == 0) { 289 | WRAP_GET_VALUE(unsigned long); 290 | } else if (strcmp(returnType, @encode(unsigned long long)) == 0) { 291 | WRAP_GET_VALUE(unsigned long long); 292 | } else if (strcmp(returnType, @encode(float)) == 0) { 293 | WRAP_GET_VALUE(float); 294 | } else if (strcmp(returnType, @encode(double)) == 0) { 295 | WRAP_GET_VALUE(double); 296 | } else if (strcmp(returnType, @encode(BOOL)) == 0) { 297 | WRAP_GET_VALUE(BOOL); 298 | } else if (strcmp(returnType, @encode(char *)) == 0) { 299 | WRAP_GET_VALUE(const char *); 300 | } else if (strcmp(returnType, @encode(void)) == 0) { 301 | return @"void"; 302 | } else { 303 | NSUInteger valueSize = 0; 304 | NSGetSizeAndAlignment(returnType, &valueSize, NULL); 305 | unsigned char valueBytes[valueSize]; 306 | [invocation getReturnValue:valueBytes]; 307 | 308 | return [NSValue valueWithBytes:valueBytes objCType:returnType]; 309 | } 310 | return nil; 311 | } 312 | 313 | //获取方法参数 314 | NSArray *qhd_method_arguments(NSInvocation *invocation) { 315 | NSMethodSignature *methodSignature = [invocation methodSignature]; 316 | NSMutableArray *argList = (methodSignature.numberOfArguments > 2 ? [NSMutableArray array] : nil); 317 | for (NSUInteger i = 2; i < methodSignature.numberOfArguments; i++) { 318 | const char *argumentType = [methodSignature getArgumentTypeAtIndex:i]; 319 | id arg = nil; 320 | 321 | if (qhd_isStructType(argumentType)) { 322 | #define GET_STRUCT_ARGUMENT(_type)\ 323 | if (is##_type(argumentType)) {\ 324 | _type arg_temp;\ 325 | [invocation getArgument:&arg_temp atIndex:i];\ 326 | arg = NSStringFrom##_type(arg_temp);\ 327 | } 328 | GET_STRUCT_ARGUMENT(CGRect) 329 | else GET_STRUCT_ARGUMENT(CGPoint) 330 | else GET_STRUCT_ARGUMENT(CGSize) 331 | else GET_STRUCT_ARGUMENT(CGVector) 332 | else GET_STRUCT_ARGUMENT(UIOffset) 333 | else GET_STRUCT_ARGUMENT(UIEdgeInsets) 334 | else GET_STRUCT_ARGUMENT(CGAffineTransform) 335 | 336 | if (arg == nil) { 337 | arg = @"{unknown}"; 338 | } 339 | } 340 | #define GET_ARGUMENT(_type)\ 341 | if (0 == strcmp(argumentType, @encode(_type))) {\ 342 | _type arg_temp;\ 343 | [invocation getArgument:&arg_temp atIndex:i];\ 344 | arg = @(arg_temp);\ 345 | } 346 | else GET_ARGUMENT(char) 347 | else GET_ARGUMENT(int) 348 | else GET_ARGUMENT(short) 349 | else GET_ARGUMENT(long) 350 | else GET_ARGUMENT(long long) 351 | else GET_ARGUMENT(unsigned char) 352 | else GET_ARGUMENT(unsigned int) 353 | else GET_ARGUMENT(unsigned short) 354 | else GET_ARGUMENT(unsigned long) 355 | else GET_ARGUMENT(unsigned long long) 356 | else GET_ARGUMENT(float) 357 | else GET_ARGUMENT(double) 358 | else GET_ARGUMENT(BOOL) 359 | else if (0 == strcmp(argumentType, @encode(id))) { 360 | __unsafe_unretained id arg_temp; 361 | [invocation getArgument:&arg_temp atIndex:i]; 362 | arg = arg_temp; 363 | if (!arg) { 364 | arg = @"nil"; 365 | } 366 | } 367 | else if (0 == strcmp(argumentType, "o^@")) { 368 | void ** arg_temp; 369 | [invocation getArgument:&arg_temp atIndex:i]; 370 | arg = (__bridge id)(*arg_temp); 371 | if (!arg) { 372 | arg = @"*nil"; 373 | } 374 | } 375 | else if (0 == strcmp(argumentType, @encode(SEL))) { 376 | SEL arg_temp; 377 | [invocation getArgument:&arg_temp atIndex:i]; 378 | arg = NSStringFromSelector(arg_temp); 379 | } 380 | else if (0 == strcmp(argumentType, @encode(char *))) { 381 | char *arg_temp; 382 | [invocation getArgument:&arg_temp atIndex:i]; 383 | arg = [NSString stringWithUTF8String:arg_temp]; 384 | } 385 | else if (0 == strcmp(argumentType, @encode(void *))) { 386 | void *arg_temp; 387 | [invocation getArgument:&arg_temp atIndex:i]; 388 | arg = (__bridge id _Nonnull)arg_temp; 389 | } 390 | else if (0 == strcmp(argumentType, @encode(Class))) { 391 | Class arg_temp; 392 | [invocation getArgument:&arg_temp atIndex:i]; 393 | arg = arg_temp; 394 | } 395 | 396 | if (!arg) { 397 | arg = [NSString stringWithFormat:@"%s:unknown", argumentType]; 398 | } 399 | [argList addObject:arg]; 400 | } 401 | return argList; 402 | } 403 | 404 | //forwardInvocation:方法的新IMP 405 | void qhd_forwardInvocation(id target, SEL selector, NSInvocation *invocation) { 406 | NSArray *argList = qhd_method_arguments(invocation); 407 | 408 | SEL originSelector = invocation.selector; 409 | 410 | NSString *originSelectorString = NSStringFromSelector(originSelector); 411 | 412 | //友盟的UMAOCTools会产生问题 413 | if ([originSelectorString rangeOfString:@"hook_"].location != NSNotFound) { 414 | return; 415 | } 416 | 417 | [invocation setSelector:qhd_createNewSelector(originSelector)]; 418 | [invocation setTarget:target]; 419 | 420 | deep++; 421 | 422 | AMLBlock *block = [SHARED_ANYMETHODLOG blockWithTarget:target]; 423 | [block rundBefore:target sel:originSelector args:argList deep:deep]; 424 | 425 | NSDate *start = [NSDate date]; 426 | 427 | [invocation invoke]; 428 | 429 | NSDate *end = [NSDate date]; 430 | NSTimeInterval interval = [end timeIntervalSinceDate:start]; 431 | 432 | [block rundAfter:target sel:originSelector args:argList interval:interval deep:deep retValue:getReturnValue(invocation)]; 433 | 434 | deep--; 435 | } 436 | 437 | //替换方法 438 | BOOL qhd_replaceMethod(Class cls, SEL originSelector, char *returnType) { 439 | Method originMethod = class_getInstanceMethod(cls, originSelector); 440 | if (originMethod == nil) { 441 | return NO; 442 | } 443 | const char *originTypes = method_getTypeEncoding(originMethod); 444 | IMP msgForwardIMP = _objc_msgForward; 445 | #if !defined(__arm64__) 446 | if (qhd_isStructType(returnType)) { 447 | //Reference JSPatch: 448 | //In some cases that returns struct, we should use the '_stret' API: 449 | //http://sealiesoftware.com/blog/archive/2008/10/30/objc_explain_objc_msgSend_stret.html 450 | //NSMethodSignature knows the detail but has no API to return, we can only get the info from debugDescription. 451 | NSMethodSignature *methodSignature = [NSMethodSignature signatureWithObjCTypes:originTypes]; 452 | if ([methodSignature.debugDescription rangeOfString:@"is special struct return? YES"].location != NSNotFound) { 453 | msgForwardIMP = (IMP)_objc_msgForward_stret; 454 | } 455 | } 456 | #endif 457 | 458 | IMP originIMP = method_getImplementation(originMethod); 459 | 460 | if (originIMP == nil || originIMP == msgForwardIMP) { 461 | return NO; 462 | } 463 | 464 | //把原方法的IMP换成_objc_msgForward,使之触发forwardInvocation方法 465 | class_replaceMethod(cls, originSelector, msgForwardIMP, originTypes); 466 | 467 | //把方法forwardInvocation的IMP换成qhd_forwardInvocation 468 | class_replaceMethod(cls, @selector(forwardInvocation:), (IMP)qhd_forwardInvocation, "v@:@"); 469 | 470 | //创建一个新方法,IMP就是原方法的原来的IMP,那么只要在qhd_forwardInvocation调用新方法即可 471 | SEL newSelecotr = qhd_createNewSelector(originSelector); 472 | BOOL isAdd = class_addMethod(cls, newSelecotr, originIMP, originTypes); 473 | if (!isAdd) { 474 | DEV_LOG(@"class_addMethod fail"); 475 | } 476 | 477 | return YES; 478 | } 479 | 480 | void qhd_logMethod(Class aClass, BOOL(^condition)(SEL sel)) { 481 | unsigned int outCount; 482 | Method *methods = class_copyMethodList(aClass,&outCount); 483 | 484 | for (int i = 0; i < outCount; i ++) { 485 | Method tempMethod = *(methods + i); 486 | SEL selector = method_getName(tempMethod); 487 | char *returnType = method_copyReturnType(tempMethod); 488 | 489 | BOOL isCan = qhd_isCanHook(tempMethod, returnType); 490 | 491 | if (isCan && condition) { 492 | isCan = condition(selector); 493 | } 494 | 495 | if (isCan) { 496 | if (qhd_replaceMethod(aClass, selector, returnType)) { 497 | DEV_LOG(@"success hook method:%@ types:%s", NSStringFromSelector(selector), method_getDescription(tempMethod)->types); 498 | } else { 499 | DEV_LOG(@"fail method:%@ types:%s", NSStringFromSelector(selector), method_getDescription(tempMethod)->types); 500 | } 501 | } else { 502 | DEV_LOG(@"can not hook method:%@ types:%s", NSStringFromSelector(selector), method_getDescription(tempMethod)->types); 503 | } 504 | free(returnType); 505 | } 506 | free(methods); 507 | } 508 | 509 | 510 | #pragma mark - ANYMethodLog implementation 511 | 512 | @implementation ANYMethodLog 513 | 514 | + (void)logMethodWithClass:(Class)aClass 515 | condition:(ConditionBlock) condition 516 | before:(BeforeBlock) before 517 | after:(AfterBlock) after { 518 | #ifndef DEBUG 519 | return; 520 | #endif 521 | 522 | if (aClass) { 523 | AMLBlock *block = [[AMLBlock alloc] init]; 524 | block.targetClassName = NSStringFromClass(aClass); 525 | block.condition = condition; 526 | block.before = before; 527 | block.after = after; 528 | [SHARED_ANYMETHODLOG setAMLBlock:block forKey:block.targetClassName]; 529 | } 530 | 531 | qhd_logMethod(aClass, condition); 532 | 533 | //获取元类,处理类方法。(注意获取元类是用object_getClass,而不是class_getSuperclass) 534 | Class metaClass = object_getClass(aClass); 535 | qhd_logMethod(metaClass, condition); 536 | } 537 | 538 | + (instancetype)sharedANYMethodLog { 539 | static ANYMethodLog *_sharedANYMethodLog = nil; 540 | static dispatch_once_t onceToken; 541 | dispatch_once(&onceToken, ^{ 542 | _sharedANYMethodLog = [[self alloc] init]; 543 | _sharedANYMethodLog.blockCache = [NSMutableDictionary dictionary]; 544 | }); 545 | return _sharedANYMethodLog; 546 | } 547 | 548 | - (void)setAMLBlock:(AMLBlock *)block forKey:(NSString *)aKey { 549 | @synchronized (self) { 550 | [self.blockCache setObject:block forKey:aKey]; 551 | } 552 | } 553 | 554 | - (AMLBlock *)blockWithTarget:(id)target { 555 | Class class = [target class]; 556 | AMLBlock *block = [self.blockCache objectForKey:NSStringFromClass(class)]; 557 | while (block == nil) { 558 | class = [class superclass]; 559 | if (class == nil) { 560 | break; 561 | } 562 | block = [self.blockCache objectForKey:NSStringFromClass(class)]; 563 | } 564 | return block; 565 | } 566 | 567 | @end 568 | -------------------------------------------------------------------------------- /ANYMethodLogDemo/ANYMethodLogDemo.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | A3C931981E2F621900C25FEC /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = A3C931971E2F621900C25FEC /* main.m */; }; 11 | A3C9319B1E2F621900C25FEC /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = A3C9319A1E2F621900C25FEC /* AppDelegate.m */; }; 12 | A3C931A11E2F621900C25FEC /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A3C9319F1E2F621900C25FEC /* Main.storyboard */; }; 13 | A3C931A31E2F621900C25FEC /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A3C931A21E2F621900C25FEC /* Assets.xcassets */; }; 14 | A3C931A61E2F621900C25FEC /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A3C931A41E2F621900C25FEC /* LaunchScreen.storyboard */; }; 15 | A3C931B11E2F621900C25FEC /* ANYMethodLogDemoTests.m in Sources */ = {isa = PBXBuildFile; fileRef = A3C931B01E2F621900C25FEC /* ANYMethodLogDemoTests.m */; }; 16 | A3C931BE1E2F625900C25FEC /* ANYMethodLog.m in Sources */ = {isa = PBXBuildFile; fileRef = A3C931BD1E2F625900C25FEC /* ANYMethodLog.m */; }; 17 | A3C931C21E2F64DA00C25FEC /* HomeController.m in Sources */ = {isa = PBXBuildFile; fileRef = A3C931C01E2F64DA00C25FEC /* HomeController.m */; }; 18 | A3C931C71E2F651F00C25FEC /* ListController.m in Sources */ = {isa = PBXBuildFile; fileRef = A3C931C51E2F651F00C25FEC /* ListController.m */; }; 19 | A3C931C81E2F651F00C25FEC /* ListController.xib in Resources */ = {isa = PBXBuildFile; fileRef = A3C931C61E2F651F00C25FEC /* ListController.xib */; }; 20 | A3C931CC1E2F652E00C25FEC /* DetailController.m in Sources */ = {isa = PBXBuildFile; fileRef = A3C931CA1E2F652E00C25FEC /* DetailController.m */; }; 21 | A3C931CD1E2F652E00C25FEC /* DetailController.xib in Resources */ = {isa = PBXBuildFile; fileRef = A3C931CB1E2F652E00C25FEC /* DetailController.xib */; }; 22 | A3FBEA411E35FD2E000F10FA /* ClassMethodTests.m in Sources */ = {isa = PBXBuildFile; fileRef = A3FBEA401E35FD2E000F10FA /* ClassMethodTests.m */; }; 23 | /* End PBXBuildFile section */ 24 | 25 | /* Begin PBXContainerItemProxy section */ 26 | A3C931AD1E2F621900C25FEC /* PBXContainerItemProxy */ = { 27 | isa = PBXContainerItemProxy; 28 | containerPortal = A3C9318B1E2F621900C25FEC /* Project object */; 29 | proxyType = 1; 30 | remoteGlobalIDString = A3C931921E2F621900C25FEC; 31 | remoteInfo = ANYMethodLogDemo; 32 | }; 33 | /* End PBXContainerItemProxy section */ 34 | 35 | /* Begin PBXFileReference section */ 36 | A3C931931E2F621900C25FEC /* ANYMethodLogDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ANYMethodLogDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 37 | A3C931971E2F621900C25FEC /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 38 | A3C931991E2F621900C25FEC /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 39 | A3C9319A1E2F621900C25FEC /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 40 | A3C931A01E2F621900C25FEC /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 41 | A3C931A21E2F621900C25FEC /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 42 | A3C931A51E2F621900C25FEC /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 43 | A3C931A71E2F621900C25FEC /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 44 | A3C931AC1E2F621900C25FEC /* ANYMethodLogDemoTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = ANYMethodLogDemoTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 45 | A3C931B01E2F621900C25FEC /* ANYMethodLogDemoTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ANYMethodLogDemoTests.m; sourceTree = ""; }; 46 | A3C931B21E2F621900C25FEC /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 47 | A3C931BC1E2F625900C25FEC /* ANYMethodLog.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ANYMethodLog.h; sourceTree = ""; }; 48 | A3C931BD1E2F625900C25FEC /* ANYMethodLog.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ANYMethodLog.m; sourceTree = ""; }; 49 | A3C931BF1E2F64DA00C25FEC /* HomeController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HomeController.h; sourceTree = ""; }; 50 | A3C931C01E2F64DA00C25FEC /* HomeController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HomeController.m; sourceTree = ""; }; 51 | A3C931C41E2F651F00C25FEC /* ListController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ListController.h; sourceTree = ""; }; 52 | A3C931C51E2F651F00C25FEC /* ListController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ListController.m; sourceTree = ""; }; 53 | A3C931C61E2F651F00C25FEC /* ListController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = ListController.xib; sourceTree = ""; }; 54 | A3C931C91E2F652E00C25FEC /* DetailController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DetailController.h; sourceTree = ""; }; 55 | A3C931CA1E2F652E00C25FEC /* DetailController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DetailController.m; sourceTree = ""; }; 56 | A3C931CB1E2F652E00C25FEC /* DetailController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = DetailController.xib; sourceTree = ""; }; 57 | A3FBEA401E35FD2E000F10FA /* ClassMethodTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ClassMethodTests.m; sourceTree = ""; }; 58 | /* End PBXFileReference section */ 59 | 60 | /* Begin PBXFrameworksBuildPhase section */ 61 | A3C931901E2F621900C25FEC /* Frameworks */ = { 62 | isa = PBXFrameworksBuildPhase; 63 | buildActionMask = 2147483647; 64 | files = ( 65 | ); 66 | runOnlyForDeploymentPostprocessing = 0; 67 | }; 68 | A3C931A91E2F621900C25FEC /* Frameworks */ = { 69 | isa = PBXFrameworksBuildPhase; 70 | buildActionMask = 2147483647; 71 | files = ( 72 | ); 73 | runOnlyForDeploymentPostprocessing = 0; 74 | }; 75 | /* End PBXFrameworksBuildPhase section */ 76 | 77 | /* Begin PBXGroup section */ 78 | A3C9318A1E2F621900C25FEC = { 79 | isa = PBXGroup; 80 | children = ( 81 | A3C931BB1E2F625900C25FEC /* ANYMethodLog */, 82 | A3C931951E2F621900C25FEC /* ANYMethodLogDemo */, 83 | A3C931AF1E2F621900C25FEC /* ANYMethodLogDemoTests */, 84 | A3C931941E2F621900C25FEC /* Products */, 85 | ); 86 | sourceTree = ""; 87 | }; 88 | A3C931941E2F621900C25FEC /* Products */ = { 89 | isa = PBXGroup; 90 | children = ( 91 | A3C931931E2F621900C25FEC /* ANYMethodLogDemo.app */, 92 | A3C931AC1E2F621900C25FEC /* ANYMethodLogDemoTests.xctest */, 93 | ); 94 | name = Products; 95 | sourceTree = ""; 96 | }; 97 | A3C931951E2F621900C25FEC /* ANYMethodLogDemo */ = { 98 | isa = PBXGroup; 99 | children = ( 100 | A3C931991E2F621900C25FEC /* AppDelegate.h */, 101 | A3C9319A1E2F621900C25FEC /* AppDelegate.m */, 102 | A3C931BF1E2F64DA00C25FEC /* HomeController.h */, 103 | A3C931C01E2F64DA00C25FEC /* HomeController.m */, 104 | A3C931C41E2F651F00C25FEC /* ListController.h */, 105 | A3C931C51E2F651F00C25FEC /* ListController.m */, 106 | A3C931C61E2F651F00C25FEC /* ListController.xib */, 107 | A3C931C91E2F652E00C25FEC /* DetailController.h */, 108 | A3C931CA1E2F652E00C25FEC /* DetailController.m */, 109 | A3C931CB1E2F652E00C25FEC /* DetailController.xib */, 110 | A3C9319F1E2F621900C25FEC /* Main.storyboard */, 111 | A3C931A21E2F621900C25FEC /* Assets.xcassets */, 112 | A3C931A41E2F621900C25FEC /* LaunchScreen.storyboard */, 113 | A3C931A71E2F621900C25FEC /* Info.plist */, 114 | A3C931961E2F621900C25FEC /* Supporting Files */, 115 | ); 116 | path = ANYMethodLogDemo; 117 | sourceTree = ""; 118 | }; 119 | A3C931961E2F621900C25FEC /* Supporting Files */ = { 120 | isa = PBXGroup; 121 | children = ( 122 | A3C931971E2F621900C25FEC /* main.m */, 123 | ); 124 | name = "Supporting Files"; 125 | sourceTree = ""; 126 | }; 127 | A3C931AF1E2F621900C25FEC /* ANYMethodLogDemoTests */ = { 128 | isa = PBXGroup; 129 | children = ( 130 | A3C931B01E2F621900C25FEC /* ANYMethodLogDemoTests.m */, 131 | A3FBEA401E35FD2E000F10FA /* ClassMethodTests.m */, 132 | A3C931B21E2F621900C25FEC /* Info.plist */, 133 | ); 134 | path = ANYMethodLogDemoTests; 135 | sourceTree = ""; 136 | }; 137 | A3C931BB1E2F625900C25FEC /* ANYMethodLog */ = { 138 | isa = PBXGroup; 139 | children = ( 140 | A3C931BC1E2F625900C25FEC /* ANYMethodLog.h */, 141 | A3C931BD1E2F625900C25FEC /* ANYMethodLog.m */, 142 | ); 143 | name = ANYMethodLog; 144 | path = ../ANYMethodLog; 145 | sourceTree = ""; 146 | }; 147 | /* End PBXGroup section */ 148 | 149 | /* Begin PBXNativeTarget section */ 150 | A3C931921E2F621900C25FEC /* ANYMethodLogDemo */ = { 151 | isa = PBXNativeTarget; 152 | buildConfigurationList = A3C931B51E2F621900C25FEC /* Build configuration list for PBXNativeTarget "ANYMethodLogDemo" */; 153 | buildPhases = ( 154 | A3C9318F1E2F621900C25FEC /* Sources */, 155 | A3C931901E2F621900C25FEC /* Frameworks */, 156 | A3C931911E2F621900C25FEC /* Resources */, 157 | ); 158 | buildRules = ( 159 | ); 160 | dependencies = ( 161 | ); 162 | name = ANYMethodLogDemo; 163 | productName = ANYMethodLogDemo; 164 | productReference = A3C931931E2F621900C25FEC /* ANYMethodLogDemo.app */; 165 | productType = "com.apple.product-type.application"; 166 | }; 167 | A3C931AB1E2F621900C25FEC /* ANYMethodLogDemoTests */ = { 168 | isa = PBXNativeTarget; 169 | buildConfigurationList = A3C931B81E2F621900C25FEC /* Build configuration list for PBXNativeTarget "ANYMethodLogDemoTests" */; 170 | buildPhases = ( 171 | A3C931A81E2F621900C25FEC /* Sources */, 172 | A3C931A91E2F621900C25FEC /* Frameworks */, 173 | A3C931AA1E2F621900C25FEC /* Resources */, 174 | ); 175 | buildRules = ( 176 | ); 177 | dependencies = ( 178 | A3C931AE1E2F621900C25FEC /* PBXTargetDependency */, 179 | ); 180 | name = ANYMethodLogDemoTests; 181 | productName = ANYMethodLogDemoTests; 182 | productReference = A3C931AC1E2F621900C25FEC /* ANYMethodLogDemoTests.xctest */; 183 | productType = "com.apple.product-type.bundle.unit-test"; 184 | }; 185 | /* End PBXNativeTarget section */ 186 | 187 | /* Begin PBXProject section */ 188 | A3C9318B1E2F621900C25FEC /* Project object */ = { 189 | isa = PBXProject; 190 | attributes = { 191 | LastUpgradeCheck = 0820; 192 | ORGANIZATIONNAME = qiuhaodong; 193 | TargetAttributes = { 194 | A3C931921E2F621900C25FEC = { 195 | CreatedOnToolsVersion = 8.2.1; 196 | DevelopmentTeam = M7686ZYZB3; 197 | ProvisioningStyle = Automatic; 198 | }; 199 | A3C931AB1E2F621900C25FEC = { 200 | CreatedOnToolsVersion = 8.2.1; 201 | DevelopmentTeam = M7686ZYZB3; 202 | ProvisioningStyle = Automatic; 203 | TestTargetID = A3C931921E2F621900C25FEC; 204 | }; 205 | }; 206 | }; 207 | buildConfigurationList = A3C9318E1E2F621900C25FEC /* Build configuration list for PBXProject "ANYMethodLogDemo" */; 208 | compatibilityVersion = "Xcode 3.2"; 209 | developmentRegion = English; 210 | hasScannedForEncodings = 0; 211 | knownRegions = ( 212 | en, 213 | Base, 214 | ); 215 | mainGroup = A3C9318A1E2F621900C25FEC; 216 | productRefGroup = A3C931941E2F621900C25FEC /* Products */; 217 | projectDirPath = ""; 218 | projectRoot = ""; 219 | targets = ( 220 | A3C931921E2F621900C25FEC /* ANYMethodLogDemo */, 221 | A3C931AB1E2F621900C25FEC /* ANYMethodLogDemoTests */, 222 | ); 223 | }; 224 | /* End PBXProject section */ 225 | 226 | /* Begin PBXResourcesBuildPhase section */ 227 | A3C931911E2F621900C25FEC /* Resources */ = { 228 | isa = PBXResourcesBuildPhase; 229 | buildActionMask = 2147483647; 230 | files = ( 231 | A3C931A61E2F621900C25FEC /* LaunchScreen.storyboard in Resources */, 232 | A3C931A31E2F621900C25FEC /* Assets.xcassets in Resources */, 233 | A3C931A11E2F621900C25FEC /* Main.storyboard in Resources */, 234 | A3C931CD1E2F652E00C25FEC /* DetailController.xib in Resources */, 235 | A3C931C81E2F651F00C25FEC /* ListController.xib in Resources */, 236 | ); 237 | runOnlyForDeploymentPostprocessing = 0; 238 | }; 239 | A3C931AA1E2F621900C25FEC /* Resources */ = { 240 | isa = PBXResourcesBuildPhase; 241 | buildActionMask = 2147483647; 242 | files = ( 243 | ); 244 | runOnlyForDeploymentPostprocessing = 0; 245 | }; 246 | /* End PBXResourcesBuildPhase section */ 247 | 248 | /* Begin PBXSourcesBuildPhase section */ 249 | A3C9318F1E2F621900C25FEC /* Sources */ = { 250 | isa = PBXSourcesBuildPhase; 251 | buildActionMask = 2147483647; 252 | files = ( 253 | A3C931C21E2F64DA00C25FEC /* HomeController.m in Sources */, 254 | A3C931BE1E2F625900C25FEC /* ANYMethodLog.m in Sources */, 255 | A3C931CC1E2F652E00C25FEC /* DetailController.m in Sources */, 256 | A3C931C71E2F651F00C25FEC /* ListController.m in Sources */, 257 | A3C9319B1E2F621900C25FEC /* AppDelegate.m in Sources */, 258 | A3C931981E2F621900C25FEC /* main.m in Sources */, 259 | ); 260 | runOnlyForDeploymentPostprocessing = 0; 261 | }; 262 | A3C931A81E2F621900C25FEC /* Sources */ = { 263 | isa = PBXSourcesBuildPhase; 264 | buildActionMask = 2147483647; 265 | files = ( 266 | A3C931B11E2F621900C25FEC /* ANYMethodLogDemoTests.m in Sources */, 267 | A3FBEA411E35FD2E000F10FA /* ClassMethodTests.m in Sources */, 268 | ); 269 | runOnlyForDeploymentPostprocessing = 0; 270 | }; 271 | /* End PBXSourcesBuildPhase section */ 272 | 273 | /* Begin PBXTargetDependency section */ 274 | A3C931AE1E2F621900C25FEC /* PBXTargetDependency */ = { 275 | isa = PBXTargetDependency; 276 | target = A3C931921E2F621900C25FEC /* ANYMethodLogDemo */; 277 | targetProxy = A3C931AD1E2F621900C25FEC /* PBXContainerItemProxy */; 278 | }; 279 | /* End PBXTargetDependency section */ 280 | 281 | /* Begin PBXVariantGroup section */ 282 | A3C9319F1E2F621900C25FEC /* Main.storyboard */ = { 283 | isa = PBXVariantGroup; 284 | children = ( 285 | A3C931A01E2F621900C25FEC /* Base */, 286 | ); 287 | name = Main.storyboard; 288 | sourceTree = ""; 289 | }; 290 | A3C931A41E2F621900C25FEC /* LaunchScreen.storyboard */ = { 291 | isa = PBXVariantGroup; 292 | children = ( 293 | A3C931A51E2F621900C25FEC /* Base */, 294 | ); 295 | name = LaunchScreen.storyboard; 296 | sourceTree = ""; 297 | }; 298 | /* End PBXVariantGroup section */ 299 | 300 | /* Begin XCBuildConfiguration section */ 301 | A3C931B31E2F621900C25FEC /* Debug */ = { 302 | isa = XCBuildConfiguration; 303 | buildSettings = { 304 | ALWAYS_SEARCH_USER_PATHS = NO; 305 | CLANG_ANALYZER_NONNULL = YES; 306 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 307 | CLANG_CXX_LIBRARY = "libc++"; 308 | CLANG_ENABLE_MODULES = YES; 309 | CLANG_ENABLE_OBJC_ARC = YES; 310 | CLANG_WARN_BOOL_CONVERSION = YES; 311 | CLANG_WARN_CONSTANT_CONVERSION = YES; 312 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 313 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 314 | CLANG_WARN_EMPTY_BODY = YES; 315 | CLANG_WARN_ENUM_CONVERSION = YES; 316 | CLANG_WARN_INFINITE_RECURSION = YES; 317 | CLANG_WARN_INT_CONVERSION = YES; 318 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 319 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 320 | CLANG_WARN_UNREACHABLE_CODE = YES; 321 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 322 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 323 | COPY_PHASE_STRIP = NO; 324 | DEBUG_INFORMATION_FORMAT = dwarf; 325 | ENABLE_STRICT_OBJC_MSGSEND = YES; 326 | ENABLE_TESTABILITY = YES; 327 | GCC_C_LANGUAGE_STANDARD = gnu99; 328 | GCC_DYNAMIC_NO_PIC = NO; 329 | GCC_NO_COMMON_BLOCKS = YES; 330 | GCC_OPTIMIZATION_LEVEL = 0; 331 | GCC_PREPROCESSOR_DEFINITIONS = ( 332 | "DEBUG=1", 333 | "$(inherited)", 334 | ); 335 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 336 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 337 | GCC_WARN_UNDECLARED_SELECTOR = YES; 338 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 339 | GCC_WARN_UNUSED_FUNCTION = YES; 340 | GCC_WARN_UNUSED_VARIABLE = YES; 341 | IPHONEOS_DEPLOYMENT_TARGET = 10.2; 342 | MTL_ENABLE_DEBUG_INFO = YES; 343 | ONLY_ACTIVE_ARCH = YES; 344 | SDKROOT = iphoneos; 345 | }; 346 | name = Debug; 347 | }; 348 | A3C931B41E2F621900C25FEC /* Release */ = { 349 | isa = XCBuildConfiguration; 350 | buildSettings = { 351 | ALWAYS_SEARCH_USER_PATHS = NO; 352 | CLANG_ANALYZER_NONNULL = YES; 353 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 354 | CLANG_CXX_LIBRARY = "libc++"; 355 | CLANG_ENABLE_MODULES = YES; 356 | CLANG_ENABLE_OBJC_ARC = YES; 357 | CLANG_WARN_BOOL_CONVERSION = YES; 358 | CLANG_WARN_CONSTANT_CONVERSION = YES; 359 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 360 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 361 | CLANG_WARN_EMPTY_BODY = YES; 362 | CLANG_WARN_ENUM_CONVERSION = YES; 363 | CLANG_WARN_INFINITE_RECURSION = YES; 364 | CLANG_WARN_INT_CONVERSION = YES; 365 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 366 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 367 | CLANG_WARN_UNREACHABLE_CODE = YES; 368 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 369 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 370 | COPY_PHASE_STRIP = NO; 371 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 372 | ENABLE_NS_ASSERTIONS = NO; 373 | ENABLE_STRICT_OBJC_MSGSEND = YES; 374 | GCC_C_LANGUAGE_STANDARD = gnu99; 375 | GCC_NO_COMMON_BLOCKS = YES; 376 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 377 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 378 | GCC_WARN_UNDECLARED_SELECTOR = YES; 379 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 380 | GCC_WARN_UNUSED_FUNCTION = YES; 381 | GCC_WARN_UNUSED_VARIABLE = YES; 382 | IPHONEOS_DEPLOYMENT_TARGET = 10.2; 383 | MTL_ENABLE_DEBUG_INFO = NO; 384 | SDKROOT = iphoneos; 385 | VALIDATE_PRODUCT = YES; 386 | }; 387 | name = Release; 388 | }; 389 | A3C931B61E2F621900C25FEC /* Debug */ = { 390 | isa = XCBuildConfiguration; 391 | buildSettings = { 392 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 393 | DEVELOPMENT_TEAM = M7686ZYZB3; 394 | INFOPLIST_FILE = ANYMethodLogDemo/Info.plist; 395 | IPHONEOS_DEPLOYMENT_TARGET = 10.0; 396 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 397 | PRODUCT_BUNDLE_IDENTIFIER = com.qiuhaodong.ANYMethodLogDemo; 398 | PRODUCT_NAME = "$(TARGET_NAME)"; 399 | }; 400 | name = Debug; 401 | }; 402 | A3C931B71E2F621900C25FEC /* Release */ = { 403 | isa = XCBuildConfiguration; 404 | buildSettings = { 405 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 406 | DEVELOPMENT_TEAM = M7686ZYZB3; 407 | INFOPLIST_FILE = ANYMethodLogDemo/Info.plist; 408 | IPHONEOS_DEPLOYMENT_TARGET = 10.0; 409 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 410 | PRODUCT_BUNDLE_IDENTIFIER = com.qiuhaodong.ANYMethodLogDemo; 411 | PRODUCT_NAME = "$(TARGET_NAME)"; 412 | }; 413 | name = Release; 414 | }; 415 | A3C931B91E2F621900C25FEC /* Debug */ = { 416 | isa = XCBuildConfiguration; 417 | buildSettings = { 418 | BUNDLE_LOADER = "$(TEST_HOST)"; 419 | DEVELOPMENT_TEAM = M7686ZYZB3; 420 | INFOPLIST_FILE = ANYMethodLogDemoTests/Info.plist; 421 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 422 | PRODUCT_BUNDLE_IDENTIFIER = com.qiuhaodong.ANYMethodLogDemoTests; 423 | PRODUCT_NAME = "$(TARGET_NAME)"; 424 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/ANYMethodLogDemo.app/ANYMethodLogDemo"; 425 | }; 426 | name = Debug; 427 | }; 428 | A3C931BA1E2F621900C25FEC /* Release */ = { 429 | isa = XCBuildConfiguration; 430 | buildSettings = { 431 | BUNDLE_LOADER = "$(TEST_HOST)"; 432 | DEVELOPMENT_TEAM = M7686ZYZB3; 433 | INFOPLIST_FILE = ANYMethodLogDemoTests/Info.plist; 434 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 435 | PRODUCT_BUNDLE_IDENTIFIER = com.qiuhaodong.ANYMethodLogDemoTests; 436 | PRODUCT_NAME = "$(TARGET_NAME)"; 437 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/ANYMethodLogDemo.app/ANYMethodLogDemo"; 438 | }; 439 | name = Release; 440 | }; 441 | /* End XCBuildConfiguration section */ 442 | 443 | /* Begin XCConfigurationList section */ 444 | A3C9318E1E2F621900C25FEC /* Build configuration list for PBXProject "ANYMethodLogDemo" */ = { 445 | isa = XCConfigurationList; 446 | buildConfigurations = ( 447 | A3C931B31E2F621900C25FEC /* Debug */, 448 | A3C931B41E2F621900C25FEC /* Release */, 449 | ); 450 | defaultConfigurationIsVisible = 0; 451 | defaultConfigurationName = Release; 452 | }; 453 | A3C931B51E2F621900C25FEC /* Build configuration list for PBXNativeTarget "ANYMethodLogDemo" */ = { 454 | isa = XCConfigurationList; 455 | buildConfigurations = ( 456 | A3C931B61E2F621900C25FEC /* Debug */, 457 | A3C931B71E2F621900C25FEC /* Release */, 458 | ); 459 | defaultConfigurationIsVisible = 0; 460 | defaultConfigurationName = Release; 461 | }; 462 | A3C931B81E2F621900C25FEC /* Build configuration list for PBXNativeTarget "ANYMethodLogDemoTests" */ = { 463 | isa = XCConfigurationList; 464 | buildConfigurations = ( 465 | A3C931B91E2F621900C25FEC /* Debug */, 466 | A3C931BA1E2F621900C25FEC /* Release */, 467 | ); 468 | defaultConfigurationIsVisible = 0; 469 | defaultConfigurationName = Release; 470 | }; 471 | /* End XCConfigurationList section */ 472 | }; 473 | rootObject = A3C9318B1E2F621900C25FEC /* Project object */; 474 | } 475 | -------------------------------------------------------------------------------- /ANYMethodLogDemo/ANYMethodLogDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /ANYMethodLogDemo/ANYMethodLogDemo/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // ANYMethodLogDemo 4 | // 5 | // Created by qiuhaodong on 2017/1/18. 6 | // Copyright © 2017年 qiuhaodong. All rights reserved. 7 | // 8 | // https://github.com/qhd/ANYMethodLog.git 9 | // 10 | 11 | #import 12 | 13 | @interface AppDelegate : UIResponder 14 | 15 | @property (strong, nonatomic) UIWindow *window; 16 | 17 | 18 | @end 19 | 20 | -------------------------------------------------------------------------------- /ANYMethodLogDemo/ANYMethodLogDemo/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // ANYMethodLogDemo 4 | // 5 | // Created by qiuhaodong on 2017/1/18. 6 | // Copyright © 2017年 qiuhaodong. All rights reserved. 7 | // 8 | // https://github.com/qhd/ANYMethodLog.git 9 | // 10 | 11 | #import "AppDelegate.h" 12 | #import "ANYMethodLog.h" 13 | #import "ListController.h" 14 | 15 | @interface AppDelegate () 16 | 17 | @end 18 | 19 | @implementation AppDelegate 20 | 21 | 22 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 23 | 24 | // Usage 1: 打印一个类定义的所有方法,包括公开方法和私有方法 25 | // [ANYMethodLog logMethodWithClass:[UIViewController class] condition:^BOOL(SEL sel) { 26 | // NSLog(@"method:%@", NSStringFromSelector(sel)); 27 | // return NO; 28 | // } before:nil after:nil]; 29 | 30 | 31 | // Usage 2: 打印在运行过程中调用了哪些方法 32 | [ANYMethodLog logMethodWithClass:[UIViewController class] condition:^BOOL(SEL sel) { 33 | return YES; 34 | } before:^(id target, SEL sel, NSArray *args, int deep) { 35 | NSLog(@"target:%@ sel:%@", target, NSStringFromSelector(sel)); 36 | } after:nil]; 37 | 38 | 39 | // Usage 3: 打印特定几个方法的调用顺序 40 | // [ANYMethodLog logMethodWithClass:[UIViewController class] condition:^BOOL(SEL sel) { 41 | // 42 | // NSArray *whiteList = @[@"loadView", @"viewWillAppear:", @"viewDidAppear:", @"viewWillDisappear:", @"viewDidDisappear:", @"viewWillLayoutSubviews", @"viewDidLayoutSubviews"]; 43 | // return [whiteList containsObject:NSStringFromSelector(sel)]; 44 | // 45 | // } before:^(id target, SEL sel, NSArray *args, int deep) { 46 | // 47 | // NSLog(@"target:%@ sel:%@", target, NSStringFromSelector(sel)); 48 | // 49 | // } after:nil]; 50 | 51 | 52 | // Usage 4: 打印调用方法时的参数值 53 | // [ANYMethodLog logMethodWithClass:NSClassFromString(@"UIViewController") condition:^BOOL(SEL sel) { 54 | // 55 | // return [NSStringFromSelector(sel) isEqualToString:@"viewWillAppear:"]; 56 | // 57 | // } before:^(id target, SEL sel, NSArray *args, int deep) { 58 | // 59 | // NSLog(@"before target:%@ sel:%@ args:%@", target, NSStringFromSelector(sel), args); 60 | // 61 | // } after:nil]; 62 | 63 | 64 | // Usage 5: 打印某个方法调用前后的变化 65 | // [ANYMethodLog logMethodWithClass:NSClassFromString(@"ListController") condition:^BOOL(SEL sel) { 66 | // 67 | // return [NSStringFromSelector(sel) isEqualToString:@"changeBackground"]; 68 | // 69 | // } before:^(id target, SEL sel, NSArray *args, int deep) { 70 | // 71 | // NSLog(@"before background color:%@", [(ListController *)target view].backgroundColor); 72 | // 73 | // } after:^(id target, SEL sel, NSArray *args, NSTimeInterval interval, int deep, id retValue) { 74 | // 75 | // NSLog(@"after background color:%@", [(ListController *)target view].backgroundColor); 76 | // 77 | // }]; 78 | 79 | 80 | // Usage 6: 打印某个方法调用的耗时 81 | // [ANYMethodLog logMethodWithClass:NSClassFromString(@"ListController") condition:^BOOL(SEL sel) { 82 | // 83 | // return [NSStringFromSelector(sel) isEqualToString:@"changeBackground"]; 84 | // 85 | // } before:^(id target, SEL sel, NSArray *args, int deep) { 86 | // 87 | // 88 | // } after:^(id target, SEL sel, NSArray *args, NSTimeInterval interval, int deep, id retValue) { 89 | // 90 | // NSLog(@"interval::%@", [@(interval) stringValue]); 91 | // 92 | // }]; 93 | 94 | // Usage 7: 打印方法调用跟踪 95 | // [ANYMethodLog logMethodWithClass:NSClassFromString(@"ListController") condition:^BOOL(SEL sel) { 96 | // return YES; 97 | // } before:^(id target, SEL sel, NSArray *args, int deep) { 98 | // NSString *selector = NSStringFromSelector(sel); 99 | // NSArray *selectorArrary = [selector componentsSeparatedByString:@":"]; 100 | // selectorArrary = [selectorArrary filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"length > 0"]]; 101 | // NSMutableString *selectorString = [NSMutableString new]; 102 | // for (int i = 0; i < selectorArrary.count; i++) { 103 | // [selectorString appendFormat:@"%@:%@ ", selectorArrary[i], args[i]]; 104 | // } 105 | // NSMutableString *deepString = [NSMutableString new]; 106 | // for (int i = 0; i < deep; i++) { 107 | // [deepString appendString:@"-"]; 108 | // } 109 | // NSLog(@"%@[%@ %@]", deepString , target, selectorString); 110 | // } after:^(id target, SEL sel, NSArray *args, NSTimeInterval interval, int deep, id retValue) { 111 | // NSMutableString *deepString = [NSMutableString new]; 112 | // for (int i = 0; i < deep; i++) { 113 | // [deepString appendString:@"-"]; 114 | // } 115 | // NSLog(@"%@ret:%@", deepString, retValue); 116 | // }]; 117 | 118 | return YES; 119 | } 120 | 121 | 122 | - (void)applicationWillResignActive:(UIApplication *)application { 123 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 124 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 125 | } 126 | 127 | 128 | - (void)applicationDidEnterBackground:(UIApplication *)application { 129 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 130 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 131 | } 132 | 133 | 134 | - (void)applicationWillEnterForeground:(UIApplication *)application { 135 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 136 | } 137 | 138 | 139 | - (void)applicationDidBecomeActive:(UIApplication *)application { 140 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 141 | } 142 | 143 | 144 | - (void)applicationWillTerminate:(UIApplication *)application { 145 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 146 | } 147 | 148 | 149 | @end 150 | -------------------------------------------------------------------------------- /ANYMethodLogDemo/ANYMethodLogDemo/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | } 43 | ], 44 | "info" : { 45 | "version" : 1, 46 | "author" : "xcode" 47 | } 48 | } -------------------------------------------------------------------------------- /ANYMethodLogDemo/ANYMethodLogDemo/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /ANYMethodLogDemo/ANYMethodLogDemo/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /ANYMethodLogDemo/ANYMethodLogDemo/DetailController.h: -------------------------------------------------------------------------------- 1 | // 2 | // DetailController.h 3 | // ANYMethodLogDemo 4 | // 5 | // Created by qiuhaodong on 2017/1/18. 6 | // Copyright © 2017年 qiuhaodong. All rights reserved. 7 | // 8 | // https://github.com/qhd/ANYMethodLog.git 9 | // 10 | 11 | #import 12 | 13 | @interface DetailController : UIViewController 14 | 15 | @end 16 | -------------------------------------------------------------------------------- /ANYMethodLogDemo/ANYMethodLogDemo/DetailController.m: -------------------------------------------------------------------------------- 1 | // 2 | // DetailController.m 3 | // ANYMethodLogDemo 4 | // 5 | // Created by qiuhaodong on 2017/1/18. 6 | // Copyright © 2017年 qiuhaodong. All rights reserved. 7 | // 8 | // https://github.com/qhd/ANYMethodLog.git 9 | // 10 | 11 | #import "DetailController.h" 12 | 13 | @interface DetailController () 14 | 15 | @end 16 | 17 | @implementation DetailController 18 | 19 | - (void)viewDidLoad { 20 | [super viewDidLoad]; 21 | // Do any additional setup after loading the view from its nib. 22 | } 23 | 24 | - (void)didReceiveMemoryWarning { 25 | [super didReceiveMemoryWarning]; 26 | // Dispose of any resources that can be recreated. 27 | } 28 | 29 | /* 30 | #pragma mark - Navigation 31 | 32 | // In a storyboard-based application, you will often want to do a little preparation before navigation 33 | - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 34 | // Get the new view controller using [segue destinationViewController]. 35 | // Pass the selected object to the new view controller. 36 | } 37 | */ 38 | 39 | @end 40 | -------------------------------------------------------------------------------- /ANYMethodLogDemo/ANYMethodLogDemo/DetailController.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /ANYMethodLogDemo/ANYMethodLogDemo/HomeController.h: -------------------------------------------------------------------------------- 1 | // 2 | // HomeController.h 3 | // ANYMethodLogDemo 4 | // 5 | // Created by qiuhaodong on 2017/1/18. 6 | // Copyright © 2017年 qiuhaodong. All rights reserved. 7 | // 8 | // https://github.com/qhd/ANYMethodLog.git 9 | // 10 | 11 | #import 12 | 13 | @interface HomeController : UIViewController 14 | 15 | @end 16 | -------------------------------------------------------------------------------- /ANYMethodLogDemo/ANYMethodLogDemo/HomeController.m: -------------------------------------------------------------------------------- 1 | // 2 | // HomeController.m 3 | // ANYMethodLogDemo 4 | // 5 | // Created by qiuhaodong on 2017/1/18. 6 | // Copyright © 2017年 qiuhaodong. All rights reserved. 7 | // 8 | // https://github.com/qhd/ANYMethodLog.git 9 | // 10 | 11 | #import "HomeController.h" 12 | #import "ListController.h" 13 | 14 | @interface HomeController () 15 | 16 | @end 17 | 18 | @implementation HomeController 19 | 20 | - (void)viewDidLoad { 21 | [super viewDidLoad]; 22 | // Do any additional setup after loading the view from its nib. 23 | } 24 | 25 | - (void)didReceiveMemoryWarning { 26 | [super didReceiveMemoryWarning]; 27 | // Dispose of any resources that can be recreated. 28 | } 29 | 30 | /* 31 | #pragma mark - Navigation 32 | 33 | // In a storyboard-based application, you will often want to do a little preparation before navigation 34 | - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 35 | // Get the new view controller using [segue destinationViewController]. 36 | // Pass the selected object to the new view controller. 37 | } 38 | */ 39 | 40 | - (IBAction)clickPushButton:(id)sender { 41 | [self.navigationController pushViewController:[[ListController alloc] init] animated:YES]; 42 | } 43 | 44 | @end 45 | -------------------------------------------------------------------------------- /ANYMethodLogDemo/ANYMethodLogDemo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /ANYMethodLogDemo/ANYMethodLogDemo/ListController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ListController.h 3 | // ANYMethodLogDemo 4 | // 5 | // Created by qiuhaodong on 2017/1/18. 6 | // Copyright © 2017年 qiuhaodong. All rights reserved. 7 | // 8 | // https://github.com/qhd/ANYMethodLog.git 9 | // 10 | 11 | #import 12 | 13 | @interface ListController : UIViewController 14 | 15 | @end 16 | -------------------------------------------------------------------------------- /ANYMethodLogDemo/ANYMethodLogDemo/ListController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ListController.m 3 | // ANYMethodLogDemo 4 | // 5 | // Created by qiuhaodong on 2017/1/18. 6 | // Copyright © 2017年 qiuhaodong. All rights reserved. 7 | // 8 | // https://github.com/qhd/ANYMethodLog.git 9 | // 10 | 11 | #import "ListController.h" 12 | #import "DetailController.h" 13 | 14 | @interface ListController () 15 | 16 | @end 17 | 18 | @implementation ListController 19 | 20 | - (void)viewDidLoad { 21 | [super viewDidLoad]; 22 | [self changeBackground]; 23 | } 24 | 25 | - (void)changeBackground { 26 | self.view.backgroundColor = [UIColor grayColor]; 27 | } 28 | 29 | - (void)didReceiveMemoryWarning { 30 | [super didReceiveMemoryWarning]; 31 | // Dispose of any resources that can be recreated. 32 | } 33 | 34 | - (void)viewWillLayoutSubviews { 35 | [super viewWillLayoutSubviews]; 36 | } 37 | 38 | - (void)viewDidLayoutSubviews { 39 | [super viewDidLayoutSubviews]; 40 | } 41 | 42 | /* 43 | #pragma mark - Navigation 44 | 45 | // In a storyboard-based application, you will often want to do a little preparation before navigation 46 | - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 47 | // Get the new view controller using [segue destinationViewController]. 48 | // Pass the selected object to the new view controller. 49 | } 50 | */ 51 | 52 | - (IBAction)clickEnterDetailButton:(id)sender { 53 | [self.navigationController pushViewController:[[DetailController alloc] init] animated:YES]; 54 | } 55 | 56 | @end 57 | -------------------------------------------------------------------------------- /ANYMethodLogDemo/ANYMethodLogDemo/ListController.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /ANYMethodLogDemo/ANYMethodLogDemo/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // ANYMethodLogDemo 4 | // 5 | // Created by qiuhaodong on 2017/1/18. 6 | // Copyright © 2017年 qiuhaodong. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /ANYMethodLogDemo/ANYMethodLogDemoTests/ANYMethodLogDemoTests.m: -------------------------------------------------------------------------------- 1 | // 2 | // ANYMethodLogDemoTests.m 3 | // ANYMethodLogDemoTests 4 | // 5 | // Created by qiuhaodong on 2017/1/18. 6 | // Copyright © 2017年 qiuhaodong. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "ANYMethodLog.h" 11 | 12 | #pragma mark - 测试类MyObject 13 | 14 | @interface MyObject : NSObject 15 | 16 | - (BOOL)methodWithA:(char)a 17 | b:(int)b 18 | c:(short)c 19 | d:(long)d 20 | e:(long long)e 21 | f:(unsigned char)f 22 | g:(unsigned int)g 23 | h:(unsigned short)h 24 | i:(unsigned long)i 25 | j:(unsigned long long)j 26 | k:(float)k 27 | l:(double)l 28 | m:(bool)m 29 | //n:(void)n 30 | o:(char *)o 31 | p:(id)p 32 | q:(Class)q 33 | r:(SEL)r 34 | s:(CGRect)s 35 | t:(CGPoint)t 36 | u:(CGSize)u 37 | v:(CGVector)v 38 | w:(CGAffineTransform)w 39 | x:(UIOffset)x 40 | y:(UIEdgeInsets)y; 41 | - (char)method1:(char)a; 42 | - (int)method2:(int)a; 43 | - (short)method3:(short)a; 44 | - (long)method4:(long)a; 45 | - (long long)method5:(long long)a; 46 | - (unsigned char)method6:(unsigned char)a; 47 | - (unsigned int)method7:(unsigned int)a; 48 | - (unsigned short)method8:(unsigned short)a; 49 | - (unsigned long)method9:(unsigned long)a; 50 | - (unsigned long long)method10:(unsigned long long)a; 51 | - (float)method11:(float)a ; 52 | - (double)method12:(double)a; 53 | - (BOOL)method13:(BOOL)a; 54 | - (void)method14; 55 | - (char *)method15:(char *)a; 56 | - (id)method16:(id)a; 57 | - (Class)method17:(Class)a; 58 | - (SEL)method18:(SEL)a; 59 | - (CGRect)method19:(CGRect)a; 60 | - (CGPoint)method20:(CGPoint)a; 61 | - (CGSize)method21:(CGSize)a; 62 | - (CGVector)method22:(CGVector)a; 63 | - (CGAffineTransform)method23:(CGAffineTransform)a; 64 | - (UIEdgeInsets)method24:(UIEdgeInsets)a; 65 | - (UIOffset)method25:(UIOffset)a; 66 | - (NSString *)method26:(NSString *)a; 67 | 68 | @end 69 | 70 | #define DEFAULT_LOG NSLog(@"cmd:%@", NSStringFromSelector(_cmd)) 71 | 72 | @implementation MyObject 73 | 74 | - (BOOL)methodWithA:(char)a 75 | b:(int)b 76 | c:(short)c 77 | d:(long)d 78 | e:(long long)e 79 | f:(unsigned char)f 80 | g:(unsigned int)g 81 | h:(unsigned short)h 82 | i:(unsigned long)i 83 | j:(unsigned long long)j 84 | k:(float)k 85 | l:(double)l 86 | m:(bool)m 87 | //n:(void)n 88 | o:(char *)o 89 | p:(id)p 90 | q:(Class)q 91 | r:(SEL)r 92 | s:(CGRect)s 93 | t:(CGPoint)t 94 | u:(CGSize)u 95 | v:(CGVector)v 96 | w:(CGAffineTransform)w 97 | x:(UIOffset)x 98 | y:(UIEdgeInsets)y 99 | { 100 | DEFAULT_LOG; 101 | NSLog(@"a:%@, b:%@, c:%@, d:%@, e:%@, f:%@, g:%@, h:%@, i:%@, j:%@, k:%@, l:%@, m:%@, n:void, o:%@, p:%@, q:%@, r:%@, s:%@, t:%@, u:%@, v:%@, w:%@, x:%@, y:%@", 102 | [@(a) stringValue], 103 | [@(b) stringValue], 104 | [@(c) stringValue], 105 | [@(d) stringValue], 106 | [@(e) stringValue], 107 | [@(f) stringValue], 108 | [@(g) stringValue], 109 | [@(h) stringValue], 110 | [@(i) stringValue], 111 | [@(j) stringValue], 112 | [@(k) stringValue], 113 | [@(l) stringValue], 114 | [@(m) stringValue], 115 | [NSString stringWithUTF8String:o], 116 | p, 117 | NSStringFromClass(q), 118 | NSStringFromSelector(r), 119 | NSStringFromCGRect(s), 120 | NSStringFromCGPoint(t), 121 | NSStringFromCGSize(u), 122 | NSStringFromCGVector(v), 123 | NSStringFromCGAffineTransform(w), 124 | NSStringFromUIOffset(x), 125 | NSStringFromUIEdgeInsets(y) 126 | ); 127 | return YES; 128 | } 129 | 130 | - (char)method1:(char)a { 131 | DEFAULT_LOG; 132 | return a; 133 | } 134 | 135 | - (int)method2:(int)a { 136 | DEFAULT_LOG; 137 | return a; 138 | } 139 | 140 | - (short)method3:(short)a { 141 | DEFAULT_LOG; 142 | return a; 143 | } 144 | 145 | - (long)method4:(long)a { 146 | DEFAULT_LOG; 147 | return a; 148 | } 149 | 150 | - (long long)method5:(long long)a { 151 | DEFAULT_LOG; 152 | return a; 153 | } 154 | 155 | - (unsigned char)method6:(unsigned char)a { 156 | DEFAULT_LOG; 157 | return a; 158 | } 159 | 160 | - (unsigned int)method7:(unsigned int)a { 161 | DEFAULT_LOG; 162 | return a; 163 | } 164 | 165 | - (unsigned short)method8:(unsigned short)a { 166 | DEFAULT_LOG; 167 | return a; 168 | } 169 | 170 | - (unsigned long)method9:(unsigned long)a { 171 | DEFAULT_LOG; 172 | return a; 173 | } 174 | 175 | - (unsigned long long)method10:(unsigned long long)a { 176 | DEFAULT_LOG; 177 | return a; 178 | } 179 | 180 | - (float)method11:(float)a { 181 | DEFAULT_LOG; 182 | return a; 183 | } 184 | 185 | - (double)method12:(double)a { 186 | DEFAULT_LOG; 187 | return a; 188 | } 189 | 190 | - (BOOL)method13:(BOOL)a { 191 | DEFAULT_LOG; 192 | return a; 193 | } 194 | 195 | - (void)method14 { 196 | DEFAULT_LOG; 197 | } 198 | 199 | - (char *)method15:(char *)a { 200 | DEFAULT_LOG; 201 | return a; 202 | } 203 | 204 | - (id)method16:(id)a { 205 | DEFAULT_LOG; 206 | return a; 207 | } 208 | 209 | - (Class)method17:(Class)a { 210 | DEFAULT_LOG; 211 | return a; 212 | } 213 | 214 | - (SEL)method18:(SEL)a { 215 | DEFAULT_LOG; 216 | return a; 217 | } 218 | 219 | - (CGRect)method19:(CGRect)a { 220 | DEFAULT_LOG; 221 | return a; 222 | } 223 | 224 | - (CGPoint)method20:(CGPoint)a { 225 | DEFAULT_LOG; 226 | return a; 227 | } 228 | 229 | - (CGSize)method21:(CGSize)a { 230 | DEFAULT_LOG; 231 | return a; 232 | } 233 | 234 | - (CGVector)method22:(CGVector)a { 235 | DEFAULT_LOG; 236 | return a; 237 | } 238 | 239 | - (CGAffineTransform)method23:(CGAffineTransform)a { 240 | DEFAULT_LOG; 241 | return a; 242 | } 243 | 244 | - (UIEdgeInsets)method24:(UIEdgeInsets)a { 245 | DEFAULT_LOG; 246 | return a; 247 | } 248 | 249 | - (UIOffset)method25:(UIOffset)a { 250 | DEFAULT_LOG; 251 | return a; 252 | } 253 | 254 | - (NSString *)method26:(NSString *)a { 255 | DEFAULT_LOG; 256 | return a; 257 | } 258 | 259 | - (void (^)())method27:(void (^)())a { 260 | DEFAULT_LOG; 261 | return a; 262 | } 263 | 264 | @end 265 | 266 | 267 | #pragma mark - 268 | 269 | @interface ANYMethodLogDemoTests : XCTestCase 270 | 271 | @end 272 | 273 | 274 | @implementation ANYMethodLogDemoTests 275 | 276 | - (void)setUp { 277 | [super setUp]; 278 | // Put setup code here. This method is called before the invocation of each test method in the class. 279 | } 280 | 281 | - (void)tearDown { 282 | // Put teardown code here. This method is called after the invocation of each test method in the class. 283 | [super tearDown]; 284 | } 285 | 286 | - (void)testExample { 287 | // This is an example of a functional test case. 288 | // Use XCTAssert and related functions to verify your tests produce the correct results. 289 | } 290 | 291 | - (void)testPerformanceExample { 292 | // This is an example of a performance test case. 293 | [self measureBlock:^{ 294 | // Put the code you want to measure the time of here. 295 | }]; 296 | } 297 | 298 | - (void)testMyObject { 299 | [ANYMethodLog logMethodWithClass:[MyObject class] condition:^BOOL(SEL sel) { 300 | return YES; 301 | } before:^(id target, SEL sel, NSArray *args, int deep) { 302 | NSLog(@"target:%@ sel:%@ args:%@", target, NSStringFromSelector(sel), args); 303 | } after:^(id target, SEL sel, NSArray *args, NSTimeInterval interval, int deep, id retValue) { 304 | 305 | }]; 306 | 307 | MyObject *o = [[MyObject alloc] init]; 308 | 309 | XCTAssertTrue([o methodWithA:'a' 310 | b:1 311 | c:12 312 | d:123 313 | e:1234 314 | f:'u' 315 | g:3 316 | h:4 317 | i:5 318 | j:6 319 | k:7.1 320 | l:7.12345 321 | m:YES 322 | o:"hello" 323 | p:o 324 | q:[o class] 325 | r:NSSelectorFromString(@"viewDidLoad") 326 | s:CGRectMake(1.1, 1.2, 1.3, 1.4) 327 | t:CGPointMake(2.11, 2.12) 328 | u:CGSizeMake(3.123, 3.1234) 329 | v:CGVectorMake(4.456, 4.567) 330 | w:CGAffineTransformMake(6.123, 6.124, 6.125, 6.126, 6.127, 6.128) 331 | x:UIOffsetMake(7.123456, 7.1234567) 332 | y:UIEdgeInsetsMake(8.0, 8.0001, 8.0002, 8.0003)]); 333 | 334 | XCTAssertTrue('a' == [o method1:'a']); 335 | XCTAssertTrue(12 == [o method2:12]); 336 | XCTAssertTrue(13 == [o method3:13]); 337 | XCTAssertTrue(1444444 == [o method4:1444444]); 338 | 339 | long long ll = 155; 340 | long long llr = [o method5:ll]; 341 | XCTAssertTrue(ll == llr); 342 | 343 | XCTAssertTrue('u' == [o method6:'u']); 344 | XCTAssertTrue(17 == [o method7:17]); 345 | XCTAssertTrue(18 == [o method8:18]); 346 | XCTAssertTrue(19000 == [o method9:19000]); 347 | XCTAssertTrue(20 == [o method10:20]); 348 | XCTAssertTrue(21 == [o method11:21]); 349 | XCTAssertTrue(22.345678 == [o method12:22.345678]); 350 | XCTAssertTrue(YES == [o method13:YES]); 351 | XCTAssertTrue(NO == [o method13:NO]); 352 | [o method14]; 353 | XCTAssertTrue("c255555" == [o method15:"c255555"]); 354 | XCTAssertTrue(o == [o method16:o]); 355 | XCTAssertTrue(NSClassFromString(@"MyObject") == [o method17:NSClassFromString(@"MyObject")]); 356 | XCTAssertTrue(NSSelectorFromString(@"method17:") == [o method18:NSSelectorFromString(@"method17:")]); 357 | XCTAssertTrue(CGRectEqualToRect(CGRectMake(1.2, 1.23, 1.234, 1.2345), [o method19:CGRectMake(1.2, 1.23, 1.234, 1.2345)])); 358 | 359 | XCTAssertTrue(CGPointEqualToPoint(CGPointMake(2.3, 2.34), [o method20:CGPointMake(2.3, 2.34)])); 360 | XCTAssertTrue(CGSizeEqualToSize(CGSizeMake(3.4, 3.45), [o method21:CGSizeMake(3.4, 3.45)])); 361 | 362 | CGVector vector = CGVectorMake(9.234, 9.2345); 363 | XCTAssertTrue(vector.dx == [o method22:vector].dx && vector.dy == [o method22:vector].dy); 364 | 365 | XCTAssertTrue(CGAffineTransformEqualToTransform(CGAffineTransformMake(4.1, 4.12, 4.123, 4.1234, 4.12345, 4.123456),[o method23:CGAffineTransformMake(4.1, 4.12, 4.123, 4.1234, 4.12345, 4.123456)])); 366 | 367 | XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsMake(5.1, 5.12, 5.123, 5.1234), [o method24:UIEdgeInsetsMake(5.1, 5.12, 5.123, 5.1234)])); 368 | 369 | XCTAssertTrue(UIOffsetEqualToOffset(UIOffsetMake(6.123, 6.1234),[o method25:UIOffsetMake(6.123, 6.1234)])); 370 | 371 | XCTAssertTrue([@"hello" isEqualToString:[o method26:@"hello"]]); 372 | 373 | void (^aBlock)() = ^() { 374 | 375 | }; 376 | XCTAssertTrue(aBlock == [o method27:aBlock]); 377 | } 378 | 379 | @end 380 | -------------------------------------------------------------------------------- /ANYMethodLogDemo/ANYMethodLogDemoTests/ClassMethodTests.m: -------------------------------------------------------------------------------- 1 | // 2 | // ClassMethodTests.m 3 | // ANYMethodLogDemo 4 | // 5 | // Created by qiuhaodong on 2017/1/23. 6 | // Copyright © 2017年 qiuhaodong. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "ANYMethodLog.h" 11 | 12 | @interface ManagerTest : NSObject 13 | 14 | + (NSString *)classMethod1:(NSString *)a; 15 | 16 | - (NSString *)instanceMethod1:(NSString *)a; 17 | 18 | @end 19 | 20 | @implementation ManagerTest 21 | 22 | + (NSString *)classMethod1:(NSString *)a { 23 | return a; 24 | } 25 | 26 | - (NSString *)instanceMethod1:(NSString *)a { 27 | return a; 28 | } 29 | 30 | @end 31 | 32 | @interface ClassMethodTests : XCTestCase 33 | 34 | @end 35 | 36 | @implementation ClassMethodTests 37 | 38 | - (void)setUp { 39 | [super setUp]; 40 | // Put setup code here. This method is called before the invocation of each test method in the class. 41 | } 42 | 43 | - (void)tearDown { 44 | // Put teardown code here. This method is called after the invocation of each test method in the class. 45 | [super tearDown]; 46 | } 47 | 48 | - (void)testExample { 49 | // This is an example of a functional test case. 50 | // Use XCTAssert and related functions to verify your tests produce the correct results. 51 | } 52 | 53 | - (void)testPerformanceExample { 54 | // This is an example of a performance test case. 55 | [self measureBlock:^{ 56 | // Put the code you want to measure the time of here. 57 | }]; 58 | } 59 | 60 | - (void)testManagerTest { 61 | [ANYMethodLog logMethodWithClass:[ManagerTest class] condition:^BOOL(SEL sel) { 62 | return YES; 63 | } before:^(id target, SEL sel, NSArray *args, int deep) { 64 | NSLog(@"befor target:%@ sel:%@", target, NSStringFromSelector(sel)); 65 | } after:^(id target, SEL sel, NSArray *args, NSTimeInterval interval, int deep, id retValue) { 66 | NSLog(@"after target:%@ sel:%@", target, NSStringFromSelector(sel)); 67 | }]; 68 | 69 | XCTAssertTrue([@"qhd" isEqualToString:[ManagerTest classMethod1:@"qhd"]]); 70 | 71 | XCTAssertTrue([@"github" isEqualToString:[[[ManagerTest alloc] init] instanceMethod1:@"github"]]); 72 | } 73 | 74 | @end 75 | -------------------------------------------------------------------------------- /ANYMethodLogDemo/ANYMethodLogDemoTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | 22 | 23 | -------------------------------------------------------------------------------- /Documentation/Images/running_show.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qhd/ANYMethodLog/a5476069eca4c014956203c6100d9a5c873e1dff/Documentation/Images/running_show.gif -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 qiuhaodong 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ANYMethodLog - log any method call of object in Objective-C 2 | [English](../master/readme-en.md) 3 | 4 | 5 | 打印 Objective-C 对象中的任何方法 6 | 7 | ![running show](Documentation/Images/running_show.gif) 8 | 9 | ## 调用说明: 10 | 11 | ```objective-c 12 | + (void)logMethodWithClass:(Class)aClass 13 | condition:(BOOL(^)(SEL sel)) condition 14 | before:(void(^)(id target, SEL sel, NSArray *args, int deep)) before 15 | after:(void(^)(id target, SEL sel, NSArray *args, NSTimeInterval interval, int deep, id retValue)) after; 16 | ``` 17 | 18 | aClass:要打印的类 19 | 20 | condition:根据此 block 来决定是否追踪方法(sel 是方法名) 21 | 22 | before:方法调用前会调用该 block(target 是检测的对象,sel 是方法名,args 是参数列表,deep 是调用层级) 23 | 24 | after:方法调用后会调用该 block(interval 是执行方法的耗时,retValue 是返回值) 25 | 26 | ## 功能: 27 | 1.打印一个类定义的所有方法,包括公开方法和私有方法: 28 | 29 | ```objective-c 30 | [ANYMethodLog logMethodWithClass:[UIViewController class] condition:^BOOL(SEL sel) { 31 | NSLog(@"method:%@", NSStringFromSelector(sel)); 32 | return NO; 33 | } before:nil after:nil]; 34 | ``` 35 | 36 | 2.打印在运行过程中调用了哪些方法: 37 | 38 | ```objective-c 39 | [ANYMethodLog logMethodWithClass:[UIViewController class] condition:^BOOL(SEL sel) { 40 | return YES; 41 | } before:^(id target, SEL sel, NSArray *args, int deep) { 42 | NSLog(@"target:%@ sel:%@", target, NSStringFromSelector(sel)); 43 | } after:nil]; 44 | ``` 45 | 46 | 3.打印特定几个方法的调用顺序: 47 | 48 | ```objective-c 49 | [ANYMethodLog logMethodWithClass:[UIViewController class] condition:^BOOL(SEL sel) { 50 | 51 | NSArray *whiteList = @[@"loadView", @"viewWillAppear:", @"viewDidAppear:", @"viewWillDisappear:", @"viewDidDisappear:", @"viewWillLayoutSubviews", @"viewDidLayoutSubviews"]; 52 | return [whiteList containsObject:NSStringFromSelector(sel)]; 53 | 54 | } before:^(id target, SEL sel, NSArray *args, int deep) { 55 | 56 | NSLog(@"target:%@ sel:%@", target, NSStringFromSelector(sel)); 57 | 58 | } after:nil]; 59 | ``` 60 | 61 | 4.打印调用方法时的参数值: 62 | 63 | ```objective-c 64 | [ANYMethodLog logMethodWithClass:NSClassFromString(@"UIViewController") condition:^BOOL(SEL sel) { 65 | 66 | return [NSStringFromSelector(sel) isEqualToString:@"viewWillAppear:"]; 67 | 68 | } before:^(id target, SEL sel, NSArray *args, int deep) { 69 | 70 | NSLog(@"before target:%@ sel:%@ args:%@", target, NSStringFromSelector(sel), args); 71 | 72 | } after:nil]; 73 | ``` 74 | 75 | 5.打印某个方法调用前后的变化: 76 | 77 | ```objective-c 78 | [ANYMethodLog logMethodWithClass:NSClassFromString(@"ListController") condition:^BOOL(SEL sel) { 79 | 80 | return [NSStringFromSelector(sel) isEqualToString:@"changeBackground"]; 81 | 82 | } before:^(id target, SEL sel, NSArray *args, int deep) { 83 | 84 | NSLog(@"before background color:%@", [(ListController *)target view].backgroundColor); 85 | 86 | } after:^(id target, SEL sel, NSArray *args, NSTimeInterval interval, int deep, id retValue) { 87 | 88 | NSLog(@"after background color:%@", [(ListController *)target view].backgroundColor); 89 | 90 | }]; 91 | ``` 92 | 93 | 6.打印某个方法调用的耗时: 94 | 95 | ```objective-c 96 | [ANYMethodLog logMethodWithClass:NSClassFromString(@"ListController") condition:^BOOL(SEL sel) { 97 | 98 | return [NSStringFromSelector(sel) isEqualToString:@"changeBackground"]; 99 | 100 | } before:^(id target, SEL sel, NSArray *args, int deep) { 101 | 102 | 103 | } after:^(id target, SEL sel, NSArray *args, NSTimeInterval interval, int deep, id retValue) { 104 | 105 | NSLog(@"interval::%@", [@(interval) stringValue]); 106 | 107 | }]; 108 | ``` 109 | 110 | 7.追踪方法调用顺序: 111 | 112 | ```objective-c 113 | [ANYMethodLog logMethodWithClass:NSClassFromString(@"ListController") condition:^BOOL(SEL sel) { 114 | return YES; 115 | } before:^(id target, SEL sel, NSArray *args, int deep) { 116 | NSString *selector = NSStringFromSelector(sel); 117 | NSArray *selectorArrary = [selector componentsSeparatedByString:@":"]; 118 | selectorArrary = [selectorArrary filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"length > 0"]]; 119 | NSMutableString *selectorString = [NSMutableString new]; 120 | for (int i = 0; i < selectorArrary.count; i++) { 121 | [selectorString appendFormat:@"%@:%@ ", selectorArrary[i], args[i]]; 122 | } 123 | NSMutableString *deepString = [NSMutableString new]; 124 | for (int i = 0; i < deep; i++) { 125 | [deepString appendString:@"-"]; 126 | } 127 | NSLog(@"%@[%@ %@]", deepString , target, selectorString); 128 | } after:^(id target, SEL sel, NSArray *args, NSTimeInterval interval, int deep, id retValue) { 129 | NSMutableString *deepString = [NSMutableString new]; 130 | for (int i = 0; i < deep; i++) { 131 | [deepString appendString:@"-"]; 132 | } 133 | NSLog(@"%@ret:%@", deepString, retValue); 134 | }]; 135 | ``` 136 | 137 | ## TODO: 138 | 139 | + 解决真机上运行出现的问题。 (已完成) 140 | + 打印调用时的参数值。 (已完成) 141 | + 打印返回值。 (已完成) 142 | + 计算某个方法的耗时。 (已完成) 143 | 144 | ## 原理: 145 | 146 | 利用runtime交换方法的实现。动态创建新方法,在新方法里再调用原来的方法。现阶段还不是很完美地调用原来方法,在需要传参的方法会出现传参失败,在真机问题较多,在模拟器问题较少,在用的时候可以过滤掉需要传参的方法。 147 | 148 | 1.把原方法的 `IMP` 换成 `_objc_msgForward` ,使之触发 `forwardInvocation` 方法; 149 | 150 | 2.把方法 `forwardInvocation` 的 `IMP` 换成 `qhd_forwardInvocation` ; 151 | 152 | 3.创建一个新方法,`IMP` 就是原方法的原来的 `IMP`,那么只要在 `qhd_forwardInvocation` 调用新方法即可。那么就可以在 `qhd_forwardInvocation` 插入 log 。 153 | 154 | 欢迎提 Issues 和 Pull requests 155 | 156 | -------------------------------------------------------------------------------- /readme-en.md: -------------------------------------------------------------------------------- 1 | # ANYMethodLog - log any method call of object in Objective-C 2 | [中文](../master/README.md) 3 | 4 | Print any method in an Objective-C object with ANYMethodLog 5 | 6 | ![running show](Documentation/Images/running_show.gif) 7 | 8 | ## How to call: 9 | 10 | ```objective-c 11 | + (void)logMethodWithClass:(Class)aClass 12 | condition:(BOOL(^)(SEL sel)) condition 13 | before:(void(^)(id target, SEL sel, NSArray *args, int deep)) before 14 | after:(void(^)(id target, SEL sel, NSArray *args, NSTimeInterval interval, int deep, id retValue)) after; 15 | ``` 16 | 17 | aClass:The class to be logged 18 | 19 | condition:Whether or not to track the method according to this block (sel is the method name) 20 | 21 | before:block will be called before the method (target is the detected object, sel is the method name, args is the parameter list, and deep is the calling level) 22 | 23 | after:block will be called after the method (interval is time spent executing the method, and retValue is the return value) 24 | 25 | ## Functionality: 26 | 1. Print all methods defined by a class, including public and private methods: 27 | 28 | ```objective-c 29 | [ANYMethodLog logMethodWithClass:[UIViewController class] condition:^BOOL(SEL sel) { 30 | NSLog(@"method:%@", NSStringFromSelector(sel)); 31 | return NO; 32 | } before:nil after:nil]; 33 | ``` 34 | 35 | 2. Print which methods are called during the run: 36 | 37 | ```objective-c 38 | [ANYMethodLog logMethodWithClass:[UIViewController class] condition:^BOOL(SEL sel) { 39 | return YES; 40 | } before:^(id target, SEL sel, NSArray *args, int deep) { 41 | NSLog(@"target:%@ sel:%@", target, NSStringFromSelector(sel)); 42 | } after:nil]; 43 | ``` 44 | 45 | 3. Print the calling sequence of specific methods: 46 | 47 | ```objective-c 48 | [ANYMethodLog logMethodWithClass:[UIViewController class] condition:^BOOL(SEL sel) { 49 | 50 | NSArray *whiteList = @[@"loadView", @"viewWillAppear:", @"viewDidAppear:", @"viewWillDisappear:", @"viewDidDisappear:", @"viewWillLayoutSubviews", @"viewDidLayoutSubviews"]; 51 | return [whiteList containsObject:NSStringFromSelector(sel)]; 52 | 53 | } before:^(id target, SEL sel, NSArray *args, int deep) { 54 | 55 | NSLog(@"target:%@ sel:%@", target, NSStringFromSelector(sel)); 56 | 57 | } after:nil]; 58 | ``` 59 | 60 | 4. Print the parameter values when calling the method: 61 | 62 | ```objective-c 63 | [ANYMethodLog logMethodWithClass:NSClassFromString(@"UIViewController") condition:^BOOL(SEL sel) { 64 | 65 | return [NSStringFromSelector(sel) isEqualToString:@"viewWillAppear:"]; 66 | 67 | } before:^(id target, SEL sel, NSArray *args, int deep) { 68 | 69 | NSLog(@"before target:%@ sel:%@ args:%@", target, NSStringFromSelector(sel), args); 70 | 71 | } after:nil]; 72 | ``` 73 | 74 | 5. Print the changes before and after a method call: 75 | 76 | ```objective-c 77 | [ANYMethodLog logMethodWithClass:NSClassFromString(@"ListController") condition:^BOOL(SEL sel) { 78 | 79 | return [NSStringFromSelector(sel) isEqualToString:@"changeBackground"]; 80 | 81 | } before:^(id target, SEL sel, NSArray *args, int deep) { 82 | 83 | NSLog(@"before background color:%@", [(ListController *)target view].backgroundColor); 84 | 85 | } after:^(id target, SEL sel, NSArray *args, NSTimeInterval interval, int deep, id retValue) { 86 | 87 | NSLog(@"after background color:%@", [(ListController *)target view].backgroundColor); 88 | 89 | }]; 90 | ``` 91 | 92 | 6. Print the time elapsed for a method call:: 93 | 94 | ```objective-c 95 | [ANYMethodLog logMethodWithClass:NSClassFromString(@"ListController") condition:^BOOL(SEL sel) { 96 | 97 | return [NSStringFromSelector(sel) isEqualToString:@"changeBackground"]; 98 | 99 | } before:^(id target, SEL sel, NSArray *args, int deep) { 100 | 101 | 102 | } after:^(id target, SEL sel, NSArray *args, NSTimeInterval interval, int deep, id retValue) { 103 | 104 | NSLog(@"interval::%@", [@(interval) stringValue]); 105 | 106 | }]; 107 | ``` 108 | 109 | 7. Trace the method call sequence:: 110 | 111 | ```objective-c 112 | [ANYMethodLog logMethodWithClass:NSClassFromString(@"ListController") condition:^BOOL(SEL sel) { 113 | return YES; 114 | } before:^(id target, SEL sel, NSArray *args, int deep) { 115 | NSString *selector = NSStringFromSelector(sel); 116 | NSArray *selectorArrary = [selector componentsSeparatedByString:@":"]; 117 | selectorArrary = [selectorArrary filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"length > 0"]]; 118 | NSMutableString *selectorString = [NSMutableString new]; 119 | for (int i = 0; i < selectorArrary.count; i++) { 120 | [selectorString appendFormat:@"%@:%@ ", selectorArrary[i], args[i]]; 121 | } 122 | NSMutableString *deepString = [NSMutableString new]; 123 | for (int i = 0; i < deep; i++) { 124 | [deepString appendString:@"-"]; 125 | } 126 | NSLog(@"%@[%@ %@]", deepString , target, selectorString); 127 | } after:^(id target, SEL sel, NSArray *args, NSTimeInterval interval, int deep, id retValue) { 128 | NSMutableString *deepString = [NSMutableString new]; 129 | for (int i = 0; i < deep; i++) { 130 | [deepString appendString:@"-"]; 131 | } 132 | NSLog(@"%@ret:%@", deepString, retValue); 133 | }]; 134 | ``` 135 | 136 | ## TODO: 137 | 138 | + Solving the problem of running on real machines. (completed) 139 | + Printing the parameter value at the time of the call. (completed) 140 | + Printing the return value. (completed) 141 | + Calculating the time taken for a method. (completed) 142 | 143 | ## Implementation: 144 | 145 | 1. Replace the `IMP` of the original method with `_objc_msgForward` to trigger the `forwardInvocation` method; 146 | 147 | 2. Replace the `IMP` of the method `forwardInvocation` with `qhd_forwardInvocation` 148 | 149 | 3. Create a new method, `IMP` overwrites the original method, then just call the new method in `qhd_forwardInvocation`. Then you can insert log in `qhd_forwardInvocation`. 150 | 151 | Issues and Pull requests are welcome 152 | 153 | --------------------------------------------------------------------------------