Add oldest rotation type option to RTCFileLogger

BUG=

Review URL: https://codereview.webrtc.org/1432753003

Cr-Commit-Position: refs/heads/master@{#10945}
This commit is contained in:
haysc 2015-12-08 13:59:05 -08:00 committed by Commit bot
parent 5e465c33ca
commit d02b0fab76
3 changed files with 57 additions and 9 deletions

View File

@ -35,15 +35,17 @@
NSString *const kDefaultLogDirName = @"webrtc_logs";
NSUInteger const kDefaultMaxFileSize = 10 * 1024 * 1024; // 10MB.
const char *kRTCFileLoggerRotatingLogPrefix = "rotating_log";
@implementation RTCFileLogger {
BOOL _hasStarted;
NSString *_dirPath;
NSUInteger _maxFileSize;
rtc::scoped_ptr<rtc::CallSessionFileRotatingLogSink> _logSink;
rtc::scoped_ptr<rtc::FileRotatingLogSink> _logSink;
}
@synthesize severity = _severity;
@synthesize rotationType = _rotationType;
- (instancetype)init {
NSArray *paths = NSSearchPathForDirectoriesInDomains(
@ -57,6 +59,14 @@ NSUInteger const kDefaultMaxFileSize = 10 * 1024 * 1024; // 10MB.
- (instancetype)initWithDirPath:(NSString *)dirPath
maxFileSize:(NSUInteger)maxFileSize {
return [self initWithDirPath:dirPath
maxFileSize:maxFileSize
rotationType:kRTCFileLoggerTypeCall];
}
- (instancetype)initWithDirPath:(NSString *)dirPath
maxFileSize:(NSUInteger)maxFileSize
rotationType:(RTCFileLoggerRotationType)rotationType {
NSParameterAssert(dirPath.length);
NSParameterAssert(maxFileSize);
if (self = [super init]) {
@ -91,8 +101,20 @@ NSUInteger const kDefaultMaxFileSize = 10 * 1024 * 1024; // 10MB.
if (_hasStarted) {
return;
}
_logSink.reset(new rtc::CallSessionFileRotatingLogSink(_dirPath.UTF8String,
_maxFileSize));
switch (_rotationType) {
case kRTCFileLoggerTypeApp:
_logSink.reset(
new rtc::FileRotatingLogSink(_dirPath.UTF8String,
kRTCFileLoggerRotatingLogPrefix,
_maxFileSize,
_maxFileSize / 10));
break;
case kRTCFileLoggerTypeCall:
_logSink.reset(
new rtc::CallSessionFileRotatingLogSink(_dirPath.UTF8String,
_maxFileSize));
break;
}
if (!_logSink->Init()) {
LOG(LS_ERROR) << "Failed to open log files at path: "
<< _dirPath.UTF8String;
@ -120,8 +142,17 @@ NSUInteger const kDefaultMaxFileSize = 10 * 1024 * 1024; // 10MB.
return nil;
}
NSMutableData* logData = [NSMutableData data];
rtc::scoped_ptr<rtc::CallSessionFileRotatingStream> stream(
new rtc::CallSessionFileRotatingStream(_dirPath.UTF8String));
rtc::scoped_ptr<rtc::FileRotatingStream> stream;
switch(_rotationType) {
case kRTCFileLoggerTypeApp:
stream.reset(
new rtc::FileRotatingStream(_dirPath.UTF8String,
kRTCFileLoggerRotatingLogPrefix));
break;
case kRTCFileLoggerTypeCall:
stream.reset(new rtc::CallSessionFileRotatingStream(_dirPath.UTF8String));
break;
}
if (!stream->Open()) {
return logData;
}

View File

@ -39,21 +39,38 @@ typedef NS_ENUM(NSUInteger, RTCFileLoggerSeverity) {
kRTCFileLoggerSeverityError
};
typedef NS_ENUM(NSUInteger, RTCFileLoggerRotationType) {
kRTCFileLoggerTypeCall,
kRTCFileLoggerTypeApp,
};
// This class intercepts WebRTC logs and saves them to a file. The file size
// will not exceed the given maximum bytesize. When the maximum bytesize is
// reached logs from the beginning and the end are preserved while the middle
// section is overwritten instead.
// reached, logs are rotated according to the rotationType specified.
// For kRTCFileLoggerTypeCall, logs from the beginning and the end
// are preserved while the middle section is overwritten instead.
// For kRTCFileLoggerTypeApp, the oldest log is overwritten.
// This class is not threadsafe.
@interface RTCFileLogger : NSObject
// The severity level to capture. The default is kRTCFileLoggerSeverityInfo.
@property(nonatomic, assign) RTCFileLoggerSeverity severity;
// Default constructor provides default settings for dir path and file size.
// The rotation type for this file logger. The default is
// kRTCFileLoggerTypeCall.
@property(nonatomic, readonly) RTCFileLoggerRotationType rotationType;
// Default constructor provides default settings for dir path, file size and
// rotation type.
- (instancetype)init;
// Create file logger with default rotation type.
- (instancetype)initWithDirPath:(NSString *)dirPath
maxFileSize:(NSUInteger)maxFileSize;
- (instancetype)initWithDirPath:(NSString *)dirPath
maxFileSize:(NSUInteger)maxFileSize
rotationType:(RTCFileLoggerRotationType)rotationType
NS_DESIGNATED_INITIALIZER;
// Starts writing WebRTC logs to disk if not already started. Overwrites any

View File

@ -281,7 +281,7 @@ void FileRotatingStream::RotateFiles() {
// Rotates the files by deleting the file at |rotation_index_|, which is the
// oldest file and then renaming the newer files to have an incremented index.
// See header file comments for example.
RTC_DCHECK_LE(rotation_index_, file_names_.size());
RTC_DCHECK_LT(rotation_index_, file_names_.size());
std::string file_to_delete = file_names_[rotation_index_];
if (Filesystem::IsFile(file_to_delete)) {
if (!Filesystem::DeleteFile(file_to_delete)) {