Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate from ASL to OSLog #192

Merged
merged 7 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 74 additions & 70 deletions GoogleUtilities/AppDelegateSwizzler/GULAppDelegateSwizzler.m
Original file line number Diff line number Diff line change
Expand Up @@ -216,27 +216,27 @@ + (GULAppDelegateInterceptorID)registerAppDelegateInterceptor:
@"AppDelegateProxy interceptor does not conform to UIApplicationDelegate");

if (!interceptor) {
GULLogError(kGULLoggerSwizzler, NO,
[NSString stringWithFormat:@"I-SWZ%06ld",
(long)kGULSwizzlerMessageCodeAppDelegateSwizzling000],
@"AppDelegateProxy cannot add nil interceptor.");
GULOSLogError(kGULLogSubsystem, kGULLoggerSwizzler, NO,
[NSString stringWithFormat:@"I-SWZ%06ld",
(long)kGULSwizzlerMessageCodeAppDelegateSwizzling000],
@"AppDelegateProxy cannot add nil interceptor.");
return nil;
}
if (![interceptor conformsToProtocol:@protocol(GULApplicationDelegate)]) {
GULLogError(kGULLoggerSwizzler, NO,
[NSString stringWithFormat:@"I-SWZ%06ld",
(long)kGULSwizzlerMessageCodeAppDelegateSwizzling001],
@"AppDelegateProxy interceptor does not conform to UIApplicationDelegate");
GULOSLogError(kGULLogSubsystem, kGULLoggerSwizzler, NO,
[NSString stringWithFormat:@"I-SWZ%06ld",
(long)kGULSwizzlerMessageCodeAppDelegateSwizzling001],
@"AppDelegateProxy interceptor does not conform to UIApplicationDelegate");
return nil;
}

// The ID should be the same given the same interceptor object.
NSString *interceptorID = [NSString stringWithFormat:@"%@%p", kGULAppDelegatePrefix, interceptor];
if (!interceptorID.length) {
GULLogError(kGULLoggerSwizzler, NO,
[NSString stringWithFormat:@"I-SWZ%06ld",
(long)kGULSwizzlerMessageCodeAppDelegateSwizzling002],
@"AppDelegateProxy cannot create Interceptor ID.");
GULOSLogError(kGULLogSubsystem, kGULLoggerSwizzler, NO,
[NSString stringWithFormat:@"I-SWZ%06ld",
(long)kGULSwizzlerMessageCodeAppDelegateSwizzling002],
@"AppDelegateProxy cannot create Interceptor ID.");
return nil;
}
GULZeroingWeakContainer *weakObject = [[GULZeroingWeakContainer alloc] init];
Expand All @@ -251,21 +251,21 @@ + (void)unregisterAppDelegateInterceptorWithID:(GULAppDelegateInterceptorID)inte
@"AppDelegateProxy cannot unregister empty interceptor ID.");

if (!interceptorID) {
GULLogError(kGULLoggerSwizzler, NO,
[NSString stringWithFormat:@"I-SWZ%06ld",
(long)kGULSwizzlerMessageCodeAppDelegateSwizzling003],
@"AppDelegateProxy cannot unregister empty interceptor ID.");
GULOSLogError(kGULLogSubsystem, kGULLoggerSwizzler, NO,
[NSString stringWithFormat:@"I-SWZ%06ld",
(long)kGULSwizzlerMessageCodeAppDelegateSwizzling003],
@"AppDelegateProxy cannot unregister empty interceptor ID.");
return;
}

GULZeroingWeakContainer *weakContainer = [GULAppDelegateSwizzler interceptors][interceptorID];
if (!weakContainer.object) {
GULLogError(kGULLoggerSwizzler, NO,
[NSString stringWithFormat:@"I-SWZ%06ld",
(long)kGULSwizzlerMessageCodeAppDelegateSwizzling004],
@"AppDelegateProxy cannot unregister interceptor that was not registered. "
"Interceptor ID %@",
interceptorID);
GULOSLogError(kGULLogSubsystem, kGULLoggerSwizzler, NO,
[NSString stringWithFormat:@"I-SWZ%06ld",
(long)kGULSwizzlerMessageCodeAppDelegateSwizzling004],
@"AppDelegateProxy cannot unregister interceptor that was not registered. "
"Interceptor ID %@",
interceptorID);
return;
}

Expand Down Expand Up @@ -343,25 +343,27 @@ + (nullable Class)createSubclassWithObject:(id<GULApplicationDelegate>)appDelega
[NSString stringWithFormat:@"%@-%@", classNameWithPrefix, [NSUUID UUID].UUIDString];

if (NSClassFromString(newClassName)) {
GULLogError(kGULLoggerSwizzler, NO,
[NSString stringWithFormat:@"I-SWZ%06ld",
(long)kGULSwizzlerMessageCodeAppDelegateSwizzling005],
@"Cannot create a proxy for App Delegate. Subclass already exists. Original Class: "
@"%@, subclass: %@",
NSStringFromClass(realClass), newClassName);
GULOSLogError(
kGULLogSubsystem, kGULLoggerSwizzler, NO,
[NSString
stringWithFormat:@"I-SWZ%06ld", (long)kGULSwizzlerMessageCodeAppDelegateSwizzling005],
@"Cannot create a proxy for App Delegate. Subclass already exists. Original Class: "
@"%@, subclass: %@",
NSStringFromClass(realClass), newClassName);
return nil;
}

// Register the new class as subclass of the real one. Do not allocate more than the real class
// size.
Class appDelegateSubClass = objc_allocateClassPair(realClass, newClassName.UTF8String, 0);
if (appDelegateSubClass == Nil) {
GULLogError(kGULLoggerSwizzler, NO,
[NSString stringWithFormat:@"I-SWZ%06ld",
(long)kGULSwizzlerMessageCodeAppDelegateSwizzling006],
@"Cannot create a proxy for App Delegate. Subclass already exists. Original Class: "
@"%@, subclass: Nil",
NSStringFromClass(realClass));
GULOSLogError(
kGULLogSubsystem, kGULLoggerSwizzler, NO,
[NSString
stringWithFormat:@"I-SWZ%06ld", (long)kGULSwizzlerMessageCodeAppDelegateSwizzling006],
@"Cannot create a proxy for App Delegate. Subclass already exists. Original Class: "
@"%@, subclass: Nil",
NSStringFromClass(realClass));
return nil;
}

Expand Down Expand Up @@ -436,25 +438,26 @@ + (nullable Class)createSubclassWithObject:(id<GULApplicationDelegate>)appDelega
// cannot have more ivars/properties than its superclass since it will cause an offset in memory
// that can lead to overwriting the isa of an object in the next frame.
if (class_getInstanceSize(realClass) != class_getInstanceSize(appDelegateSubClass)) {
GULLogError(kGULLoggerSwizzler, NO,
[NSString stringWithFormat:@"I-SWZ%06ld",
(long)kGULSwizzlerMessageCodeAppDelegateSwizzling007],
@"Cannot create subclass of App Delegate, because the created subclass is not the "
@"same size. %@",
NSStringFromClass(realClass));
GULOSLogError(
kGULLogSubsystem, kGULLoggerSwizzler, NO,
[NSString
stringWithFormat:@"I-SWZ%06ld", (long)kGULSwizzlerMessageCodeAppDelegateSwizzling007],
@"Cannot create subclass of App Delegate, because the created subclass is not the "
@"same size. %@",
NSStringFromClass(realClass));
NSAssert(NO, @"Classes must be the same size to swizzle isa");
return nil;
}

// Make the newly created class to be the subclass of the real App Delegate class.
objc_registerClassPair(appDelegateSubClass);
if (object_setClass(appDelegate, appDelegateSubClass)) {
GULLogDebug(kGULLoggerSwizzler, NO,
[NSString stringWithFormat:@"I-SWZ%06ld",
(long)kGULSwizzlerMessageCodeAppDelegateSwizzling008],
@"Successfully created App Delegate Proxy automatically. To disable the "
@"proxy, set the flag %@ to NO (Boolean) in the Info.plist",
[GULAppDelegateSwizzler correctAppDelegateProxyKey]);
GULOSLogDebug(kGULLogSubsystem, kGULLoggerSwizzler, NO,
[NSString stringWithFormat:@"I-SWZ%06ld",
(long)kGULSwizzlerMessageCodeAppDelegateSwizzling008],
@"Successfully created App Delegate Proxy automatically. To disable the "
@"proxy, set the flag %@ to NO (Boolean) in the Info.plist",
[GULAppDelegateSwizzler correctAppDelegateProxyKey]);
}

return appDelegateSubClass;
Expand Down Expand Up @@ -604,11 +607,12 @@ + (void)addInstanceMethodWithDestinationSelector:(SEL)destinationSelector
IMP methodIMP = method_getImplementation(method);
const char *types = method_getTypeEncoding(method);
if (!class_addMethod(toClass, destinationSelector, methodIMP, types)) {
GULLogWarning(kGULLoggerSwizzler, NO,
[NSString stringWithFormat:@"I-SWZ%06ld",
(long)kGULSwizzlerMessageCodeAppDelegateSwizzling009],
@"Cannot copy method to destination selector %@ as it already exists",
NSStringFromSelector(destinationSelector));
GULOSLogWarning(
kGULLogSubsystem, kGULLoggerSwizzler, NO,
[NSString
stringWithFormat:@"I-SWZ%06ld", (long)kGULSwizzlerMessageCodeAppDelegateSwizzling009],
@"Cannot copy method to destination selector %@ as it already exists",
NSStringFromSelector(destinationSelector));
}
}

Expand Down Expand Up @@ -640,8 +644,8 @@ + (void)notifyInterceptorsWithMethodSelector:(SEL)methodSelector
GULZeroingWeakContainer *interceptorContainer = obj;
id interceptor = interceptorContainer.object;
if (!interceptor) {
GULLogWarning(
kGULLoggerSwizzler, NO,
GULOSLogWarning(
kGULLogSubsystem, kGULLoggerSwizzler, NO,
[NSString
stringWithFormat:@"I-SWZ%06ld", (long)kGULSwizzlerMessageCodeAppDelegateSwizzling010],
@"AppDelegateProxy cannot find interceptor with ID %@. Removing the interceptor.", key);
Expand Down Expand Up @@ -943,8 +947,8 @@ + (nullable NSInvocation *)appDelegateInvocationForSelector:(SEL)selector {

+ (void)proxyAppDelegate:(id<GULApplicationDelegate>)appDelegate {
if (![appDelegate conformsToProtocol:@protocol(GULApplicationDelegate)]) {
GULLogNotice(
kGULLoggerSwizzler, NO,
GULOSLogNotice(
kGULLogSubsystem, kGULLoggerSwizzler, NO,
[NSString
stringWithFormat:@"I-SWZ%06ld",
(long)kGULSwizzlerMessageCodeAppDelegateSwizzlingInvalidAppDelegate],
Expand All @@ -956,20 +960,20 @@ + (void)proxyAppDelegate:(id<GULApplicationDelegate>)appDelegate {
id<GULApplicationDelegate> originalDelegate = appDelegate;
// Do not create a subclass if it is not enabled.
if (![GULAppDelegateSwizzler isAppDelegateProxyEnabled]) {
GULLogNotice(kGULLoggerSwizzler, NO,
[NSString stringWithFormat:@"I-SWZ%06ld",
(long)kGULSwizzlerMessageCodeAppDelegateSwizzling011],
@"App Delegate Proxy is disabled. %@",
[GULAppDelegateSwizzler correctAlternativeWhenAppDelegateProxyNotCreated]);
GULOSLogNotice(kGULLogSubsystem, kGULLoggerSwizzler, NO,
[NSString stringWithFormat:@"I-SWZ%06ld",
(long)kGULSwizzlerMessageCodeAppDelegateSwizzling011],
@"App Delegate Proxy is disabled. %@",
[GULAppDelegateSwizzler correctAlternativeWhenAppDelegateProxyNotCreated]);
return;
}
// Do not accept nil delegate.
if (!originalDelegate) {
GULLogError(kGULLoggerSwizzler, NO,
[NSString stringWithFormat:@"I-SWZ%06ld",
(long)kGULSwizzlerMessageCodeAppDelegateSwizzling012],
@"Cannot create App Delegate Proxy because App Delegate instance is nil. %@",
[GULAppDelegateSwizzler correctAlternativeWhenAppDelegateProxyNotCreated]);
GULOSLogError(kGULLogSubsystem, kGULLoggerSwizzler, NO,
[NSString stringWithFormat:@"I-SWZ%06ld",
(long)kGULSwizzlerMessageCodeAppDelegateSwizzling012],
@"Cannot create App Delegate Proxy because App Delegate instance is nil. %@",
[GULAppDelegateSwizzler correctAlternativeWhenAppDelegateProxyNotCreated]);
return;
}

Expand All @@ -978,11 +982,11 @@ + (void)proxyAppDelegate:(id<GULApplicationDelegate>)appDelegate {
gAppDelegateSubclass = [self createSubclassWithObject:originalDelegate];
[self reassignAppDelegate];
} @catch (NSException *exception) {
GULLogError(kGULLoggerSwizzler, NO,
[NSString stringWithFormat:@"I-SWZ%06ld",
(long)kGULSwizzlerMessageCodeAppDelegateSwizzling013],
@"Cannot create App Delegate Proxy. %@",
[GULAppDelegateSwizzler correctAlternativeWhenAppDelegateProxyNotCreated]);
GULOSLogError(kGULLogSubsystem, kGULLoggerSwizzler, NO,
[NSString stringWithFormat:@"I-SWZ%06ld",
(long)kGULSwizzlerMessageCodeAppDelegateSwizzling013],
@"Cannot create App Delegate Proxy. %@",
[GULAppDelegateSwizzler correctAlternativeWhenAppDelegateProxyNotCreated]);
return;
}
}
Expand Down
Loading
Loading