| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 NSString *defaultDirPath = | 38 NSString *defaultDirPath = |
| 39 [documentsDirPath stringByAppendingPathComponent:kDefaultLogDirName]; | 39 [documentsDirPath stringByAppendingPathComponent:kDefaultLogDirName]; |
| 40 return [self initWithDirPath:defaultDirPath | 40 return [self initWithDirPath:defaultDirPath |
| 41 maxFileSize:kDefaultMaxFileSize]; | 41 maxFileSize:kDefaultMaxFileSize]; |
| 42 } | 42 } |
| 43 | 43 |
| 44 - (instancetype)initWithDirPath:(NSString *)dirPath | 44 - (instancetype)initWithDirPath:(NSString *)dirPath |
| 45 maxFileSize:(NSUInteger)maxFileSize { | 45 maxFileSize:(NSUInteger)maxFileSize { |
| 46 return [self initWithDirPath:dirPath | 46 return [self initWithDirPath:dirPath |
| 47 maxFileSize:maxFileSize | 47 maxFileSize:maxFileSize |
| 48 rotationType:kRTCFileLoggerTypeCall]; | 48 rotationType:RTCFileLoggerTypeCall]; |
| 49 } | 49 } |
| 50 | 50 |
| 51 - (instancetype)initWithDirPath:(NSString *)dirPath | 51 - (instancetype)initWithDirPath:(NSString *)dirPath |
| 52 maxFileSize:(NSUInteger)maxFileSize | 52 maxFileSize:(NSUInteger)maxFileSize |
| 53 rotationType:(RTCFileLoggerRotationType)rotationType { | 53 rotationType:(RTCFileLoggerRotationType)rotationType { |
| 54 NSParameterAssert(dirPath.length); | 54 NSParameterAssert(dirPath.length); |
| 55 NSParameterAssert(maxFileSize); | 55 NSParameterAssert(maxFileSize); |
| 56 if (self = [super init]) { | 56 if (self = [super init]) { |
| 57 BOOL isDir = NO; | 57 BOOL isDir = NO; |
| 58 NSFileManager *fileManager = [NSFileManager defaultManager]; | 58 NSFileManager *fileManager = [NSFileManager defaultManager]; |
| 59 if ([fileManager fileExistsAtPath:dirPath isDirectory:&isDir]) { | 59 if ([fileManager fileExistsAtPath:dirPath isDirectory:&isDir]) { |
| 60 if (!isDir) { | 60 if (!isDir) { |
| 61 // Bail if something already exists there. | 61 // Bail if something already exists there. |
| 62 return nil; | 62 return nil; |
| 63 } | 63 } |
| 64 } else { | 64 } else { |
| 65 if (![fileManager createDirectoryAtPath:dirPath | 65 if (![fileManager createDirectoryAtPath:dirPath |
| 66 withIntermediateDirectories:NO | 66 withIntermediateDirectories:NO |
| 67 attributes:nil | 67 attributes:nil |
| 68 error:nil]) { | 68 error:nil]) { |
| 69 // Bail if we failed to create a directory. | 69 // Bail if we failed to create a directory. |
| 70 return nil; | 70 return nil; |
| 71 } | 71 } |
| 72 } | 72 } |
| 73 _dirPath = dirPath; | 73 _dirPath = dirPath; |
| 74 _maxFileSize = maxFileSize; | 74 _maxFileSize = maxFileSize; |
| 75 _severity = kRTCFileLoggerSeverityInfo; | 75 _severity = RTCFileLoggerSeverityInfo; |
| 76 } | 76 } |
| 77 return self; | 77 return self; |
| 78 } | 78 } |
| 79 | 79 |
| 80 - (void)dealloc { | 80 - (void)dealloc { |
| 81 [self stop]; | 81 [self stop]; |
| 82 } | 82 } |
| 83 | 83 |
| 84 - (void)start { | 84 - (void)start { |
| 85 if (_hasStarted) { | 85 if (_hasStarted) { |
| 86 return; | 86 return; |
| 87 } | 87 } |
| 88 switch (_rotationType) { | 88 switch (_rotationType) { |
| 89 case kRTCFileLoggerTypeApp: | 89 case RTCFileLoggerTypeApp: |
| 90 _logSink.reset( | 90 _logSink.reset( |
| 91 new rtc::FileRotatingLogSink(_dirPath.UTF8String, | 91 new rtc::FileRotatingLogSink(_dirPath.UTF8String, |
| 92 kRTCFileLoggerRotatingLogPrefix, | 92 kRTCFileLoggerRotatingLogPrefix, |
| 93 _maxFileSize, | 93 _maxFileSize, |
| 94 _maxFileSize / 10)); | 94 _maxFileSize / 10)); |
| 95 break; | 95 break; |
| 96 case kRTCFileLoggerTypeCall: | 96 case RTCFileLoggerTypeCall: |
| 97 _logSink.reset( | 97 _logSink.reset( |
| 98 new rtc::CallSessionFileRotatingLogSink(_dirPath.UTF8String, | 98 new rtc::CallSessionFileRotatingLogSink(_dirPath.UTF8String, |
| 99 _maxFileSize)); | 99 _maxFileSize)); |
| 100 break; | 100 break; |
| 101 } | 101 } |
| 102 if (!_logSink->Init()) { | 102 if (!_logSink->Init()) { |
| 103 LOG(LS_ERROR) << "Failed to open log files at path: " | 103 LOG(LS_ERROR) << "Failed to open log files at path: " |
| 104 << _dirPath.UTF8String; | 104 << _dirPath.UTF8String; |
| 105 _logSink.reset(); | 105 _logSink.reset(); |
| 106 return; | 106 return; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 124 _logSink.reset(); | 124 _logSink.reset(); |
| 125 } | 125 } |
| 126 | 126 |
| 127 - (NSData *)logData { | 127 - (NSData *)logData { |
| 128 if (_hasStarted) { | 128 if (_hasStarted) { |
| 129 return nil; | 129 return nil; |
| 130 } | 130 } |
| 131 NSMutableData* logData = [NSMutableData data]; | 131 NSMutableData* logData = [NSMutableData data]; |
| 132 rtc::scoped_ptr<rtc::FileRotatingStream> stream; | 132 rtc::scoped_ptr<rtc::FileRotatingStream> stream; |
| 133 switch(_rotationType) { | 133 switch(_rotationType) { |
| 134 case kRTCFileLoggerTypeApp: | 134 case RTCFileLoggerTypeApp: |
| 135 stream.reset( | 135 stream.reset( |
| 136 new rtc::FileRotatingStream(_dirPath.UTF8String, | 136 new rtc::FileRotatingStream(_dirPath.UTF8String, |
| 137 kRTCFileLoggerRotatingLogPrefix)); | 137 kRTCFileLoggerRotatingLogPrefix)); |
| 138 break; | 138 break; |
| 139 case kRTCFileLoggerTypeCall: | 139 case RTCFileLoggerTypeCall: |
| 140 stream.reset(new rtc::CallSessionFileRotatingStream(_dirPath.UTF8String)); | 140 stream.reset(new rtc::CallSessionFileRotatingStream(_dirPath.UTF8String)); |
| 141 break; | 141 break; |
| 142 } | 142 } |
| 143 if (!stream->Open()) { | 143 if (!stream->Open()) { |
| 144 return logData; | 144 return logData; |
| 145 } | 145 } |
| 146 size_t bufferSize = 0; | 146 size_t bufferSize = 0; |
| 147 if (!stream->GetSize(&bufferSize) || bufferSize == 0) { | 147 if (!stream->GetSize(&bufferSize) || bufferSize == 0) { |
| 148 return logData; | 148 return logData; |
| 149 } | 149 } |
| 150 size_t read = 0; | 150 size_t read = 0; |
| 151 // Allocate memory using malloc so we can pass it direcly to NSData without | 151 // Allocate memory using malloc so we can pass it direcly to NSData without |
| 152 // copying. | 152 // copying. |
| 153 rtc::scoped_ptr<uint8_t[]> buffer(static_cast<uint8_t*>(malloc(bufferSize))); | 153 rtc::scoped_ptr<uint8_t[]> buffer(static_cast<uint8_t*>(malloc(bufferSize))); |
| 154 stream->ReadAll(buffer.get(), bufferSize, &read, nullptr); | 154 stream->ReadAll(buffer.get(), bufferSize, &read, nullptr); |
| 155 logData = [[NSMutableData alloc] initWithBytesNoCopy:buffer.release() | 155 logData = [[NSMutableData alloc] initWithBytesNoCopy:buffer.release() |
| 156 length:read]; | 156 length:read]; |
| 157 return logData; | 157 return logData; |
| 158 } | 158 } |
| 159 | 159 |
| 160 #pragma mark - Private | 160 #pragma mark - Private |
| 161 | 161 |
| 162 - (rtc::LoggingSeverity)rtcSeverity { | 162 - (rtc::LoggingSeverity)rtcSeverity { |
| 163 switch (_severity) { | 163 switch (_severity) { |
| 164 case kRTCFileLoggerSeverityVerbose: | 164 case RTCFileLoggerSeverityVerbose: |
| 165 return rtc::LS_VERBOSE; | 165 return rtc::LS_VERBOSE; |
| 166 case kRTCFileLoggerSeverityInfo: | 166 case RTCFileLoggerSeverityInfo: |
| 167 return rtc::LS_INFO; | 167 return rtc::LS_INFO; |
| 168 case kRTCFileLoggerSeverityWarning: | 168 case RTCFileLoggerSeverityWarning: |
| 169 return rtc::LS_WARNING; | 169 return rtc::LS_WARNING; |
| 170 case kRTCFileLoggerSeverityError: | 170 case RTCFileLoggerSeverityError: |
| 171 return rtc::LS_ERROR; | 171 return rtc::LS_ERROR; |
| 172 } | 172 } |
| 173 } | 173 } |
| 174 | 174 |
| 175 @end | 175 @end |
| OLD | NEW |