Skip to content

Commit 566e7a8

Browse files
committed
Don't call FIRConfiguration setLoggerLevel: before FIRApp exists.
Instead, cache the value, and call setLoggerLevel later, after the iOS FIRApp instance is created.
1 parent b7aa15b commit 566e7a8

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

app/src/app_ios.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@
1717
#ifndef FIREBASE_APP_SRC_APP_IOS_H_
1818
#define FIREBASE_APP_SRC_APP_IOS_H_
1919
#include "FIRApp.h"
20+
#include "FIRConfiguration.h"
2021
#include "app/src/util_ios.h"
2122

2223
namespace firebase {
2324

2425
namespace internal {
2526
OBJ_C_PTR_WRAPPER_NAMED(AppInternal, FIRApp);
27+
28+
void SetFirConfigurationLoggerLevel(FIRLoggerLevel level);
29+
2630
} // namespace internal
2731

2832
} // namespace firebase

app/src/app_ios.mm

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ static void PlatformOptionsToAppOptions(FIROptions* platform_options, AppOptions
133133
return platform_app;
134134
}
135135

136+
// Settings cached by SetFirConfigurationLoggerLevel if App wasn't already initialized.
137+
static bool g_delayed_fir_configuration_logger_level_set = false;
138+
static FIRLoggerLevel g_delayed_fir_configuration_logger_level = FIRLoggerLevelWarning;
139+
136140
// Create an iOS FIRApp instance.
137141
static FIRApp* CreatePlatformApp(const AppOptions& options, const char* name) {
138142
__block FIRApp* platform_app = nil;
@@ -155,6 +159,14 @@ static void PlatformOptionsToAppOptions(FIROptions* platform_options, AppOptions
155159
[FIRApp configureWithName:@(name) options:platform_options];
156160
}
157161
platform_app = GetPlatformAppByName(name);
162+
// If the logger level was cached due to logging happening prior to
163+
// App's initialization, apply the delayed setting now, when FIRApp
164+
// is guaranteed to exist and we are in the main thread.
165+
if (g_delayed_fir_configuration_logger_level_set) {
166+
g_delayed_fir_configuration_logger_level_set = false;
167+
[[FIRConfiguration sharedInstance]
168+
setLoggerLevel:g_delayed_fir_configuration_logger_level];
169+
}
158170
} @catch (NSException* e) {
159171
LogError("Unable to configure Firebase app (%s)",
160172
util::NSStringToString(e.reason).c_str());
@@ -276,4 +288,19 @@ static void PlatformOptionsToAppOptions(FIROptions* platform_options, AppOptions
276288

277289
FIRApp* App::GetPlatformApp() const { return internal_->get(); }
278290

291+
void SetFirConfigurationLoggerLevel(FIRLoggerLevel level) {
292+
// Check if a FIRApp has been created. FIRApp.allApps will return a
293+
// list of all FIRApps, or nil if no FIRApp has been created yet.
294+
if (FIRApp.allApps != nil) {
295+
// FIRApp has already been initialized, it's safe to set this
296+
// value now.
297+
[[FIRConfiguration sharedInstance] setLoggerLevel:level];
298+
} else {
299+
// FIRApp has not yet been initialized. Cache this value to set
300+
// later, in CreatePlatformApp().
301+
g_delayed_fir_configuration_logger_level = level;
302+
g_delayed_fir_configuration_logger_level_set = true;
303+
}
304+
}
305+
279306
} // namespace firebase

app/src/log_ios.mm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#import "FIRConfiguration.h"
2222

23+
#include "app/src/app_ios.h"
2324
#include "app/src/include/firebase/internal/common.h"
2425

2526
#include <stdarg.h>
@@ -61,7 +62,7 @@ void LogInitialize() {
6162
// Set the platform specific SDK log level.
6263
void LogSetPlatformLevel(LogLevel level) {
6364
assert(level < FIREBASE_ARRAYSIZE(kCppToIOSLogLevel));
64-
[[FIRConfiguration sharedInstance] setLoggerLevel:kCppToIOSLogLevel[level]];
65+
firebase::internal::SetFirConfigurationLoggerLevel(kCppToIOSLogLevel[level]);
6566
}
6667

6768
// Log a firebase message.

0 commit comments

Comments
 (0)