33#import < Cocoa/Cocoa.h>
44#import < objc/runtime.h>
55#import < jni.h>
6+ #import < os/log.h>
67
78@class DPDelegateProxy; // Forward declaration
89
1314
1415#pragma mark - Helpers
1516
17+ static os_log_t getLog (void ) {
18+ static os_log_t log = NULL ;
19+ static dispatch_once_t onceToken;
20+ dispatch_once (&onceToken, ^{
21+ log = os_log_create (" com.diffplug.deeplink" , " DeepLinkBridge" );
22+ });
23+ return log;
24+ }
25+
1626static void abortWithMessage (NSString *message) {
17- NSLog (@" [DeepLink] FATAL: %@ " , message);
18- fflush (stdout); // Ensure message is printed before crash
27+ os_log_fault (getLog (), " FATAL: %{public}@" , message);
1928
2029 // Most aggressive crash - direct null pointer dereference
2130 // This causes SIGSEGV which is very hard to catch
@@ -31,26 +40,26 @@ static void abortWithMessage(NSString *message) {
3140
3241JNIEXPORT jint JNICALL JNI_OnLoad (JavaVM *vm, void *reserved) {
3342 gJVM = vm;
34- NSLog ( @" [DeepLink] JNI_OnLoad: JavaVM stored" );
43+ os_log_info ( getLog (), " JNI_OnLoad: JavaVM stored" );
3544 return JNI_VERSION_1_6;
3645}
3746
3847JNIEXPORT void JNICALL JNI_OnUnload (JavaVM *vm, void *reserved) {
39- NSLog ( @" [DeepLink] JNI_OnUnload: Cleaning up" );
48+ os_log_info ( getLog (), " JNI_OnUnload: Cleaning up" );
4049
4150 // Deregister Apple Event handler
4251 [[NSAppleEventManager sharedAppleEventManager ]
4352 removeEventHandlerForEventClass: kInternetEventClass
4453 andEventID: kAEGetURL ];
45- NSLog ( @" [DeepLink] Removed Apple Event handler" );
54+ os_log_info ( getLog (), " Removed Apple Event handler" );
4655
4756 // Restore original delegate before releasing proxy
4857 if (gDelegateProxy && NSApp ) {
4958 // Access the original delegate directly via ivar
5059 Ivar ivar = class_getInstanceVariable (object_getClass (gDelegateProxy ), " _realDelegate" );
5160 id originalDelegate = object_getIvar (gDelegateProxy , ivar);
5261 [NSApp setDelegate: originalDelegate];
53- NSLog ( @" [DeepLink] Restored original delegate" );
62+ os_log_info ( getLog (), " Restored original delegate" );
5463 }
5564
5665 // Clean up global reference
@@ -59,7 +68,7 @@ JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) {
5968 if ((*vm)->GetEnv (vm, (void **)&env, JNI_VERSION_1_6) == JNI_OK) {
6069 (*env)->DeleteGlobalRef (env, gHandlerClass );
6170 gHandlerClass = NULL ;
62- NSLog ( @" [DeepLink] Released global class reference" );
71+ os_log_info ( getLog (), " Released global class reference" );
6372 }
6473 }
6574
@@ -87,7 +96,7 @@ JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) {
8796}
8897
8998static void deliverToJava (NSString *s) {
90- NSLog ( @" [DeepLink] deliverToJava called with URL " );
99+ os_log_debug ( getLog (), " deliverToJava called" );
91100 // These should never be null since we control registration timing
92101 if (!gHandlerClass || !gDeliverMID ) {
93102 abortWithMessage (@" JNI handler not initialized - applicationStartBeforeSwt must be called first" );
@@ -103,10 +112,10 @@ static void deliverToJava(NSString *s) {
103112 if (utf8) {
104113 jstring jstr = (*env)->NewStringUTF (env, utf8);
105114 if (jstr) {
106- NSLog ( @" [DeepLink] Calling Java deliverURL" );
115+ os_log_debug ( getLog (), " Calling Java deliverURL" );
107116 (*env)->CallStaticVoidMethod (env, gHandlerClass , gDeliverMID , jstr);
108117 if ((*env)->ExceptionCheck (env)) {
109- NSLog ( @" [DeepLink] Java exception occurred! " );
118+ os_log_error ( getLog (), " Java exception occurred" );
110119 (*env)->ExceptionDescribe (env);
111120 (*env)->ExceptionClear (env);
112121 }
@@ -137,7 +146,7 @@ + (instancetype)sharedHandler {
137146
138147- (void )handleGetURL : (NSAppleEventDescriptor *)event withReply : (NSAppleEventDescriptor *)reply {
139148 NSString *urlString = [[event paramDescriptorForKeyword: keyDirectObject] stringValue ];
140- NSLog ( @" [DeepLink] Apple Event received URL " );
149+ os_log_debug ( getLog (), " Apple Event received" );
141150 if (urlString.length ) {
142151 deliverToJava (urlString);
143152 }
@@ -148,7 +157,7 @@ - (void)handleGetURL:(NSAppleEventDescriptor *)event withReply:(NSAppleEventDesc
148157// Install Apple Event handler when Java is ready
149158static void installEarlyAEHandler (void ) {
150159 @autoreleasepool {
151- NSLog ( @" [DeepLink] Installing Apple Event handler" );
160+ os_log_info ( getLog (), " Installing Apple Event handler" );
152161
153162 // Register handler for kAEGetURL events
154163 [[NSAppleEventManager sharedAppleEventManager ]
@@ -157,7 +166,7 @@ static void installEarlyAEHandler(void) {
157166 forEventClass: kInternetEventClass
158167 andEventID: kAEGetURL ];
159168
160- NSLog ( @" [DeepLink] Apple Event handler installed" );
169+ os_log_info ( getLog (), " Apple Event handler installed" );
161170 }
162171}
163172
@@ -196,11 +205,11 @@ - (BOOL)respondsToSelector:(SEL)sel {
196205}
197206
198207- (void )application : (NSApplication *)app openURLs : (NSArray <NSURL *> *)urls {
199- NSLog ( @" [DeepLink] DPDelegateProxy application:openURLs: received %lu URLs " , (unsigned long )urls.count );
208+ os_log_debug ( getLog (), " DPDelegateProxy received %lu URL(s) " , (unsigned long )urls.count );
200209 for (NSURL *u in urls) {
201210 if (!u) continue ;
202211 NSString *s = u.absoluteString ;
203- NSLog ( @" [DeepLink] DPDelegateProxy processing URL" );
212+ os_log_debug ( getLog (), " Processing URL" );
204213 if (s.length ) deliverToJava (s);
205214 }
206215}
@@ -212,20 +221,20 @@ - (void)application:(NSApplication *)app openURLs:(NSArray<NSURL *> *)urls {
212221JNIEXPORT void JNICALL Java_com_diffplug_common_swt_widgets_MacDeepLink_nativeBeforeSwt
213222 (JNIEnv *env, jclass clazz) {
214223
215- NSLog ( @" [DeepLink] nativeBeforeSwt called from Java" );
224+ os_log_info ( getLog (), " nativeBeforeSwt called from Java" );
216225
217226 // Cache class & method (global ref so it survives)
218227 if (!gHandlerClass ) {
219228 gHandlerClass = (*env)->NewGlobalRef (env, clazz);
220- NSLog ( @" [DeepLink] Cached Java class reference" );
229+ os_log_debug ( getLog (), " Cached Java class reference" );
221230 }
222231 if (!gDeliverMID ) {
223232 gDeliverMID = (*env)->GetStaticMethodID (env, gHandlerClass , " deliverURL" , " (Ljava/lang/String;)V" );
224233 if (!gDeliverMID ) {
225- NSLog ( @" [DeepLink] ERROR: Could not find deliverURL method! " );
234+ os_log_error ( getLog (), " Could not find deliverURL method" );
226235 return ;
227236 }
228- NSLog ( @" [DeepLink] Cached deliverURL method ID" );
237+ os_log_debug ( getLog (), " Cached deliverURL method ID" );
229238 }
230239
231240 // Now that JNI is ready, register with macOS for Apple Events
@@ -236,18 +245,18 @@ - (void)application:(NSApplication *)app openURLs:(NSArray<NSURL *> *)urls {
236245JNIEXPORT void JNICALL Java_com_diffplug_common_swt_widgets_MacDeepLink_nativeAfterSwt
237246 (JNIEnv *env, jclass clazz) {
238247
239- NSLog ( @" [DeepLink] nativeAfterSwt called from Java" );
248+ os_log_info ( getLog (), " nativeAfterSwt called from Java" );
240249
241250 if (!NSApp ) {
242251 abortWithMessage (@" NSApp is nil! Make sure SWT Display is created first" );
243252 }
244253
245254 // Wrap the existing delegate with our proxy
246255 id current = [NSApp delegate ];
247- NSLog ( @" [DeepLink] Current NSApp delegate: %@ " , current);
256+ os_log_debug ( getLog (), " Current NSApp delegate: %{public} @" , NSStringFromClass ([ current class ]) );
248257
249258 // Store proxy in static to prevent deallocation (NSApp.delegate is weak)
250259 gDelegateProxy = [[DPDelegateProxy alloc ] initWithDelegate: current];
251260 [NSApp setDelegate: (id <NSApplicationDelegate >)gDelegateProxy ];
252- NSLog ( @" [DeepLink] Installed delegate proxy" );
261+ os_log_info ( getLog (), " Installed delegate proxy" );
253262}
0 commit comments