Created
October 29, 2010 07:54
-
-
Save holtwick/653110 to your computer and use it in GitHub Desktop.
Log current method and the arguments passed to that method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if DEBUG | |
#import <objc/runtime.h> | |
#import <objc/message.h> | |
#include <execinfo.h> | |
#include <stdio.h> | |
void dumpCall(id *__selfPtr, SEL __cmd) { | |
// Get argument stack | |
id __self = *__selfPtr; | |
void *stack = __selfPtr; | |
stack += sizeof(__self) + sizeof(__cmd); | |
// Prepare Method info | |
Method method = class_getInstanceMethod([__self class], __cmd); | |
NSArray *parts = [NSStringFromSelector(method_getName(method)) componentsSeparatedByString:@":"]; | |
NSString *output = [NSString stringWithFormat:@"* -[%@", NSStringFromClass([__self class])]; | |
// Loop arguments | |
for(unsigned i = 2; i < method_getNumberOfArguments(method); i++) { | |
char *argtype = method_copyArgumentType(method, i); | |
// Object | |
if(strlen(argtype) == 1) { | |
if(argtype[0] == '@') { | |
id o = (id)*(Handle)stack; | |
if([o isKindOfClass:[NSString class]]) { | |
output = [output stringByAppendingFormat:@" %@:@%@", [parts objectAtIndex:i - 2], [o repr]]; | |
} else { | |
output = [output stringByAppendingFormat:@" %@:%@", [parts objectAtIndex:i - 2], o]; | |
} | |
stack += sizeof(o); | |
} | |
else if(argtype[0] == 'i') { | |
int *v = stack; | |
output = [output stringByAppendingFormat:@" %@:%d", [parts objectAtIndex:i - 2], *v]; | |
stack += sizeof(int); | |
} | |
// ... | |
else { | |
output = [output stringByAppendingFormat:@" %@:***fail at '%s' ***", [parts objectAtIndex:i - 2], argtype]; | |
break; | |
} | |
} else { | |
if(strcmp(argtype, "{CGPoint=") > 0) { | |
CGPoint *v = (CGPoint *)(Handle)stack; | |
output = [output stringByAppendingFormat:@" %@:%@", [parts objectAtIndex:i - 2], NSStringFromCGPoint(*v)]; | |
stack += sizeof(CGPoint); | |
} | |
// ... | |
else { | |
output = [output stringByAppendingFormat:@" %@:***fail at '%s' ***", [parts objectAtIndex:i - 2], argtype]; | |
break; | |
} | |
} | |
free(argtype); | |
} | |
NSLog(@"%@]", output); | |
} | |
#define XPingFull dumpCall(&self, _cmd); | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Inspired by http://stackoverflow.com/questions/1797964/how-to-pass-all-arguments-of-a-method-into-nslog/1799472#1799472 | |
#if DEBUG | |
#import <objc/runtime.h> | |
#import <objc/message.h> | |
#include <execinfo.h> | |
#include <stdio.h> | |
void dumpCall(id *__selfPtr, SEL __cmd) { | |
// Get argument stack | |
id __self = *__selfPtr; | |
void *stack = __selfPtr; | |
stack += sizeof(__self) + sizeof(__cmd); | |
// Prepare Method info | |
Method method = class_getInstanceMethod([__self class], __cmd); | |
NSArray *parts = [NSStringFromSelector(method_getName(method)) componentsSeparatedByString:@":"]; | |
NSString *output = [NSString stringWithFormat:@"* -[%@", NSStringFromClass([__self class])]; | |
// Loop arguments | |
for(unsigned i = 2; i < method_getNumberOfArguments(method); i++) { | |
char *argtype = method_copyArgumentType(method, i); | |
// Object | |
if(strlen(argtype) == 1) { | |
if(argtype[0] == '@') { | |
id o = (id)*(Handle)stack; | |
if([o isKindOfClass:[NSString class]]) { | |
output = [output stringByAppendingFormat:@" %@:@%@", [parts objectAtIndex:i - 2], [o repr]]; | |
} else { | |
output = [output stringByAppendingFormat:@" %@:%@", [parts objectAtIndex:i - 2], o]; | |
} | |
stack += sizeof(o); | |
} | |
else if(argtype[0] == 'i') { | |
int *v = stack; | |
output = [output stringByAppendingFormat:@" %@:%d", [parts objectAtIndex:i - 2], *v]; | |
stack += sizeof(int); | |
} | |
// ... | |
else { | |
output = [output stringByAppendingFormat:@" %@:***fail at '%s' ***", [parts objectAtIndex:i - 2], argtype]; | |
break; | |
} | |
} else { | |
if(strcmp(argtype, "{CGPoint=") > 0) { | |
CGPoint *v = (CGPoint *)(Handle)stack; | |
output = [output stringByAppendingFormat:@" %@:%@", [parts objectAtIndex:i - 2], NSStringFromCGPoint(*v)]; | |
stack += sizeof(CGPoint); | |
} | |
// ... | |
else { | |
output = [output stringByAppendingFormat:@" %@:***fail at '%s' ***", [parts objectAtIndex:i - 2], argtype]; | |
break; | |
} | |
} | |
free(argtype); | |
} | |
NSLog(@"%@]", output); | |
} | |
#define XPingFull dumpCall(&self, _cmd); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great idea! Now how to put that into a macro?