Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2015 Google Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #import "RTCFileLogger.h" |
| 29 | |
| 30 | #include "webrtc/base/checks.h" |
tkchin | 28bae02 | 2015-07-23 12:27:02 -0700 | [diff] [blame] | 31 | #include "webrtc/base/filerotatingstream.h" |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 32 | #include "webrtc/base/logging.h" |
tkchin | 28bae02 | 2015-07-23 12:27:02 -0700 | [diff] [blame] | 33 | #include "webrtc/base/logsinks.h" |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 34 | #include "webrtc/base/scoped_ptr.h" |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 35 | |
tkchin | 28bae02 | 2015-07-23 12:27:02 -0700 | [diff] [blame] | 36 | NSString *const kDefaultLogDirName = @"webrtc_logs"; |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 37 | NSUInteger const kDefaultMaxFileSize = 10 * 1024 * 1024; // 10MB. |
| 38 | |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 39 | @implementation RTCFileLogger { |
| 40 | BOOL _hasStarted; |
tkchin | 28bae02 | 2015-07-23 12:27:02 -0700 | [diff] [blame] | 41 | NSString *_dirPath; |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 42 | NSUInteger _maxFileSize; |
tkchin | 28bae02 | 2015-07-23 12:27:02 -0700 | [diff] [blame] | 43 | rtc::scoped_ptr<rtc::CallSessionFileRotatingLogSink> _logSink; |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | @synthesize severity = _severity; |
| 47 | |
| 48 | - (instancetype)init { |
| 49 | NSArray *paths = NSSearchPathForDirectoriesInDomains( |
| 50 | NSDocumentDirectory, NSUserDomainMask, YES); |
| 51 | NSString *documentsDirPath = [paths firstObject]; |
tkchin | 28bae02 | 2015-07-23 12:27:02 -0700 | [diff] [blame] | 52 | NSString *defaultDirPath = |
| 53 | [documentsDirPath stringByAppendingPathComponent:kDefaultLogDirName]; |
| 54 | return [self initWithDirPath:defaultDirPath |
| 55 | maxFileSize:kDefaultMaxFileSize]; |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 56 | } |
| 57 | |
tkchin | 28bae02 | 2015-07-23 12:27:02 -0700 | [diff] [blame] | 58 | - (instancetype)initWithDirPath:(NSString *)dirPath |
| 59 | maxFileSize:(NSUInteger)maxFileSize { |
| 60 | NSParameterAssert(dirPath.length); |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 61 | NSParameterAssert(maxFileSize); |
| 62 | if (self = [super init]) { |
tkchin | 28bae02 | 2015-07-23 12:27:02 -0700 | [diff] [blame] | 63 | BOOL isDir = NO; |
| 64 | NSFileManager *fileManager = [NSFileManager defaultManager]; |
| 65 | if ([fileManager fileExistsAtPath:dirPath isDirectory:&isDir]) { |
| 66 | if (!isDir) { |
| 67 | // Bail if something already exists there. |
| 68 | return nil; |
| 69 | } |
| 70 | } else { |
| 71 | if (![fileManager createDirectoryAtPath:dirPath |
| 72 | withIntermediateDirectories:NO |
| 73 | attributes:nil |
| 74 | error:nil]) { |
| 75 | // Bail if we failed to create a directory. |
| 76 | return nil; |
| 77 | } |
| 78 | } |
| 79 | _dirPath = dirPath; |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 80 | _maxFileSize = maxFileSize; |
| 81 | _severity = kRTCFileLoggerSeverityInfo; |
| 82 | } |
| 83 | return self; |
| 84 | } |
| 85 | |
| 86 | - (void)dealloc { |
| 87 | [self stop]; |
| 88 | } |
| 89 | |
| 90 | - (void)start { |
| 91 | if (_hasStarted) { |
| 92 | return; |
| 93 | } |
tkchin | 28bae02 | 2015-07-23 12:27:02 -0700 | [diff] [blame] | 94 | _logSink.reset(new rtc::CallSessionFileRotatingLogSink(_dirPath.UTF8String, |
| 95 | _maxFileSize)); |
| 96 | if (!_logSink->Init()) { |
| 97 | LOG(LS_ERROR) << "Failed to open log files at path: " |
| 98 | << _dirPath.UTF8String; |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 99 | _logSink.reset(); |
| 100 | return; |
| 101 | } |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 102 | rtc::LogMessage::LogThreads(true); |
| 103 | rtc::LogMessage::LogTimestamps(true); |
| 104 | rtc::LogMessage::AddLogToStream(_logSink.get(), [self rtcSeverity]); |
| 105 | _hasStarted = YES; |
| 106 | } |
| 107 | |
| 108 | - (void)stop { |
| 109 | if (!_hasStarted) { |
| 110 | return; |
| 111 | } |
| 112 | DCHECK(_logSink); |
| 113 | rtc::LogMessage::RemoveLogToStream(_logSink.get()); |
| 114 | _hasStarted = NO; |
tkchin | 28bae02 | 2015-07-23 12:27:02 -0700 | [diff] [blame] | 115 | _logSink.reset(); |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | - (NSData *)logData { |
| 119 | if (_hasStarted) { |
| 120 | return nil; |
| 121 | } |
tkchin | 28bae02 | 2015-07-23 12:27:02 -0700 | [diff] [blame] | 122 | NSMutableData* logData = [NSMutableData data]; |
| 123 | rtc::scoped_ptr<rtc::CallSessionFileRotatingStream> stream( |
| 124 | new rtc::CallSessionFileRotatingStream(_dirPath.UTF8String)); |
| 125 | if (!stream->Open()) { |
| 126 | return logData; |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 127 | } |
tkchin | 28bae02 | 2015-07-23 12:27:02 -0700 | [diff] [blame] | 128 | size_t bufferSize = 0; |
| 129 | if (!stream->GetSize(&bufferSize) || bufferSize == 0) { |
| 130 | return logData; |
| 131 | } |
| 132 | size_t read = 0; |
| 133 | // Allocate memory using malloc so we can pass it direcly to NSData without |
| 134 | // copying. |
| 135 | rtc::scoped_ptr<uint8_t[]> buffer(static_cast<uint8_t*>(malloc(bufferSize))); |
| 136 | stream->ReadAll(buffer.get(), bufferSize, &read, nullptr); |
| 137 | logData = [[NSMutableData alloc] initWithBytesNoCopy:buffer.release() |
| 138 | length:read]; |
| 139 | return logData; |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | #pragma mark - Private |
| 143 | |
Zeke Chin | 2d3b7e2 | 2015-07-14 12:55:44 -0700 | [diff] [blame] | 144 | - (rtc::LoggingSeverity)rtcSeverity { |
| 145 | switch (_severity) { |
| 146 | case kRTCFileLoggerSeverityVerbose: |
| 147 | return rtc::LS_VERBOSE; |
| 148 | case kRTCFileLoggerSeverityInfo: |
| 149 | return rtc::LS_INFO; |
| 150 | case kRTCFileLoggerSeverityWarning: |
| 151 | return rtc::LS_WARNING; |
| 152 | case kRTCFileLoggerSeverityError: |
| 153 | return rtc::LS_ERROR; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | @end |