No Description

FIRLogger.m 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright 2017 Google
  2. //
  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
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  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.
  14. #import "Private/FIRLogger.h"
  15. #import <FirebaseCore/FIRLoggerLevel.h>
  16. #import <GoogleUtilities/GULAppEnvironmentUtil.h>
  17. #import <GoogleUtilities/GULLogger.h>
  18. #import "Private/FIRVersion.h"
  19. FIRLoggerService kFIRLoggerCore = @"[Firebase/Core]";
  20. // All the FIRLoggerService definitions should be migrated to clients. Do not add new ones!
  21. FIRLoggerService kFIRLoggerABTesting = @"[Firebase/ABTesting]";
  22. FIRLoggerService kFIRLoggerAdMob = @"[Firebase/AdMob]";
  23. FIRLoggerService kFIRLoggerAnalytics = @"[Firebase/Analytics]";
  24. FIRLoggerService kFIRLoggerAuth = @"[Firebase/Auth]";
  25. FIRLoggerService kFIRLoggerCrash = @"[Firebase/Crash]";
  26. FIRLoggerService kFIRLoggerMLKit = @"[Firebase/MLKit]";
  27. FIRLoggerService kFIRLoggerPerf = @"[Firebase/Performance]";
  28. FIRLoggerService kFIRLoggerRemoteConfig = @"[Firebase/RemoteConfig]";
  29. /// Arguments passed on launch.
  30. NSString *const kFIRDisableDebugModeApplicationArgument = @"-FIRDebugDisabled";
  31. NSString *const kFIREnableDebugModeApplicationArgument = @"-FIRDebugEnabled";
  32. NSString *const kFIRLoggerForceSDTERRApplicationArgument = @"-FIRLoggerForceSTDERR";
  33. /// Key for the debug mode bit in NSUserDefaults.
  34. NSString *const kFIRPersistedDebugModeKey = @"/google/firebase/debug_mode";
  35. /// NSUserDefaults that should be used to store and read variables. If nil, `standardUserDefaults`
  36. /// will be used.
  37. static NSUserDefaults *sFIRLoggerUserDefaults;
  38. static dispatch_once_t sFIRLoggerOnceToken;
  39. // The sFIRAnalyticsDebugMode flag is here to support the -FIRDebugEnabled/-FIRDebugDisabled
  40. // flags used by Analytics. Users who use those flags expect Analytics to log verbosely,
  41. // while the rest of Firebase logs at the default level. This flag is introduced to support
  42. // that behavior.
  43. static BOOL sFIRAnalyticsDebugMode;
  44. #ifdef DEBUG
  45. /// The regex pattern for the message code.
  46. static NSString *const kMessageCodePattern = @"^I-[A-Z]{3}[0-9]{6}$";
  47. static NSRegularExpression *sMessageCodeRegex;
  48. #endif
  49. void FIRLoggerInitializeASL() {
  50. dispatch_once(&sFIRLoggerOnceToken, ^{
  51. // Register Firebase Version with GULLogger.
  52. GULLoggerRegisterVersion(FIRVersionString);
  53. // Override the aslOptions to ASL_OPT_STDERR if the override argument is passed in.
  54. NSArray *arguments = [NSProcessInfo processInfo].arguments;
  55. BOOL overrideSTDERR = [arguments containsObject:kFIRLoggerForceSDTERRApplicationArgument];
  56. // Use the standard NSUserDefaults if it hasn't been explicitly set.
  57. if (sFIRLoggerUserDefaults == nil) {
  58. sFIRLoggerUserDefaults = [NSUserDefaults standardUserDefaults];
  59. }
  60. BOOL forceDebugMode = NO;
  61. BOOL debugMode = [sFIRLoggerUserDefaults boolForKey:kFIRPersistedDebugModeKey];
  62. if ([arguments containsObject:kFIRDisableDebugModeApplicationArgument]) { // Default mode
  63. [sFIRLoggerUserDefaults removeObjectForKey:kFIRPersistedDebugModeKey];
  64. } else if ([arguments containsObject:kFIREnableDebugModeApplicationArgument] ||
  65. debugMode) { // Debug mode
  66. [sFIRLoggerUserDefaults setBool:YES forKey:kFIRPersistedDebugModeKey];
  67. forceDebugMode = YES;
  68. }
  69. GULLoggerInitializeASL();
  70. if (overrideSTDERR) {
  71. GULLoggerEnableSTDERR();
  72. }
  73. if (forceDebugMode) {
  74. GULLoggerForceDebug();
  75. }
  76. });
  77. }
  78. __attribute__((no_sanitize("thread"))) void FIRSetAnalyticsDebugMode(BOOL analyticsDebugMode) {
  79. sFIRAnalyticsDebugMode = analyticsDebugMode;
  80. }
  81. void FIRSetLoggerLevel(FIRLoggerLevel loggerLevel) {
  82. FIRLoggerInitializeASL();
  83. GULSetLoggerLevel((GULLoggerLevel)loggerLevel);
  84. }
  85. #ifdef DEBUG
  86. void FIRResetLogger() {
  87. extern void GULResetLogger(void);
  88. sFIRLoggerOnceToken = 0;
  89. [sFIRLoggerUserDefaults removeObjectForKey:kFIRPersistedDebugModeKey];
  90. sFIRLoggerUserDefaults = nil;
  91. GULResetLogger();
  92. }
  93. void FIRSetLoggerUserDefaults(NSUserDefaults *defaults) {
  94. sFIRLoggerUserDefaults = defaults;
  95. }
  96. #endif
  97. /**
  98. * Check if the level is high enough to be loggable.
  99. *
  100. * Analytics can override the log level with an intentional race condition.
  101. * Add the attribute to get a clean thread sanitizer run.
  102. */
  103. __attribute__((no_sanitize("thread"))) BOOL FIRIsLoggableLevel(FIRLoggerLevel loggerLevel,
  104. BOOL analyticsComponent) {
  105. FIRLoggerInitializeASL();
  106. if (sFIRAnalyticsDebugMode && analyticsComponent) {
  107. return YES;
  108. }
  109. return GULIsLoggableLevel((GULLoggerLevel)loggerLevel);
  110. }
  111. void FIRLogBasic(FIRLoggerLevel level,
  112. FIRLoggerService service,
  113. NSString *messageCode,
  114. NSString *message,
  115. va_list args_ptr) {
  116. FIRLoggerInitializeASL();
  117. GULLogBasic((GULLoggerLevel)level, service,
  118. sFIRAnalyticsDebugMode && [kFIRLoggerAnalytics isEqualToString:service], messageCode,
  119. message, args_ptr);
  120. }
  121. /**
  122. * Generates the logging functions using macros.
  123. *
  124. * Calling FIRLogError(kFIRLoggerCore, @"I-COR000001", @"Configure %@ failed.", @"blah") shows:
  125. * yyyy-mm-dd hh:mm:ss.SSS sender[PID] <Error> [Firebase/Core][I-COR000001] Configure blah failed.
  126. * Calling FIRLogDebug(kFIRLoggerCore, @"I-COR000001", @"Configure succeed.") shows:
  127. * yyyy-mm-dd hh:mm:ss.SSS sender[PID] <Debug> [Firebase/Core][I-COR000001] Configure succeed.
  128. */
  129. #define FIR_LOGGING_FUNCTION(level) \
  130. void FIRLog##level(FIRLoggerService service, NSString *messageCode, NSString *message, ...) { \
  131. va_list args_ptr; \
  132. va_start(args_ptr, message); \
  133. FIRLogBasic(FIRLoggerLevel##level, service, messageCode, message, args_ptr); \
  134. va_end(args_ptr); \
  135. }
  136. FIR_LOGGING_FUNCTION(Error)
  137. FIR_LOGGING_FUNCTION(Warning)
  138. FIR_LOGGING_FUNCTION(Notice)
  139. FIR_LOGGING_FUNCTION(Info)
  140. FIR_LOGGING_FUNCTION(Debug)
  141. #undef FIR_MAKE_LOGGER
  142. #pragma mark - FIRLoggerWrapper
  143. @implementation FIRLoggerWrapper
  144. + (void)logWithLevel:(FIRLoggerLevel)level
  145. withService:(FIRLoggerService)service
  146. withCode:(NSString *)messageCode
  147. withMessage:(NSString *)message
  148. withArgs:(va_list)args {
  149. FIRLogBasic(level, service, messageCode, message, args);
  150. }
  151. @end