3
3
#import < Cocoa/Cocoa.h>
4
4
#import < objc/runtime.h>
5
5
#import < jni.h>
6
+ #import < os/log.h>
6
7
7
8
@class DPDelegateProxy; // Forward declaration
8
9
13
14
14
15
#pragma mark - Helpers
15
16
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
+
16
26
static 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);
19
28
20
29
// Most aggressive crash - direct null pointer dereference
21
30
// This causes SIGSEGV which is very hard to catch
@@ -31,26 +40,26 @@ static void abortWithMessage(NSString *message) {
31
40
32
41
JNIEXPORT jint JNICALL JNI_OnLoad (JavaVM *vm, void *reserved) {
33
42
gJVM = vm;
34
- NSLog ( @" [DeepLink] JNI_OnLoad: JavaVM stored" );
43
+ os_log_info ( getLog (), " JNI_OnLoad: JavaVM stored" );
35
44
return JNI_VERSION_1_6;
36
45
}
37
46
38
47
JNIEXPORT void JNICALL JNI_OnUnload (JavaVM *vm, void *reserved) {
39
- NSLog ( @" [DeepLink] JNI_OnUnload: Cleaning up" );
48
+ os_log_info ( getLog (), " JNI_OnUnload: Cleaning up" );
40
49
41
50
// Deregister Apple Event handler
42
51
[[NSAppleEventManager sharedAppleEventManager ]
43
52
removeEventHandlerForEventClass: kInternetEventClass
44
53
andEventID: kAEGetURL ];
45
- NSLog ( @" [DeepLink] Removed Apple Event handler" );
54
+ os_log_info ( getLog (), " Removed Apple Event handler" );
46
55
47
56
// Restore original delegate before releasing proxy
48
57
if (gDelegateProxy && NSApp ) {
49
58
// Access the original delegate directly via ivar
50
59
Ivar ivar = class_getInstanceVariable (object_getClass (gDelegateProxy ), " _realDelegate" );
51
60
id originalDelegate = object_getIvar (gDelegateProxy , ivar);
52
61
[NSApp setDelegate: originalDelegate];
53
- NSLog ( @" [DeepLink] Restored original delegate" );
62
+ os_log_info ( getLog (), " Restored original delegate" );
54
63
}
55
64
56
65
// Clean up global reference
@@ -59,7 +68,7 @@ JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) {
59
68
if ((*vm)->GetEnv (vm, (void **)&env, JNI_VERSION_1_6) == JNI_OK) {
60
69
(*env)->DeleteGlobalRef (env, gHandlerClass );
61
70
gHandlerClass = NULL ;
62
- NSLog ( @" [DeepLink] Released global class reference" );
71
+ os_log_info ( getLog (), " Released global class reference" );
63
72
}
64
73
}
65
74
@@ -87,7 +96,7 @@ JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) {
87
96
}
88
97
89
98
static void deliverToJava (NSString *s) {
90
- NSLog ( @" [DeepLink] deliverToJava called with URL " );
99
+ os_log_debug ( getLog (), " deliverToJava called" );
91
100
// These should never be null since we control registration timing
92
101
if (!gHandlerClass || !gDeliverMID ) {
93
102
abortWithMessage (@" JNI handler not initialized - applicationStartBeforeSwt must be called first" );
@@ -103,10 +112,10 @@ static void deliverToJava(NSString *s) {
103
112
if (utf8) {
104
113
jstring jstr = (*env)->NewStringUTF (env, utf8);
105
114
if (jstr) {
106
- NSLog ( @" [DeepLink] Calling Java deliverURL" );
115
+ os_log_debug ( getLog (), " Calling Java deliverURL" );
107
116
(*env)->CallStaticVoidMethod (env, gHandlerClass , gDeliverMID , jstr);
108
117
if ((*env)->ExceptionCheck (env)) {
109
- NSLog ( @" [DeepLink] Java exception occurred! " );
118
+ os_log_error ( getLog (), " Java exception occurred" );
110
119
(*env)->ExceptionDescribe (env);
111
120
(*env)->ExceptionClear (env);
112
121
}
@@ -137,7 +146,7 @@ + (instancetype)sharedHandler {
137
146
138
147
- (void )handleGetURL : (NSAppleEventDescriptor *)event withReply : (NSAppleEventDescriptor *)reply {
139
148
NSString *urlString = [[event paramDescriptorForKeyword: keyDirectObject] stringValue ];
140
- NSLog ( @" [DeepLink] Apple Event received URL " );
149
+ os_log_debug ( getLog (), " Apple Event received" );
141
150
if (urlString.length ) {
142
151
deliverToJava (urlString);
143
152
}
@@ -148,7 +157,7 @@ - (void)handleGetURL:(NSAppleEventDescriptor *)event withReply:(NSAppleEventDesc
148
157
// Install Apple Event handler when Java is ready
149
158
static void installEarlyAEHandler (void ) {
150
159
@autoreleasepool {
151
- NSLog ( @" [DeepLink] Installing Apple Event handler" );
160
+ os_log_info ( getLog (), " Installing Apple Event handler" );
152
161
153
162
// Register handler for kAEGetURL events
154
163
[[NSAppleEventManager sharedAppleEventManager ]
@@ -157,7 +166,7 @@ static void installEarlyAEHandler(void) {
157
166
forEventClass: kInternetEventClass
158
167
andEventID: kAEGetURL ];
159
168
160
- NSLog ( @" [DeepLink] Apple Event handler installed" );
169
+ os_log_info ( getLog (), " Apple Event handler installed" );
161
170
}
162
171
}
163
172
@@ -196,11 +205,11 @@ - (BOOL)respondsToSelector:(SEL)sel {
196
205
}
197
206
198
207
- (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 );
200
209
for (NSURL *u in urls) {
201
210
if (!u) continue ;
202
211
NSString *s = u.absoluteString ;
203
- NSLog ( @" [DeepLink] DPDelegateProxy processing URL" );
212
+ os_log_debug ( getLog (), " Processing URL" );
204
213
if (s.length ) deliverToJava (s);
205
214
}
206
215
}
@@ -212,20 +221,20 @@ - (void)application:(NSApplication *)app openURLs:(NSArray<NSURL *> *)urls {
212
221
JNIEXPORT void JNICALL Java_com_diffplug_common_swt_widgets_MacDeepLink_nativeBeforeSwt
213
222
(JNIEnv *env, jclass clazz) {
214
223
215
- NSLog ( @" [DeepLink] nativeBeforeSwt called from Java" );
224
+ os_log_info ( getLog (), " nativeBeforeSwt called from Java" );
216
225
217
226
// Cache class & method (global ref so it survives)
218
227
if (!gHandlerClass ) {
219
228
gHandlerClass = (*env)->NewGlobalRef (env, clazz);
220
- NSLog ( @" [DeepLink] Cached Java class reference" );
229
+ os_log_debug ( getLog (), " Cached Java class reference" );
221
230
}
222
231
if (!gDeliverMID ) {
223
232
gDeliverMID = (*env)->GetStaticMethodID (env, gHandlerClass , " deliverURL" , " (Ljava/lang/String;)V" );
224
233
if (!gDeliverMID ) {
225
- NSLog ( @" [DeepLink] ERROR: Could not find deliverURL method! " );
234
+ os_log_error ( getLog (), " Could not find deliverURL method" );
226
235
return ;
227
236
}
228
- NSLog ( @" [DeepLink] Cached deliverURL method ID" );
237
+ os_log_debug ( getLog (), " Cached deliverURL method ID" );
229
238
}
230
239
231
240
// Now that JNI is ready, register with macOS for Apple Events
@@ -236,18 +245,18 @@ - (void)application:(NSApplication *)app openURLs:(NSArray<NSURL *> *)urls {
236
245
JNIEXPORT void JNICALL Java_com_diffplug_common_swt_widgets_MacDeepLink_nativeAfterSwt
237
246
(JNIEnv *env, jclass clazz) {
238
247
239
- NSLog ( @" [DeepLink] nativeAfterSwt called from Java" );
248
+ os_log_info ( getLog (), " nativeAfterSwt called from Java" );
240
249
241
250
if (!NSApp ) {
242
251
abortWithMessage (@" NSApp is nil! Make sure SWT Display is created first" );
243
252
}
244
253
245
254
// Wrap the existing delegate with our proxy
246
255
id current = [NSApp delegate ];
247
- NSLog ( @" [DeepLink] Current NSApp delegate: %@ " , current);
256
+ os_log_debug ( getLog (), " Current NSApp delegate: %{public} @" , NSStringFromClass ([ current class ]) );
248
257
249
258
// Store proxy in static to prevent deallocation (NSApp.delegate is weak)
250
259
gDelegateProxy = [[DPDelegateProxy alloc ] initWithDelegate: current];
251
260
[NSApp setDelegate: (id <NSApplicationDelegate >)gDelegateProxy ];
252
- NSLog ( @" [DeepLink] Installed delegate proxy" );
261
+ os_log_info ( getLog (), " Installed delegate proxy" );
253
262
}
0 commit comments