Skip to content

Commit 6946d8a

Browse files
authored
Remove calls to LogInfo, LogError, LogDebug during obj-c +load. (#706)
* Remove calls to LogInfo, LogError, LogDebug during obj-c +load. This could be causing an issue in C++ as global class constructors have not yet been run.
1 parent 29089a7 commit 6946d8a

File tree

2 files changed

+61
-67
lines changed

2 files changed

+61
-67
lines changed

app/src/util_ios.mm

Lines changed: 59 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ - (void)application:(UIApplication *)application
5858
}
5959
- (void)application:(UIApplication *)application
6060
didReceiveRemoteNotification:(NSDictionary *)userInfo
61-
fetchCompletionHandler:
62-
(void (^)(UIBackgroundFetchResult result))handler {
61+
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler {
6362
}
6463
#if defined(__IPHONE_12_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_12_0
6564
- (BOOL)application:(UIApplication *)application
@@ -74,7 +73,7 @@ - (BOOL)application:(UIApplication *)application
7473
restorationHandler:(void (^)(NSArray *))restorationHandler {
7574
return NO;
7675
}
77-
#endif // defined(__IPHONE_12_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_12_0
76+
#endif // defined(__IPHONE_12_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_12_0
7877
@end
7978

8079
namespace firebase {
@@ -102,17 +101,19 @@ void ForEachAppDelegateClass(void (^block)(Class)) {
102101
}
103102
}
104103
if (!blacklisted) {
105-
::firebase::LogDebug("Firebase: Found UIApplicationDelegate class %s",
106-
class_name);
104+
if (GetLogLevel() <= kLogLevelDebug) {
105+
// Call NSLog directly because we may be in a +load method,
106+
// and C++ classes may not be constructed yet.
107+
NSLog(@"Firebase: Found UIApplicationDelegate class %s", class_name);
108+
}
107109
block(clazz);
108110
}
109111
}
110112
}
111113
free(classes);
112114
}
113115

114-
NSDictionary *StringMapToNSDictionary(
115-
const std::map<std::string, std::string> &string_map) {
116+
NSDictionary *StringMapToNSDictionary(const std::map<std::string, std::string> &string_map) {
116117
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
117118
for (auto &kv : string_map) {
118119
dictionary[[NSString stringWithUTF8String:kv.first.c_str()]] =
@@ -121,8 +122,7 @@ void ForEachAppDelegateClass(void (^block)(Class)) {
121122
return dictionary;
122123
}
123124

124-
NSDictionary *CharArrayMapToNSDictionary(
125-
const std::map<const char *, const char *> &string_map) {
125+
NSDictionary *CharArrayMapToNSDictionary(const std::map<const char *, const char *> &string_map) {
126126
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
127127
for (auto &kv : string_map) {
128128
dictionary[[NSString stringWithUTF8String:kv.first]] =
@@ -143,17 +143,14 @@ void ForEachAppDelegateClass(void (^block)(Class)) {
143143
return std::string(static_cast<const char *>(data.bytes), data.length);
144144
}
145145

146-
NSString *StringToNSString(const std::string &str) {
147-
return CStringToNSString(str.c_str());
148-
}
146+
NSString *StringToNSString(const std::string &str) { return CStringToNSString(str.c_str()); }
149147

150148
NSString *CStringToNSString(const char *c_str) {
151149
return [NSString stringWithCString:c_str encoding:NSUTF8StringEncoding];
152150
}
153151

154152
std::string NSStringToString(NSString *str) {
155-
return str ? std::string([str cStringUsingEncoding:NSUTF8StringEncoding]) :
156-
std::string();
153+
return str ? std::string([str cStringUsingEncoding:NSUTF8StringEncoding]) : std::string();
157154
}
158155

159156
NSMutableArray *StringVectorToNSMutableArray(const std::vector<std::string> &vector) {
@@ -164,41 +161,37 @@ void ForEachAppDelegateClass(void (^block)(Class)) {
164161
return array;
165162
}
166163

167-
NSMutableArray* StdVectorToNSMutableArray(const std::vector<Variant>& vector) {
168-
NSMutableArray* array =
169-
[[NSMutableArray alloc] initWithCapacity:vector.size()];
170-
for (auto& variant : vector) {
164+
NSMutableArray *StdVectorToNSMutableArray(const std::vector<Variant> &vector) {
165+
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:vector.size()];
166+
for (auto &variant : vector) {
171167
[array addObject:VariantToId(variant)];
172168
}
173169
return array;
174170
}
175171

176-
NSMutableDictionary* StdMapToNSMutableDictionary(
177-
const std::map<Variant, Variant>& map) {
178-
NSMutableDictionary* dictionary =
179-
[[NSMutableDictionary alloc] initWithCapacity:map.size()];
180-
for (auto& pair : map) {
181-
[dictionary setObject:VariantToId(pair.second)
182-
forKey:VariantToId(pair.first)];
172+
NSMutableDictionary *StdMapToNSMutableDictionary(const std::map<Variant, Variant> &map) {
173+
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithCapacity:map.size()];
174+
for (auto &pair : map) {
175+
[dictionary setObject:VariantToId(pair.second) forKey:VariantToId(pair.first)];
183176
}
184177
return dictionary;
185178
}
186179

187-
void NSArrayToStdVector(NSArray *array, std::vector<Variant>* vector) {
180+
void NSArrayToStdVector(NSArray *array, std::vector<Variant> *vector) {
188181
vector->reserve(array.count);
189182
for (id object in array) {
190183
vector->push_back(IdToVariant(object));
191184
}
192185
}
193186

194-
void NSDictionaryToStdMap(NSDictionary *dictionary, std::map<Variant, Variant>* map) {
187+
void NSDictionaryToStdMap(NSDictionary *dictionary, std::map<Variant, Variant> *map) {
195188
for (id key in dictionary) {
196189
id value = [dictionary objectForKey:key];
197190
map->insert(std::make_pair(IdToVariant(key), IdToVariant(value)));
198191
}
199192
}
200193

201-
id VariantToId(const Variant& variant) {
194+
id VariantToId(const Variant &variant) {
202195
switch (variant.type()) {
203196
case Variant::kTypeNull: {
204197
return [NSNull null];
@@ -231,22 +224,18 @@ id VariantToId(const Variant& variant) {
231224

232225
static bool IdIsBoolean(id value) {
233226
if ([value isKindOfClass:[NSNumber class]]) {
234-
const char* type = [value objCType];
235-
return strcmp(type, @encode(BOOL)) == 0 ||
236-
strcmp(type, @encode(signed char)) == 0;
227+
const char *type = [value objCType];
228+
return strcmp(type, @encode(BOOL)) == 0 || strcmp(type, @encode(signed char)) == 0;
237229
}
238230
return false;
239231
}
240232

241233
static bool IdIsInteger(id value) {
242234
if ([value isKindOfClass:[NSNumber class]]) {
243-
const char* type = [value objCType];
244-
return strcmp(type, @encode(int)) == 0 ||
245-
strcmp(type, @encode(short)) == 0 ||
246-
strcmp(type, @encode(long)) == 0 ||
247-
strcmp(type, @encode(long long)) == 0 ||
248-
strcmp(type, @encode(unsigned char)) == 0 ||
249-
strcmp(type, @encode(unsigned int)) == 0 ||
235+
const char *type = [value objCType];
236+
return strcmp(type, @encode(int)) == 0 || strcmp(type, @encode(short)) == 0 ||
237+
strcmp(type, @encode(long)) == 0 || strcmp(type, @encode(long long)) == 0 ||
238+
strcmp(type, @encode(unsigned char)) == 0 || strcmp(type, @encode(unsigned int)) == 0 ||
250239
strcmp(type, @encode(unsigned short)) == 0 ||
251240
strcmp(type, @encode(unsigned long)) == 0 ||
252241
strcmp(type, @encode(unsigned long long)) == 0;
@@ -256,9 +245,8 @@ static bool IdIsInteger(id value) {
256245

257246
static bool IdIsFloatingPoint(id value) {
258247
if ([value isKindOfClass:[NSNumber class]]) {
259-
const char* type = [value objCType];
260-
return strcmp(type, @encode(float)) == 0 ||
261-
strcmp(type, @encode(double)) == 0;
248+
const char *type = [value objCType];
249+
return strcmp(type, @encode(float)) == 0 || strcmp(type, @encode(double)) == 0;
262250
}
263251
return false;
264252
}
@@ -307,15 +295,13 @@ void DispatchAsyncSafeMainQueue(void (^block)(void)) {
307295
}
308296
}
309297

310-
void RunOnMainThread(void (*function_ptr)(void *function_data),
311-
void *function_data) {
298+
void RunOnMainThread(void (*function_ptr)(void *function_data), void *function_data) {
312299
dispatch_async(dispatch_get_main_queue(), ^{
313300
function_ptr(function_data);
314301
});
315302
}
316303

317-
void RunOnBackgroundThread(void (*function_ptr)(void *function_data),
318-
void *function_data) {
304+
void RunOnBackgroundThread(void (*function_ptr)(void *function_data), void *function_data) {
319305
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
320306
function_ptr(function_data);
321307
});
@@ -350,12 +336,14 @@ void RunOnBackgroundThread(void (*function_ptr)(void *function_data),
350336

351337
// Get the type encoding of the selector from a type_encoding_class (which is a class which
352338
// implements a stub for the method).
353-
const char *type_encoding = method_getTypeEncoding(
354-
class_getInstanceMethod(type_encoding_class, name));
339+
const char *type_encoding =
340+
method_getTypeEncoding(class_getInstanceMethod(type_encoding_class, name));
355341
FIREBASE_ASSERT(type_encoding);
356342

357343
NSString *new_method_name_nsstring = nil;
358-
::firebase::LogDebug("Registering method for %s selector %s", class_name, selector_name);
344+
if (GetLogLevel() <= kLogLevelDebug) {
345+
NSLog(@"Registering method for %s selector %s", class_name, selector_name);
346+
}
359347
if (original_method_implementation) {
360348
// Try adding a method with randomized prefix on the name.
361349
int retry = kRandomNameGenerationRetries;
@@ -370,27 +358,32 @@ void RunOnBackgroundThread(void (*function_ptr)(void *function_data),
370358
}
371359
const char *new_method_name = new_method_name_nsstring.UTF8String;
372360
if (retry == 0) {
373-
LogError("Failed to add method %s on class %s as the %s method already exists on the class. "
374-
"To resolve this issue, change the name of the method %s on the class %s.",
375-
new_method_name, class_name, new_method_name, new_method_name, class_name);
361+
NSLog(@"Failed to add method %s on class %s as the %s method already exists on the class. To "
362+
@"resolve this issue, change the name of the method %s on the class %s.",
363+
new_method_name, class_name, new_method_name, new_method_name, class_name);
376364
return;
377365
}
378366
method_setImplementation(method, imp);
379367
// Save the selector name that points at the original method implementation.
380368
SetMethod(name, new_method_name_nsstring);
381-
::firebase::LogDebug("Registered method for %s selector %s (original method %s 0x%08x)",
382-
class_name, selector_name, new_method_name,
383-
static_cast<int>(reinterpret_cast<intptr_t>(
384-
original_method_implementation)));
369+
if (GetLogLevel() <= kLogLevelDebug) {
370+
NSLog(@"Registered method for %s selector %s (original method %s 0x%08x)", class_name,
371+
selector_name, new_method_name,
372+
static_cast<int>(reinterpret_cast<intptr_t>(original_method_implementation)));
373+
}
385374
} else if (add_method) {
386-
::firebase::LogDebug("Adding method for %s selector %s", class_name, selector_name);
375+
if (GetLogLevel() <= kLogLevelDebug) {
376+
NSLog(@"Adding method for %s selector %s", class_name, selector_name);
377+
}
387378
// The class doesn't implement the selector so simply install our method implementation.
388379
if (!class_addMethod(clazz, name, imp, type_encoding)) {
389-
LogError("Failed to add new method %s on class %s.", selector_name, class_name);
380+
NSLog(@"Failed to add new method %s on class %s.", selector_name, class_name);
390381
}
391382
} else {
392-
::firebase::LogDebug("Method implementation for %s selector %s not found, ignoring.",
393-
class_name, selector_name);
383+
if (GetLogLevel() <= kLogLevelDebug) {
384+
NSLog(@"Method implementation for %s selector %s not found, ignoring.", class_name,
385+
selector_name);
386+
}
394387
}
395388
}
396389

@@ -420,19 +413,19 @@ void RunOnBackgroundThread(void (*function_ptr)(void *function_data),
420413
const char *selector_implementation_name = selector_implementation_name_nsstring.UTF8String;
421414
SEL selector_implementation = NSSelectorFromString(selector_implementation_name_nsstring);
422415
search_class = clazz;
423-
for ( ; search_class; search_class = class_getSuperclass(search_class)) {
416+
for (; search_class; search_class = class_getSuperclass(search_class)) {
424417
const char *search_class_name = class_getName(search_class);
425-
firebase::LogDebug("Searching for selector %s (%s) on class %s",
426-
selector_name, selector_implementation_name, search_class_name);
418+
firebase::LogDebug("Searching for selector %s (%s) on class %s", selector_name,
419+
selector_implementation_name, search_class_name);
427420
Method method = class_getInstanceMethod(search_class, selector_implementation);
428421
method_implementation = method ? method_getImplementation(method) : nil;
429422
if (method_implementation) break;
430423
}
431424
if (method_implementation) break;
432425
}
433426
if (!method_implementation) {
434-
firebase::LogDebug("Class %s does not respond to selector %s (%s)", class_name,
435-
selector_name, selector_implementation_name_nsstring.UTF8String);
427+
firebase::LogDebug("Class %s does not respond to selector %s (%s)", class_name, selector_name,
428+
selector_implementation_name_nsstring.UTF8String);
436429
return nil;
437430
}
438431
firebase::LogDebug("Found %s (%s, 0x%08x) on class %s (%s)", selector_name,
@@ -472,8 +465,8 @@ void RunOnBackgroundThread(void (*function_ptr)(void *function_data),
472465
while (retry--) {
473466
// Cache the old method implementation in a new method so that we can lookup the original
474467
// implementation from the instance of the class.
475-
NSString *random_selector_name = [[NSString alloc] initWithFormat:@"FIRA%x%@", arc4random(),
476-
selector_name];
468+
NSString *random_selector_name =
469+
[[NSString alloc] initWithFormat:@"FIRA%x%@", arc4random(), selector_name];
477470
if (!implementation_selector_names[random_selector_name]) {
478471
return random_selector_name;
479472
}

messaging/src/ios/messaging.mm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,8 @@ void SetTokenRegistrationOnInitEnabled(bool enable) {
869869
// http://www.opensource.apple.com/source/objc4/objc4-274/runtime/objc-runtime.m)
870870
@implementation UIApplication (FIRFCM)
871871
+ (void)load {
872-
::firebase::LogInfo("FCM: Loading UIApplication FIRFCM category");
872+
// C++ constructors may not be called yet so call NSLog rather than LogInfo.
873+
NSLog(@"FCM: Loading UIApplication FIRFCM category");
873874
::firebase::util::ForEachAppDelegateClass(^(Class clazz) {
874875
FirebaseMessagingHookAppDelegate(clazz);
875876
});

0 commit comments

Comments
 (0)