Skip to content

Commit

Permalink
Not working on iOS devices. Added workaround to avoid crashes.
Browse files Browse the repository at this point in the history
  • Loading branch information
holtwick committed Oct 31, 2010
1 parent 3c704c0 commit 140c628
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 41 deletions.
18 changes: 16 additions & 2 deletions HOLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,25 @@
// Copyright 2010 holtwick.it. All rights reserved.
//

// TODO: Currently this Logger crashes on iOS devices!

#if TARGET_IPHONE_SIMULATOR

#import <objc/runtime.h>
#import <objc/message.h>
#include <execinfo.h>
#include <stdio.h>

NSString *__hoGetMethodCallWithArguments(id *__selfPtr, SEL __cmd);
NSString *HOGetMethodCallWithArguments(id *__selfPtr, SEL __cmd, char countCalls);

#define HOLogPing \
NSLog(@"%@", HOGetMethodCallWithArguments(&self, _cmd));

#else

// Fallback solution for iOS devices

#define HOLogPing \
NSLog(@"%s", __PRETTY_FUNCTION__);

#define HOLogPing NSLog(@"\n\n %@\n\n", __hoGetMethodCallWithArguments(&self, _cmd));
#endif
91 changes: 52 additions & 39 deletions HOLog.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#import "HOLog.h"

#if TARGET_IPHONE_SIMULATOR

#import <objc/runtime.h>
#import <objc/message.h>
#include <execinfo.h>
Expand All @@ -19,64 +21,75 @@
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 countCalls) {

// Get argument stack
id __self = *__selfPtr;
void *stack = __selfPtr;
stack += sizeof(__self) + sizeof(__cmd);

// Prepare Method info
// Prepare Method info
Method method = class_getInstanceMethod([__self class], __cmd);
NSArray *parts = [NSStringFromSelector(method_getName(method)) componentsSeparatedByString:@":"];
NSString *methodName = NSStringFromSelector(method_getName(method));
NSString *output = [NSString stringWithFormat:@"-[%@", NSStringFromClass([__self class])];

// Loop arguments
for(unsigned i = 2; i < method_getNumberOfArguments(method); ++i) {
char *argtype = method_copyArgumentType(method, i);
int nargs = method_getNumberOfArguments(method);
if(nargs <= 2) {
// No arguments
output = [output stringByAppendingFormat:@" %@", methodName];

// NSLog(@"%s", argtype);
} else {
// Loop arguments
NSArray *parts = [methodName componentsSeparatedByString:@":"];

// 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);
for(unsigned i = 2; i < nargs; ++i) {
char *argtype = method_copyArgumentType(method, i);

}

_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\"")
// 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 repr]];
} 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;
}

_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);
}
}

output = [NSString stringWithFormat:@"%@]", output];
return output;
}

#undef _testStruct
#undef _testType

#endif
2 changes: 2 additions & 0 deletions HOLogDemo/HOLogDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
28D7ACF70DDB3853001CB0EB /* HOLogDemoViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HOLogDemoViewController.m; sourceTree = "<group>"; };
29B97316FDCFA39411CA2CEA /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
32CA4F630368D1EE00C91783 /* HOLogDemo_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HOLogDemo_Prefix.pch; sourceTree = "<group>"; };
4321FD8C127DA55100130ECC /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = README.md; path = ../README.md; sourceTree = SOURCE_ROOT; };
4399ABD5127ADAB800BFD49D /* HOLog.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = HOLog.h; path = ../HOLog.h; sourceTree = SOURCE_ROOT; };
4399ABD6127ADAB800BFD49D /* HOLog.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = HOLog.m; path = ../HOLog.m; sourceTree = SOURCE_ROOT; };
8D1107310486CEB800E47090 /* HOLogDemo-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "HOLogDemo-Info.plist"; plistStructureDefinitionIdentifier = "com.apple.xcode.plist.structure-definition.iphone.info-plist"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -72,6 +73,7 @@
29B97314FDCFA39411CA2CEA /* CustomTemplate */ = {
isa = PBXGroup;
children = (
4321FD8C127DA55100130ECC /* README.md */,
4399ABD5127ADAB800BFD49D /* HOLog.h */,
4399ABD6127ADAB800BFD49D /* HOLog.m */,
080E96DDFE201D6D7F000001 /* Classes */,
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ Examples output:

-[HOLogDemoViewController exampleMethod:@a string a1:(int)42 a2:(CGPoint){12, 21} a3:(double)1.230000 a4:(char)1]

Known Bugs
----------

- Just works on iOS Simulator, not on actual devices. Help is very appreciated!

Useful Links and Credits
------------------------

Expand Down

0 comments on commit 140c628

Please sign in to comment.