blob: 44ada3e22e2a549dfc65bb88c45f0858aed50b0b [file] [log] [blame]
Zeke Chin2d3b7e22015-07-14 12:55:44 -07001/*
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"
tkchin28bae022015-07-23 12:27:02 -070031#include "webrtc/base/filerotatingstream.h"
Zeke Chin2d3b7e22015-07-14 12:55:44 -070032#include "webrtc/base/logging.h"
tkchin28bae022015-07-23 12:27:02 -070033#include "webrtc/base/logsinks.h"
Zeke Chin2d3b7e22015-07-14 12:55:44 -070034#include "webrtc/base/scoped_ptr.h"
Zeke Chin2d3b7e22015-07-14 12:55:44 -070035
tkchin28bae022015-07-23 12:27:02 -070036NSString *const kDefaultLogDirName = @"webrtc_logs";
Zeke Chin2d3b7e22015-07-14 12:55:44 -070037NSUInteger const kDefaultMaxFileSize = 10 * 1024 * 1024; // 10MB.
hayscd02b0fa2015-12-08 13:59:05 -080038const char *kRTCFileLoggerRotatingLogPrefix = "rotating_log";
Zeke Chin2d3b7e22015-07-14 12:55:44 -070039
Zeke Chin2d3b7e22015-07-14 12:55:44 -070040@implementation RTCFileLogger {
41 BOOL _hasStarted;
tkchin28bae022015-07-23 12:27:02 -070042 NSString *_dirPath;
Zeke Chin2d3b7e22015-07-14 12:55:44 -070043 NSUInteger _maxFileSize;
hayscd02b0fa2015-12-08 13:59:05 -080044 rtc::scoped_ptr<rtc::FileRotatingLogSink> _logSink;
Zeke Chin2d3b7e22015-07-14 12:55:44 -070045}
46
47@synthesize severity = _severity;
hayscd02b0fa2015-12-08 13:59:05 -080048@synthesize rotationType = _rotationType;
Zeke Chin2d3b7e22015-07-14 12:55:44 -070049
50- (instancetype)init {
51 NSArray *paths = NSSearchPathForDirectoriesInDomains(
52 NSDocumentDirectory, NSUserDomainMask, YES);
53 NSString *documentsDirPath = [paths firstObject];
tkchin28bae022015-07-23 12:27:02 -070054 NSString *defaultDirPath =
55 [documentsDirPath stringByAppendingPathComponent:kDefaultLogDirName];
56 return [self initWithDirPath:defaultDirPath
57 maxFileSize:kDefaultMaxFileSize];
Zeke Chin2d3b7e22015-07-14 12:55:44 -070058}
59
tkchin28bae022015-07-23 12:27:02 -070060- (instancetype)initWithDirPath:(NSString *)dirPath
61 maxFileSize:(NSUInteger)maxFileSize {
hayscd02b0fa2015-12-08 13:59:05 -080062 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 {
tkchin28bae022015-07-23 12:27:02 -070070 NSParameterAssert(dirPath.length);
Zeke Chin2d3b7e22015-07-14 12:55:44 -070071 NSParameterAssert(maxFileSize);
72 if (self = [super init]) {
tkchin28bae022015-07-23 12:27:02 -070073 BOOL isDir = NO;
74 NSFileManager *fileManager = [NSFileManager defaultManager];
75 if ([fileManager fileExistsAtPath:dirPath isDirectory:&isDir]) {
76 if (!isDir) {
77 // Bail if something already exists there.
78 return nil;
79 }
80 } else {
81 if (![fileManager createDirectoryAtPath:dirPath
82 withIntermediateDirectories:NO
83 attributes:nil
84 error:nil]) {
85 // Bail if we failed to create a directory.
86 return nil;
87 }
88 }
89 _dirPath = dirPath;
Zeke Chin2d3b7e22015-07-14 12:55:44 -070090 _maxFileSize = maxFileSize;
91 _severity = kRTCFileLoggerSeverityInfo;
92 }
93 return self;
94}
95
96- (void)dealloc {
97 [self stop];
98}
99
100- (void)start {
101 if (_hasStarted) {
102 return;
103 }
hayscd02b0fa2015-12-08 13:59:05 -0800104 switch (_rotationType) {
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 }
tkchin28bae022015-07-23 12:27:02 -0700118 if (!_logSink->Init()) {
119 LOG(LS_ERROR) << "Failed to open log files at path: "
120 << _dirPath.UTF8String;
Zeke Chin2d3b7e22015-07-14 12:55:44 -0700121 _logSink.reset();
122 return;
123 }
Zeke Chin2d3b7e22015-07-14 12:55:44 -0700124 rtc::LogMessage::LogThreads(true);
125 rtc::LogMessage::LogTimestamps(true);
126 rtc::LogMessage::AddLogToStream(_logSink.get(), [self rtcSeverity]);
127 _hasStarted = YES;
128}
129
130- (void)stop {
131 if (!_hasStarted) {
132 return;
133 }
henrikg91d6ede2015-09-17 00:24:34 -0700134 RTC_DCHECK(_logSink);
Zeke Chin2d3b7e22015-07-14 12:55:44 -0700135 rtc::LogMessage::RemoveLogToStream(_logSink.get());
136 _hasStarted = NO;
tkchin28bae022015-07-23 12:27:02 -0700137 _logSink.reset();
Zeke Chin2d3b7e22015-07-14 12:55:44 -0700138}
139
140- (NSData *)logData {
141 if (_hasStarted) {
142 return nil;
143 }
tkchin28bae022015-07-23 12:27:02 -0700144 NSMutableData* logData = [NSMutableData data];
hayscd02b0fa2015-12-08 13:59:05 -0800145 rtc::scoped_ptr<rtc::FileRotatingStream> stream;
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 }
tkchin28bae022015-07-23 12:27:02 -0700156 if (!stream->Open()) {
157 return logData;
Zeke Chin2d3b7e22015-07-14 12:55:44 -0700158 }
tkchin28bae022015-07-23 12:27:02 -0700159 size_t bufferSize = 0;
160 if (!stream->GetSize(&bufferSize) || bufferSize == 0) {
161 return logData;
162 }
163 size_t read = 0;
164 // Allocate memory using malloc so we can pass it direcly to NSData without
165 // copying.
166 rtc::scoped_ptr<uint8_t[]> buffer(static_cast<uint8_t*>(malloc(bufferSize)));
167 stream->ReadAll(buffer.get(), bufferSize, &read, nullptr);
168 logData = [[NSMutableData alloc] initWithBytesNoCopy:buffer.release()
169 length:read];
170 return logData;
Zeke Chin2d3b7e22015-07-14 12:55:44 -0700171}
172
173#pragma mark - Private
174
Zeke Chin2d3b7e22015-07-14 12:55:44 -0700175- (rtc::LoggingSeverity)rtcSeverity {
176 switch (_severity) {
177 case kRTCFileLoggerSeverityVerbose:
178 return rtc::LS_VERBOSE;
179 case kRTCFileLoggerSeverityInfo:
180 return rtc::LS_INFO;
181 case kRTCFileLoggerSeverityWarning:
182 return rtc::LS_WARNING;
183 case kRTCFileLoggerSeverityError:
184 return rtc::LS_ERROR;
185 }
186}
187
188@end