GACAppCheckLogger.m 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright 2020 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "AppCheckCore/Sources/Public/AppCheckCore/GACAppCheckLogger.h"
  17. #import "AppCheckCore/Sources/Core/GACAppCheckLogger+Internal.h"
  18. NS_ASSUME_NONNULL_BEGIN
  19. #pragma mark - Public
  20. @implementation GACAppCheckLogger
  21. // Note: Declared as volatile to make getting and setting atomic.
  22. static volatile GACAppCheckLogLevel _logLevel;
  23. + (GACAppCheckLogLevel)logLevel {
  24. return _logLevel;
  25. }
  26. + (void)setLogLevel:(GACAppCheckLogLevel)logLevel {
  27. _logLevel = logLevel;
  28. }
  29. @end
  30. #pragma mark - Helpers
  31. NSString *GACAppCheckMessageCodeEnumToString(GACAppCheckMessageCode code) {
  32. return [[NSString alloc] initWithFormat:@"I-GAC%06ld", (long)code];
  33. }
  34. NSString *GACAppCheckLoggerLevelEnumToString(GACAppCheckLogLevel logLevel) {
  35. switch (logLevel) {
  36. case GACAppCheckLogLevelFault:
  37. return @"Fault";
  38. case GACAppCheckLogLevelError:
  39. return @"Error";
  40. case GACAppCheckLogLevelWarning:
  41. return @"Warning";
  42. case GACAppCheckLogLevelInfo:
  43. return @"Info";
  44. case GACAppCheckLogLevelDebug:
  45. return @"Debug";
  46. }
  47. }
  48. #pragma mark - Logging Functions
  49. /**
  50. * Generates the logging functions using macros.
  51. *
  52. * Calling GACLogError(@"Firebase", @"I-GAC000001", @"Configure %@ failed.", @"blah") shows:
  53. * yyyy-mm-dd hh:mm:ss.SSS sender[PID] <Error> [Firebase/AppCheck][I-GAC000001] Configure blah
  54. * failed. Calling GACLogDebug(@"GoogleSignIn", @"I-GAC000002", @"Configure succeed.") shows:
  55. * yyyy-mm-dd hh:mm:ss.SSS sender[PID] <Debug> [GoogleSignIn/AppCheck][I-COR000002] Configure
  56. * succeed.
  57. */
  58. void GACAppCheckLog(GACAppCheckMessageCode code, GACAppCheckLogLevel logLevel, NSString *message) {
  59. // Don't log anything in not debug builds.
  60. #if !NDEBUG
  61. if (logLevel >= GACAppCheckLogger.logLevel) {
  62. NSLog(@"<%@> [AppCheckCore][%@] %@", GACAppCheckLoggerLevelEnumToString(logLevel),
  63. GACAppCheckMessageCodeEnumToString(code), message);
  64. }
  65. #endif // !NDEBUG
  66. }
  67. NS_ASSUME_NONNULL_END