Skip to content
Browse files

Fixing block and device errors and added a fallback output

  • Loading branch information...
1 parent 726e244 commit 8ab2776dbd51b16838390346ba8a639b71677244 @holtwick committed Nov 9, 2010
Showing with 87 additions and 57 deletions.
  1. +2 −2 HOLog.h
  2. +72 −55 HOLog.m
  3. +13 −0 HOLogDemo/Classes/HOLogDemoViewController.m
View
4 HOLog.h
@@ -11,10 +11,10 @@
#include <execinfo.h>
#include <stdio.h>
-NSString *HOGetMethodCallWithArguments(id *__selfPtr, SEL __cmd);
+NSString *HOGetMethodCallWithArguments(id *__selfPtr, SEL __cmd, char *fallback);
#define HOLogPing \
- NSLog(@"%@", HOGetMethodCallWithArguments(&self, _cmd));
+ NSLog(@"%@", HOGetMethodCallWithArguments(&self, _cmd, (char *)__PRETTY_FUNCTION__));
#else
View
127 HOLog.m
@@ -21,71 +21,88 @@
output = [output stringByAppendingFormat:@" %@:(%s)" fmt, [parts objectAtIndex:i - 2], #type, *v]; \
stack += sizeof(type); }
-NSString *HOGetMethodCallWithArguments(id *__selfPtr, SEL __cmd) {
+NSString *HOGetMethodCallWithArguments(id *__selfPtr, SEL __cmd, char *fallback) {
- // Get argument stack
- id __self = *__selfPtr;
- void *stack = __selfPtr;
- stack += sizeof(__self) + sizeof(__cmd);
+ NSString *fallbackString = [NSString stringWithFormat:@"%s", fallback];
- // Prepare Method info
- Method method = class_getInstanceMethod([__self class], __cmd);
- NSString *methodName = NSStringFromSelector(method_getName(method));
- NSString *output = [NSString stringWithFormat:@"-[%@", NSStringFromClass([__self class])];
+ // If called inside a block we will not be able to get the arguments correctly, therefore fall back
+ if([fallbackString hasPrefix:@"__-"]) {
+ return fallbackString;
+ }
- int nargs = method_getNumberOfArguments(method);
- if(nargs <= 2) {
- // No arguments
- output = [output stringByAppendingFormat:@" %@", methodName];
+ @try {
- } else {
- // Loop arguments
- NSArray *parts = [methodName componentsSeparatedByString:@":"];
+ // Get argument stack
+ id __self = *__selfPtr;
+ void *stack = __selfPtr;
+ stack += sizeof(__self) + sizeof(__cmd);
- for(unsigned i = 2; i < nargs; ++i) {
- char *argtype = method_copyArgumentType(method, i);
-
- // NSLog(@"%s", argtype);
-
- // Object
- if(strcmp(argtype, @encode(id)) == 0) {
- id o = (id)*(Handle)stack;
- if([o isKindOfClass:[NSString class]]) {
- output = [output stringByAppendingFormat:@" %@:@\"%@\"", [parts objectAtIndex:i - 2], [o description]];
- } else {
- output = [output stringByAppendingFormat:@" %@:%@", [parts objectAtIndex:i - 2], o];
- }
- stack += sizeof(o);
- }
-
- _testType(int, @"%d")
- _testType(unsigned int, @"%u")
- _testType(short, @"%hi")
- _testType(unsigned short, @"%hu")
- _testType(long long, @"%qi")
- _testType(unsigned long long, @"%qu")
- _testType(float, @"%f")
- _testType(double, @"%f")
- _testType(char, @"%d")
- _testType(char *, @"\"%s\"")
+ // Prepare Method info
+ Method method = class_getInstanceMethod([__self class], __cmd);
+ NSString *methodName = NSStringFromSelector(method_getName(method));
+ NSString *output = [NSString stringWithFormat:@"-[%@", NSStringFromClass([__self class])];
+
+ int nargs = method_getNumberOfArguments(method);
+ if(nargs <= 2) {
+ // No arguments
+ output = [output stringByAppendingFormat:@" %@", methodName];
- _testStruct(CGPoint, NSStringFromCGPoint)
- _testStruct(CGRect, NSStringFromCGRect)
- _testStruct(CGSize, NSStringFromCGSize)
- _testStruct(NSRange, NSStringFromRange)
+ } else {
+ // Loop arguments
+ NSArray *parts = [methodName componentsSeparatedByString:@":"];
- else {
- output = [output stringByAppendingFormat:@" %@:***not supported '%s' | BREAKING HERE! ***", [parts objectAtIndex:i - 2], argtype];
+ for(unsigned i = 2; i < nargs; ++i) {
+ char *argtype = method_copyArgumentType(method, i);
+
+ // NSLog(@"%s", argtype);
+
+ // Object
+ if(strcmp(argtype, @encode(id)) == 0) {
+ id o = (id)*(Handle)stack;
+ if([o isKindOfClass:[NSString class]]) {
+ output = [output stringByAppendingFormat:@" %@:@\"%@\"", [parts objectAtIndex:i - 2], [o description]];
+ } else {
+ output = [output stringByAppendingFormat:@" %@:%@", [parts objectAtIndex:i - 2], o];
+ }
+ stack += sizeof(o);
+ }
+
+ _testType(int, @"%d")
+ _testType(unsigned int, @"%u")
+ _testType(short, @"%hi")
+ _testType(unsigned short, @"%hu")
+ _testType(long long, @"%qi")
+ _testType(unsigned long long, @"%qu")
+ _testType(float, @"%f")
+ _testType(double, @"%f")
+ _testType(char, @"%d")
+ _testType(char *, @"\"%s\"")
+
+ _testStruct(CGPoint, NSStringFromCGPoint)
+ _testStruct(CGRect, NSStringFromCGRect)
+ _testStruct(CGSize, NSStringFromCGSize)
+ _testStruct(NSRange, NSStringFromRange)
+
+ else {
+ output = [output stringByAppendingFormat:@" %@:***not supported '%s' | BREAKING HERE! ***", [parts objectAtIndex:i - 2], argtype];
+ free(argtype);
+ break;
+ }
+
free(argtype);
- break;
}
-
- free(argtype);
- }
- }
+ }
+
+ output = [NSString stringWithFormat:@"%@]", output];
+ return output;
+
+ } @catch(NSException * e) {
+ ;
+ }
+
+ // If something wen't wrong we still get an informative feedback
+ return fallbackString;
- output = [NSString stringWithFormat:@"%@]", output];
- return output;
}
#undef _testStruct
View
13 HOLogDemo/Classes/HOLogDemoViewController.m
@@ -11,6 +11,10 @@
@implementation HOLogDemoViewController
+- (void)exampleInt:(int)v {
+ HOLogPing
+}
+
- (void)exampleMethod:(id)obj
a1:(int)a1
a2:(CGPoint)a2
@@ -28,13 +32,22 @@ - (void)exampleMethod:(id)obj
- (void)viewDidLoad {
[super viewDidLoad];
+ [self exampleInt:1];
+
// Test call
[self exampleMethod:@"a string"
a1:42
a2:CGPointMake(12, 21)
a3:1.23
a4:YES
];
+
+ ^(id x) {
+
+ // Will fail
+ HOLogPing
+
+ }(nil);
}
@end

0 comments on commit 8ab2776

Please sign in to comment.
Something went wrong with that request. Please try again.