You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
5.2 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2015 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #import "RTCFileLogger.h"
  11. #include <memory>
  12. #include "rtc_base/checks.h"
  13. #include "rtc_base/file_rotating_stream.h"
  14. #include "rtc_base/log_sinks.h"
  15. #include "rtc_base/logging.h"
  16. NSString *const kDefaultLogDirName = @"webrtc_logs";
  17. NSUInteger const kDefaultMaxFileSize = 10 * 1024 * 1024; // 10MB.
  18. const char *kRTCFileLoggerRotatingLogPrefix = "rotating_log";
  19. @implementation RTCFileLogger {
  20. BOOL _hasStarted;
  21. NSString *_dirPath;
  22. NSUInteger _maxFileSize;
  23. std::unique_ptr<rtc::FileRotatingLogSink> _logSink;
  24. }
  25. @synthesize severity = _severity;
  26. @synthesize rotationType = _rotationType;
  27. @synthesize shouldDisableBuffering = _shouldDisableBuffering;
  28. - (instancetype)init {
  29. NSArray *paths = NSSearchPathForDirectoriesInDomains(
  30. NSDocumentDirectory, NSUserDomainMask, YES);
  31. NSString *documentsDirPath = [paths firstObject];
  32. NSString *defaultDirPath =
  33. [documentsDirPath stringByAppendingPathComponent:kDefaultLogDirName];
  34. return [self initWithDirPath:defaultDirPath
  35. maxFileSize:kDefaultMaxFileSize];
  36. }
  37. - (instancetype)initWithDirPath:(NSString *)dirPath
  38. maxFileSize:(NSUInteger)maxFileSize {
  39. return [self initWithDirPath:dirPath
  40. maxFileSize:maxFileSize
  41. rotationType:RTCFileLoggerTypeCall];
  42. }
  43. - (instancetype)initWithDirPath:(NSString *)dirPath
  44. maxFileSize:(NSUInteger)maxFileSize
  45. rotationType:(RTCFileLoggerRotationType)rotationType {
  46. NSParameterAssert(dirPath.length);
  47. NSParameterAssert(maxFileSize);
  48. if (self = [super init]) {
  49. BOOL isDir = NO;
  50. NSFileManager *fileManager = [NSFileManager defaultManager];
  51. if ([fileManager fileExistsAtPath:dirPath isDirectory:&isDir]) {
  52. if (!isDir) {
  53. // Bail if something already exists there.
  54. return nil;
  55. }
  56. } else {
  57. if (![fileManager createDirectoryAtPath:dirPath
  58. withIntermediateDirectories:NO
  59. attributes:nil
  60. error:nil]) {
  61. // Bail if we failed to create a directory.
  62. return nil;
  63. }
  64. }
  65. _dirPath = dirPath;
  66. _maxFileSize = maxFileSize;
  67. _severity = RTCFileLoggerSeverityInfo;
  68. }
  69. return self;
  70. }
  71. - (void)dealloc {
  72. [self stop];
  73. }
  74. - (void)start {
  75. if (_hasStarted) {
  76. return;
  77. }
  78. switch (_rotationType) {
  79. case RTCFileLoggerTypeApp:
  80. _logSink.reset(
  81. new rtc::FileRotatingLogSink(_dirPath.UTF8String,
  82. kRTCFileLoggerRotatingLogPrefix,
  83. _maxFileSize,
  84. _maxFileSize / 10));
  85. break;
  86. case RTCFileLoggerTypeCall:
  87. _logSink.reset(
  88. new rtc::CallSessionFileRotatingLogSink(_dirPath.UTF8String,
  89. _maxFileSize));
  90. break;
  91. }
  92. if (!_logSink->Init()) {
  93. RTC_LOG(LS_ERROR) << "Failed to open log files at path: " << _dirPath.UTF8String;
  94. _logSink.reset();
  95. return;
  96. }
  97. if (_shouldDisableBuffering) {
  98. _logSink->DisableBuffering();
  99. }
  100. rtc::LogMessage::LogThreads(true);
  101. rtc::LogMessage::LogTimestamps(true);
  102. rtc::LogMessage::AddLogToStream(_logSink.get(), [self rtcSeverity]);
  103. _hasStarted = YES;
  104. }
  105. - (void)stop {
  106. if (!_hasStarted) {
  107. return;
  108. }
  109. RTC_DCHECK(_logSink);
  110. rtc::LogMessage::RemoveLogToStream(_logSink.get());
  111. _hasStarted = NO;
  112. _logSink.reset();
  113. }
  114. - (nullable NSData *)logData {
  115. if (_hasStarted) {
  116. return nil;
  117. }
  118. NSMutableData* logData = [NSMutableData data];
  119. std::unique_ptr<rtc::FileRotatingStreamReader> stream;
  120. switch(_rotationType) {
  121. case RTCFileLoggerTypeApp:
  122. stream = std::make_unique<rtc::FileRotatingStreamReader>(_dirPath.UTF8String,
  123. kRTCFileLoggerRotatingLogPrefix);
  124. break;
  125. case RTCFileLoggerTypeCall:
  126. stream = std::make_unique<rtc::CallSessionFileRotatingStreamReader>(_dirPath.UTF8String);
  127. break;
  128. }
  129. size_t bufferSize = stream->GetSize();
  130. if (bufferSize == 0) {
  131. return logData;
  132. }
  133. // Allocate memory using malloc so we can pass it direcly to NSData without
  134. // copying.
  135. std::unique_ptr<uint8_t[]> buffer(static_cast<uint8_t*>(malloc(bufferSize)));
  136. size_t read = stream->ReadAll(buffer.get(), bufferSize);
  137. logData = [[NSMutableData alloc] initWithBytesNoCopy:buffer.release()
  138. length:read];
  139. return logData;
  140. }
  141. #pragma mark - Private
  142. - (rtc::LoggingSeverity)rtcSeverity {
  143. switch (_severity) {
  144. case RTCFileLoggerSeverityVerbose:
  145. return rtc::LS_VERBOSE;
  146. case RTCFileLoggerSeverityInfo:
  147. return rtc::LS_INFO;
  148. case RTCFileLoggerSeverityWarning:
  149. return rtc::LS_WARNING;
  150. case RTCFileLoggerSeverityError:
  151. return rtc::LS_ERROR;
  152. }
  153. }
  154. @end