1 // Copyright 2017 Google
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 #import "Private/FIRLogger.h"
17 #import <FirebaseCore/FIRLoggerLevel.h>
18 #import <GoogleUtilities/GULAppEnvironmentUtil.h>
19 #import <GoogleUtilities/GULLogger.h>
21 #import "Private/FIRVersion.h"
23 FIRLoggerService kFIRLoggerABTesting = @"[Firebase/ABTesting]";
24 FIRLoggerService kFIRLoggerAdMob = @"[Firebase/AdMob]";
25 FIRLoggerService kFIRLoggerAnalytics = @"[Firebase/Analytics]";
26 FIRLoggerService kFIRLoggerAuth = @"[Firebase/Auth]";
27 FIRLoggerService kFIRLoggerCore = @"[Firebase/Core]";
28 FIRLoggerService kFIRLoggerCrash = @"[Firebase/Crash]";
29 FIRLoggerService kFIRLoggerDatabase = @"[Firebase/Database]";
30 FIRLoggerService kFIRLoggerDynamicLinks = @"[Firebase/DynamicLinks]";
31 FIRLoggerService kFIRLoggerFirestore = @"[Firebase/Firestore]";
32 FIRLoggerService kFIRLoggerInstanceID = @"[Firebase/InstanceID]";
33 FIRLoggerService kFIRLoggerInvites = @"[Firebase/Invites]";
34 FIRLoggerService kFIRLoggerMLKit = @"[Firebase/MLKit]";
35 FIRLoggerService kFIRLoggerMessaging = @"[Firebase/Messaging]";
36 FIRLoggerService kFIRLoggerPerf = @"[Firebase/Performance]";
37 FIRLoggerService kFIRLoggerRemoteConfig = @"[Firebase/RemoteConfig]";
38 FIRLoggerService kFIRLoggerStorage = @"[Firebase/Storage]";
39 FIRLoggerService kFIRLoggerSwizzler = @"[FirebaseSwizzlingUtilities]";
41 /// Arguments passed on launch.
42 NSString *const kFIRDisableDebugModeApplicationArgument = @"-FIRDebugDisabled";
43 NSString *const kFIREnableDebugModeApplicationArgument = @"-FIRDebugEnabled";
44 NSString *const kFIRLoggerForceSDTERRApplicationArgument = @"-FIRLoggerForceSTDERR";
46 /// Key for the debug mode bit in NSUserDefaults.
47 NSString *const kFIRPersistedDebugModeKey = @"/google/firebase/debug_mode";
49 /// NSUserDefaults that should be used to store and read variables. If nil, `standardUserDefaults`
51 static NSUserDefaults *sFIRLoggerUserDefaults;
53 static dispatch_once_t sFIRLoggerOnceToken;
55 // The sFIRAnalyticsDebugMode flag is here to support the -FIRDebugEnabled/-FIRDebugDisabled
56 // flags used by Analytics. Users who use those flags expect Analytics to log verbosely,
57 // while the rest of Firebase logs at the default level. This flag is introduced to support
59 static BOOL sFIRAnalyticsDebugMode;
62 /// The regex pattern for the message code.
63 static NSString *const kMessageCodePattern = @"^I-[A-Z]{3}[0-9]{6}$";
64 static NSRegularExpression *sMessageCodeRegex;
67 void FIRLoggerInitializeASL() {
68 dispatch_once(&sFIRLoggerOnceToken, ^{
69 // Register Firebase Version with GULLogger.
70 GULLoggerRegisterVersion(FIRVersionString);
72 // Override the aslOptions to ASL_OPT_STDERR if the override argument is passed in.
73 NSArray *arguments = [NSProcessInfo processInfo].arguments;
74 BOOL overrideSTDERR = [arguments containsObject:kFIRLoggerForceSDTERRApplicationArgument];
76 // Use the standard NSUserDefaults if it hasn't been explicitly set.
77 if (sFIRLoggerUserDefaults == nil) {
78 sFIRLoggerUserDefaults = [NSUserDefaults standardUserDefaults];
81 BOOL forceDebugMode = NO;
82 BOOL debugMode = [sFIRLoggerUserDefaults boolForKey:kFIRPersistedDebugModeKey];
83 if ([arguments containsObject:kFIRDisableDebugModeApplicationArgument]) { // Default mode
84 [sFIRLoggerUserDefaults removeObjectForKey:kFIRPersistedDebugModeKey];
85 } else if ([arguments containsObject:kFIREnableDebugModeApplicationArgument] ||
86 debugMode) { // Debug mode
87 [sFIRLoggerUserDefaults setBool:YES forKey:kFIRPersistedDebugModeKey];
90 GULLoggerInitializeASL();
92 GULLoggerEnableSTDERR();
95 GULLoggerForceDebug();
100 __attribute__((no_sanitize("thread"))) void FIRSetAnalyticsDebugMode(BOOL analyticsDebugMode) {
101 sFIRAnalyticsDebugMode = analyticsDebugMode;
104 void FIRSetLoggerLevel(FIRLoggerLevel loggerLevel) {
105 FIRLoggerInitializeASL();
106 GULSetLoggerLevel((GULLoggerLevel)loggerLevel);
110 void FIRResetLogger() {
111 extern void GULResetLogger(void);
112 sFIRLoggerOnceToken = 0;
113 [sFIRLoggerUserDefaults removeObjectForKey:kFIRPersistedDebugModeKey];
114 sFIRLoggerUserDefaults = nil;
118 void FIRSetLoggerUserDefaults(NSUserDefaults *defaults) {
119 sFIRLoggerUserDefaults = defaults;
124 * Check if the level is high enough to be loggable.
126 * Analytics can override the log level with an intentional race condition.
127 * Add the attribute to get a clean thread sanitizer run.
129 __attribute__((no_sanitize("thread"))) BOOL FIRIsLoggableLevel(FIRLoggerLevel loggerLevel,
130 BOOL analyticsComponent) {
131 FIRLoggerInitializeASL();
132 if (sFIRAnalyticsDebugMode && analyticsComponent) {
135 return GULIsLoggableLevel((GULLoggerLevel)loggerLevel);
138 void FIRLogBasic(FIRLoggerLevel level,
139 FIRLoggerService service,
140 NSString *messageCode,
143 FIRLoggerInitializeASL();
144 GULLogBasic((GULLoggerLevel)level, service,
145 sFIRAnalyticsDebugMode && [kFIRLoggerAnalytics isEqualToString:service], messageCode,
150 * Generates the logging functions using macros.
152 * Calling FIRLogError(kFIRLoggerCore, @"I-COR000001", @"Configure %@ failed.", @"blah") shows:
153 * yyyy-mm-dd hh:mm:ss.SSS sender[PID] <Error> [Firebase/Core][I-COR000001] Configure blah failed.
154 * Calling FIRLogDebug(kFIRLoggerCore, @"I-COR000001", @"Configure succeed.") shows:
155 * yyyy-mm-dd hh:mm:ss.SSS sender[PID] <Debug> [Firebase/Core][I-COR000001] Configure succeed.
157 #define FIR_LOGGING_FUNCTION(level) \
158 void FIRLog##level(FIRLoggerService service, NSString *messageCode, NSString *message, ...) { \
160 va_start(args_ptr, message); \
161 FIRLogBasic(FIRLoggerLevel##level, service, messageCode, message, args_ptr); \
165 FIR_LOGGING_FUNCTION(Error)
166 FIR_LOGGING_FUNCTION(Warning)
167 FIR_LOGGING_FUNCTION(Notice)
168 FIR_LOGGING_FUNCTION(Info)
169 FIR_LOGGING_FUNCTION(Debug)
171 #undef FIR_MAKE_LOGGER
173 #pragma mark - FIRLoggerWrapper
175 @implementation FIRLoggerWrapper
177 + (void)logWithLevel:(FIRLoggerLevel)level
178 withService:(FIRLoggerService)service
179 withCode:(NSString *)messageCode
180 withMessage:(NSString *)message
181 withArgs:(va_list)args {
182 FIRLogBasic(level, service, messageCode, message, args);