Skip to content

Instantly share code, notes, and snippets.

@holtwick
Created October 29, 2010 07:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save holtwick/653110 to your computer and use it in GitHub Desktop.
Save holtwick/653110 to your computer and use it in GitHub Desktop.
Log current method and the arguments passed to that method
#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
// 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
@futuretap
Copy link

Great idea! Now how to put that into a macro?

@holtwick
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment