FIRMessagingFileLogger.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright 2017 Google
  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 "FIRMessagingFileLogger.h"
  17. #if FIRMessaging_PROBER
  18. #import "DDFileLogger.h"
  19. #import "DDLog.h"
  20. @interface FIRMessagingFileLogFilter ()
  21. @property(nonatomic, readwrite, assign) FIRMessagingLogLevel level;
  22. @end
  23. @implementation FIRMessagingFileLogFilter
  24. #pragma mark - GTMLogFilter protocol
  25. - (BOOL)filterAllowsMessage:(NSString *)msg level:(FIRMessagingLogLevel)level {
  26. // allow everything
  27. return YES;
  28. }
  29. @end
  30. @interface FIRMessagingFileLogFormatter ()
  31. @property(nonatomic, readwrite, strong) NSDateFormatter *dateFormatter;
  32. @end
  33. @implementation FIRMessagingFileLogFormatter
  34. static NSString *const kFIRMessagingLogPrefix = @"FIRMessaging";
  35. - (id)init {
  36. if ((self = [super init])) {
  37. _dateFormatter = [[NSDateFormatter alloc] init];
  38. [_dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
  39. [_dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
  40. }
  41. return self;
  42. }
  43. #pragma mark - GTMLogFormatter protocol
  44. static DDLogMessage *currentMessage;
  45. - (NSString *)stringForFunc:(NSString *)func
  46. withFormat:(NSString *)fmt
  47. valist:(va_list)args
  48. level:(FIRMessagingLogLevel)level {
  49. NSString *logMessage = [[NSString alloc] initWithFormat:fmt arguments:args];
  50. currentMessage = [[DDLogMessage alloc] initWithMessage:logMessage
  51. level:0
  52. flag:0
  53. context:0
  54. file:NULL
  55. function:NULL
  56. line:0
  57. tag:0
  58. options:0
  59. timestamp:[NSDate date]];
  60. return logMessage;
  61. }
  62. @end
  63. @interface FIRMessagingFileLogWriter ()
  64. @property(nonatomic, readwrite, strong) DDFileLogger *fileLogger;
  65. @end
  66. @implementation FIRMessagingFileLogWriter
  67. - (instancetype)init {
  68. self = [super init];
  69. if (self) {
  70. _fileLogger = [[DDFileLogger alloc] init];
  71. }
  72. return self;
  73. }
  74. #pragma mark - GTMLogWriter protocol
  75. - (void)logMessage:(NSString *)msg level:(FIRMessagingLogLevel)level {
  76. // log to stdout
  77. NSLog(@"%@", msg);
  78. [self.fileLogger logMessage:currentMessage];
  79. }
  80. @end
  81. #endif