Last active
September 13, 2015 17:15
-
-
Save swillits/a1754f42c81b93423ca6 to your computer and use it in GitHub Desktop.
This file contains 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
// | |
// Probie.m | |
// Probie | |
// | |
// Created by Seth Willits, Keith Bauer on 11/19/14. | |
// Copyright (c) 2014 Araelium Group. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
#import <objc/runtime.h> | |
template<class C, class R, class... Args> | |
void probie(SEL s, double maxTimeBeforeLogging = 0.0); | |
// --------------------------------------------------------------------------------- | |
// --------------------------------------------------------------------------------- | |
#if ENABLE_PROBIE || DEBUG | |
#import "Foo.h" | |
__attribute__((constructor)) | |
static void InstallProbieProbes() | |
{ | |
probie<Foo, void>(@selector(test1), 0.1); | |
probie<Foo, int>(@selector(test2)); | |
} | |
#endif | |
// --------------------------------------------------------------------------------- | |
// --------------------------------------------------------------------------------- | |
struct probie_timer | |
{ | |
probie_timer(Class cls, SEL selector, double maxTimeBeforeLogging) : | |
_class(cls), | |
_selector(selector), | |
_maxTimeBeforeLogging(maxTimeBeforeLogging), | |
_start([NSDate timeIntervalSinceReferenceDate]) | |
{} | |
~probie_timer() { | |
double t = [NSDate timeIntervalSinceReferenceDate] - _start; | |
if (t > _maxTimeBeforeLogging) { | |
NSLog(@"PROBIE -[%@ %@]: %.4f sec", NSStringFromClass(_class), NSStringFromSelector(_selector), t); | |
} | |
} | |
private: | |
Class _class; | |
SEL _selector; | |
NSTimeInterval _maxTimeBeforeLogging; | |
NSTimeInterval _start; | |
}; | |
template<class C, class R, class... Args> | |
void probie(SEL s, double maxTimeBeforeLogging) | |
{ | |
Method orig = class_getInstanceMethod([C class], s); | |
IMP imp = method_getImplementation(orig); | |
auto block = [=](C *self, Args... args) -> R { | |
probie_timer t([C class], s, maxTimeBeforeLogging); | |
return ((R (*)(id, SEL, Args...))imp)(self, s, args...); | |
}; | |
IMP new_imp = imp_implementationWithBlock(block); | |
method_setImplementation(orig, new_imp); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment