| 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 |
| 11 #import "WebRTC/RTCFileLogger.h" | 11 #import "WebRTC/RTCFileLogger.h" |
| 12 | 12 |
| 13 #include <memory> |
| 14 |
| 13 #include "webrtc/base/checks.h" | 15 #include "webrtc/base/checks.h" |
| 14 #include "webrtc/base/filerotatingstream.h" | 16 #include "webrtc/base/filerotatingstream.h" |
| 15 #include "webrtc/base/logging.h" | 17 #include "webrtc/base/logging.h" |
| 16 #include "webrtc/base/logsinks.h" | 18 #include "webrtc/base/logsinks.h" |
| 17 #include "webrtc/base/scoped_ptr.h" | |
| 18 | 19 |
| 19 NSString *const kDefaultLogDirName = @"webrtc_logs"; | 20 NSString *const kDefaultLogDirName = @"webrtc_logs"; |
| 20 NSUInteger const kDefaultMaxFileSize = 10 * 1024 * 1024; // 10MB. | 21 NSUInteger const kDefaultMaxFileSize = 10 * 1024 * 1024; // 10MB. |
| 21 const char *kRTCFileLoggerRotatingLogPrefix = "rotating_log"; | 22 const char *kRTCFileLoggerRotatingLogPrefix = "rotating_log"; |
| 22 | 23 |
| 23 @implementation RTCFileLogger { | 24 @implementation RTCFileLogger { |
| 24 BOOL _hasStarted; | 25 BOOL _hasStarted; |
| 25 NSString *_dirPath; | 26 NSString *_dirPath; |
| 26 NSUInteger _maxFileSize; | 27 NSUInteger _maxFileSize; |
| 27 rtc::scoped_ptr<rtc::FileRotatingLogSink> _logSink; | 28 std::unique_ptr<rtc::FileRotatingLogSink> _logSink; |
| 28 } | 29 } |
| 29 | 30 |
| 30 @synthesize severity = _severity; | 31 @synthesize severity = _severity; |
| 31 @synthesize rotationType = _rotationType; | 32 @synthesize rotationType = _rotationType; |
| 32 @synthesize shouldDisableBuffering = _shouldDisableBuffering; | 33 @synthesize shouldDisableBuffering = _shouldDisableBuffering; |
| 33 | 34 |
| 34 - (instancetype)init { | 35 - (instancetype)init { |
| 35 NSArray *paths = NSSearchPathForDirectoriesInDomains( | 36 NSArray *paths = NSSearchPathForDirectoriesInDomains( |
| 36 NSDocumentDirectory, NSUserDomainMask, YES); | 37 NSDocumentDirectory, NSUserDomainMask, YES); |
| 37 NSString *documentsDirPath = [paths firstObject]; | 38 NSString *documentsDirPath = [paths firstObject]; |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 rtc::LogMessage::RemoveLogToStream(_logSink.get()); | 123 rtc::LogMessage::RemoveLogToStream(_logSink.get()); |
| 123 _hasStarted = NO; | 124 _hasStarted = NO; |
| 124 _logSink.reset(); | 125 _logSink.reset(); |
| 125 } | 126 } |
| 126 | 127 |
| 127 - (NSData *)logData { | 128 - (NSData *)logData { |
| 128 if (_hasStarted) { | 129 if (_hasStarted) { |
| 129 return nil; | 130 return nil; |
| 130 } | 131 } |
| 131 NSMutableData* logData = [NSMutableData data]; | 132 NSMutableData* logData = [NSMutableData data]; |
| 132 rtc::scoped_ptr<rtc::FileRotatingStream> stream; | 133 std::unique_ptr<rtc::FileRotatingStream> stream; |
| 133 switch(_rotationType) { | 134 switch(_rotationType) { |
| 134 case RTCFileLoggerTypeApp: | 135 case RTCFileLoggerTypeApp: |
| 135 stream.reset( | 136 stream.reset( |
| 136 new rtc::FileRotatingStream(_dirPath.UTF8String, | 137 new rtc::FileRotatingStream(_dirPath.UTF8String, |
| 137 kRTCFileLoggerRotatingLogPrefix)); | 138 kRTCFileLoggerRotatingLogPrefix)); |
| 138 break; | 139 break; |
| 139 case RTCFileLoggerTypeCall: | 140 case RTCFileLoggerTypeCall: |
| 140 stream.reset(new rtc::CallSessionFileRotatingStream(_dirPath.UTF8String)); | 141 stream.reset(new rtc::CallSessionFileRotatingStream(_dirPath.UTF8String)); |
| 141 break; | 142 break; |
| 142 } | 143 } |
| 143 if (!stream->Open()) { | 144 if (!stream->Open()) { |
| 144 return logData; | 145 return logData; |
| 145 } | 146 } |
| 146 size_t bufferSize = 0; | 147 size_t bufferSize = 0; |
| 147 if (!stream->GetSize(&bufferSize) || bufferSize == 0) { | 148 if (!stream->GetSize(&bufferSize) || bufferSize == 0) { |
| 148 return logData; | 149 return logData; |
| 149 } | 150 } |
| 150 size_t read = 0; | 151 size_t read = 0; |
| 151 // Allocate memory using malloc so we can pass it direcly to NSData without | 152 // Allocate memory using malloc so we can pass it direcly to NSData without |
| 152 // copying. | 153 // copying. |
| 153 rtc::scoped_ptr<uint8_t[]> buffer(static_cast<uint8_t*>(malloc(bufferSize))); | 154 std::unique_ptr<uint8_t[]> buffer(static_cast<uint8_t*>(malloc(bufferSize))); |
| 154 stream->ReadAll(buffer.get(), bufferSize, &read, nullptr); | 155 stream->ReadAll(buffer.get(), bufferSize, &read, nullptr); |
| 155 logData = [[NSMutableData alloc] initWithBytesNoCopy:buffer.release() | 156 logData = [[NSMutableData alloc] initWithBytesNoCopy:buffer.release() |
| 156 length:read]; | 157 length:read]; |
| 157 return logData; | 158 return logData; |
| 158 } | 159 } |
| 159 | 160 |
| 160 #pragma mark - Private | 161 #pragma mark - Private |
| 161 | 162 |
| 162 - (rtc::LoggingSeverity)rtcSeverity { | 163 - (rtc::LoggingSeverity)rtcSeverity { |
| 163 switch (_severity) { | 164 switch (_severity) { |
| 164 case RTCFileLoggerSeverityVerbose: | 165 case RTCFileLoggerSeverityVerbose: |
| 165 return rtc::LS_VERBOSE; | 166 return rtc::LS_VERBOSE; |
| 166 case RTCFileLoggerSeverityInfo: | 167 case RTCFileLoggerSeverityInfo: |
| 167 return rtc::LS_INFO; | 168 return rtc::LS_INFO; |
| 168 case RTCFileLoggerSeverityWarning: | 169 case RTCFileLoggerSeverityWarning: |
| 169 return rtc::LS_WARNING; | 170 return rtc::LS_WARNING; |
| 170 case RTCFileLoggerSeverityError: | 171 case RTCFileLoggerSeverityError: |
| 171 return rtc::LS_ERROR; | 172 return rtc::LS_ERROR; |
| 172 } | 173 } |
| 173 } | 174 } |
| 174 | 175 |
| 175 @end | 176 @end |
| OLD | NEW |