| OLD | NEW |
| 1 /* | 1 /* |
| 2 * libjingle | 2 * libjingle |
| 3 * Copyright 2015 Google Inc. | 3 * Copyright 2015 Google Inc. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions are met: | 6 * modification, are permitted provided that the following conditions are met: |
| 7 * | 7 * |
| 8 * 1. Redistributions of source code must retain the above copyright notice, | 8 * 1. Redistributions of source code must retain the above copyright notice, |
| 9 * this list of conditions and the following disclaimer. | 9 * this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright notice, | 10 * 2. Redistributions in binary form must reproduce the above copyright notice, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 #import "RTCFileLogger.h" | 28 #import "RTCFileLogger.h" |
| 29 | 29 |
| 30 #include "webrtc/base/checks.h" | 30 #include "webrtc/base/checks.h" |
| 31 #include "webrtc/base/filerotatingstream.h" | 31 #include "webrtc/base/filerotatingstream.h" |
| 32 #include "webrtc/base/logging.h" | 32 #include "webrtc/base/logging.h" |
| 33 #include "webrtc/base/logsinks.h" | 33 #include "webrtc/base/logsinks.h" |
| 34 #include "webrtc/base/scoped_ptr.h" | 34 #include "webrtc/base/scoped_ptr.h" |
| 35 | 35 |
| 36 NSString *const kDefaultLogDirName = @"webrtc_logs"; | 36 NSString *const kDefaultLogDirName = @"webrtc_logs"; |
| 37 NSUInteger const kDefaultMaxFileSize = 10 * 1024 * 1024; // 10MB. | 37 NSUInteger const kDefaultMaxFileSize = 10 * 1024 * 1024; // 10MB. |
| 38 const char *kRTCFileLoggerRotatingLogPrefix = "rotating_log"; |
| 38 | 39 |
| 39 @implementation RTCFileLogger { | 40 @implementation RTCFileLogger { |
| 40 BOOL _hasStarted; | 41 BOOL _hasStarted; |
| 41 NSString *_dirPath; | 42 NSString *_dirPath; |
| 42 NSUInteger _maxFileSize; | 43 NSUInteger _maxFileSize; |
| 43 rtc::scoped_ptr<rtc::CallSessionFileRotatingLogSink> _logSink; | 44 rtc::scoped_ptr<rtc::FileRotatingLogSink> _logSink; |
| 44 } | 45 } |
| 45 | 46 |
| 46 @synthesize severity = _severity; | 47 @synthesize severity = _severity; |
| 48 @synthesize rotationType = _rotationType; |
| 47 | 49 |
| 48 - (instancetype)init { | 50 - (instancetype)init { |
| 49 NSArray *paths = NSSearchPathForDirectoriesInDomains( | 51 NSArray *paths = NSSearchPathForDirectoriesInDomains( |
| 50 NSDocumentDirectory, NSUserDomainMask, YES); | 52 NSDocumentDirectory, NSUserDomainMask, YES); |
| 51 NSString *documentsDirPath = [paths firstObject]; | 53 NSString *documentsDirPath = [paths firstObject]; |
| 52 NSString *defaultDirPath = | 54 NSString *defaultDirPath = |
| 53 [documentsDirPath stringByAppendingPathComponent:kDefaultLogDirName]; | 55 [documentsDirPath stringByAppendingPathComponent:kDefaultLogDirName]; |
| 54 return [self initWithDirPath:defaultDirPath | 56 return [self initWithDirPath:defaultDirPath |
| 55 maxFileSize:kDefaultMaxFileSize]; | 57 maxFileSize:kDefaultMaxFileSize]; |
| 56 } | 58 } |
| 57 | 59 |
| 58 - (instancetype)initWithDirPath:(NSString *)dirPath | 60 - (instancetype)initWithDirPath:(NSString *)dirPath |
| 59 maxFileSize:(NSUInteger)maxFileSize { | 61 maxFileSize:(NSUInteger)maxFileSize { |
| 62 return [self initWithDirPath:dirPath |
| 63 maxFileSize:maxFileSize |
| 64 rotationType:kRTCFileLoggerTypeCall]; |
| 65 } |
| 66 |
| 67 - (instancetype)initWithDirPath:(NSString *)dirPath |
| 68 maxFileSize:(NSUInteger)maxFileSize |
| 69 rotationType:(RTCFileLoggerRotationType)rotationType { |
| 60 NSParameterAssert(dirPath.length); | 70 NSParameterAssert(dirPath.length); |
| 61 NSParameterAssert(maxFileSize); | 71 NSParameterAssert(maxFileSize); |
| 62 if (self = [super init]) { | 72 if (self = [super init]) { |
| 63 BOOL isDir = NO; | 73 BOOL isDir = NO; |
| 64 NSFileManager *fileManager = [NSFileManager defaultManager]; | 74 NSFileManager *fileManager = [NSFileManager defaultManager]; |
| 65 if ([fileManager fileExistsAtPath:dirPath isDirectory:&isDir]) { | 75 if ([fileManager fileExistsAtPath:dirPath isDirectory:&isDir]) { |
| 66 if (!isDir) { | 76 if (!isDir) { |
| 67 // Bail if something already exists there. | 77 // Bail if something already exists there. |
| 68 return nil; | 78 return nil; |
| 69 } | 79 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 84 } | 94 } |
| 85 | 95 |
| 86 - (void)dealloc { | 96 - (void)dealloc { |
| 87 [self stop]; | 97 [self stop]; |
| 88 } | 98 } |
| 89 | 99 |
| 90 - (void)start { | 100 - (void)start { |
| 91 if (_hasStarted) { | 101 if (_hasStarted) { |
| 92 return; | 102 return; |
| 93 } | 103 } |
| 94 _logSink.reset(new rtc::CallSessionFileRotatingLogSink(_dirPath.UTF8String, | 104 switch (_rotationType) { |
| 95 _maxFileSize)); | 105 case kRTCFileLoggerTypeApp: |
| 106 _logSink.reset( |
| 107 new rtc::FileRotatingLogSink(_dirPath.UTF8String, |
| 108 kRTCFileLoggerRotatingLogPrefix, |
| 109 _maxFileSize, |
| 110 _maxFileSize / 10)); |
| 111 break; |
| 112 case kRTCFileLoggerTypeCall: |
| 113 _logSink.reset( |
| 114 new rtc::CallSessionFileRotatingLogSink(_dirPath.UTF8String, |
| 115 _maxFileSize)); |
| 116 break; |
| 117 } |
| 96 if (!_logSink->Init()) { | 118 if (!_logSink->Init()) { |
| 97 LOG(LS_ERROR) << "Failed to open log files at path: " | 119 LOG(LS_ERROR) << "Failed to open log files at path: " |
| 98 << _dirPath.UTF8String; | 120 << _dirPath.UTF8String; |
| 99 _logSink.reset(); | 121 _logSink.reset(); |
| 100 return; | 122 return; |
| 101 } | 123 } |
| 102 rtc::LogMessage::LogThreads(true); | 124 rtc::LogMessage::LogThreads(true); |
| 103 rtc::LogMessage::LogTimestamps(true); | 125 rtc::LogMessage::LogTimestamps(true); |
| 104 rtc::LogMessage::AddLogToStream(_logSink.get(), [self rtcSeverity]); | 126 rtc::LogMessage::AddLogToStream(_logSink.get(), [self rtcSeverity]); |
| 105 _hasStarted = YES; | 127 _hasStarted = YES; |
| 106 } | 128 } |
| 107 | 129 |
| 108 - (void)stop { | 130 - (void)stop { |
| 109 if (!_hasStarted) { | 131 if (!_hasStarted) { |
| 110 return; | 132 return; |
| 111 } | 133 } |
| 112 RTC_DCHECK(_logSink); | 134 RTC_DCHECK(_logSink); |
| 113 rtc::LogMessage::RemoveLogToStream(_logSink.get()); | 135 rtc::LogMessage::RemoveLogToStream(_logSink.get()); |
| 114 _hasStarted = NO; | 136 _hasStarted = NO; |
| 115 _logSink.reset(); | 137 _logSink.reset(); |
| 116 } | 138 } |
| 117 | 139 |
| 118 - (NSData *)logData { | 140 - (NSData *)logData { |
| 119 if (_hasStarted) { | 141 if (_hasStarted) { |
| 120 return nil; | 142 return nil; |
| 121 } | 143 } |
| 122 NSMutableData* logData = [NSMutableData data]; | 144 NSMutableData* logData = [NSMutableData data]; |
| 123 rtc::scoped_ptr<rtc::CallSessionFileRotatingStream> stream( | 145 rtc::scoped_ptr<rtc::FileRotatingStream> stream; |
| 124 new rtc::CallSessionFileRotatingStream(_dirPath.UTF8String)); | 146 switch(_rotationType) { |
| 147 case kRTCFileLoggerTypeApp: |
| 148 stream.reset( |
| 149 new rtc::FileRotatingStream(_dirPath.UTF8String, |
| 150 kRTCFileLoggerRotatingLogPrefix)); |
| 151 break; |
| 152 case kRTCFileLoggerTypeCall: |
| 153 stream.reset(new rtc::CallSessionFileRotatingStream(_dirPath.UTF8String)); |
| 154 break; |
| 155 } |
| 125 if (!stream->Open()) { | 156 if (!stream->Open()) { |
| 126 return logData; | 157 return logData; |
| 127 } | 158 } |
| 128 size_t bufferSize = 0; | 159 size_t bufferSize = 0; |
| 129 if (!stream->GetSize(&bufferSize) || bufferSize == 0) { | 160 if (!stream->GetSize(&bufferSize) || bufferSize == 0) { |
| 130 return logData; | 161 return logData; |
| 131 } | 162 } |
| 132 size_t read = 0; | 163 size_t read = 0; |
| 133 // Allocate memory using malloc so we can pass it direcly to NSData without | 164 // Allocate memory using malloc so we can pass it direcly to NSData without |
| 134 // copying. | 165 // copying. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 148 case kRTCFileLoggerSeverityInfo: | 179 case kRTCFileLoggerSeverityInfo: |
| 149 return rtc::LS_INFO; | 180 return rtc::LS_INFO; |
| 150 case kRTCFileLoggerSeverityWarning: | 181 case kRTCFileLoggerSeverityWarning: |
| 151 return rtc::LS_WARNING; | 182 return rtc::LS_WARNING; |
| 152 case kRTCFileLoggerSeverityError: | 183 case kRTCFileLoggerSeverityError: |
| 153 return rtc::LS_ERROR; | 184 return rtc::LS_ERROR; |
| 154 } | 185 } |
| 155 } | 186 } |
| 156 | 187 |
| 157 @end | 188 @end |
| OLD | NEW |