blob: 3080ebc080ec9b13fe421afdcfb8e7930cb38891 [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.
38
Zeke Chin2d3b7e22015-07-14 12:55:44 -070039@implementation RTCFileLogger {
40 BOOL _hasStarted;
tkchin28bae022015-07-23 12:27:02 -070041 NSString *_dirPath;
Zeke Chin2d3b7e22015-07-14 12:55:44 -070042 NSUInteger _maxFileSize;
tkchin28bae022015-07-23 12:27:02 -070043 rtc::scoped_ptr<rtc::CallSessionFileRotatingLogSink> _logSink;
Zeke Chin2d3b7e22015-07-14 12:55:44 -070044}
45
46@synthesize severity = _severity;
47
48- (instancetype)init {
49 NSArray *paths = NSSearchPathForDirectoriesInDomains(
50 NSDocumentDirectory, NSUserDomainMask, YES);
51 NSString *documentsDirPath = [paths firstObject];
tkchin28bae022015-07-23 12:27:02 -070052 NSString *defaultDirPath =
53 [documentsDirPath stringByAppendingPathComponent:kDefaultLogDirName];
54 return [self initWithDirPath:defaultDirPath
55 maxFileSize:kDefaultMaxFileSize];
Zeke Chin2d3b7e22015-07-14 12:55:44 -070056}
57
tkchin28bae022015-07-23 12:27:02 -070058- (instancetype)initWithDirPath:(NSString *)dirPath
59 maxFileSize:(NSUInteger)maxFileSize {
60 NSParameterAssert(dirPath.length);
Zeke Chin2d3b7e22015-07-14 12:55:44 -070061 NSParameterAssert(maxFileSize);
62 if (self = [super init]) {
tkchin28bae022015-07-23 12:27:02 -070063 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 Chin2d3b7e22015-07-14 12:55:44 -070080 _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 }
tkchin28bae022015-07-23 12:27:02 -070094 _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 Chin2d3b7e22015-07-14 12:55:44 -070099 _logSink.reset();
100 return;
101 }
Zeke Chin2d3b7e22015-07-14 12:55:44 -0700102 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;
tkchin28bae022015-07-23 12:27:02 -0700115 _logSink.reset();
Zeke Chin2d3b7e22015-07-14 12:55:44 -0700116}
117
118- (NSData *)logData {
119 if (_hasStarted) {
120 return nil;
121 }
tkchin28bae022015-07-23 12:27:02 -0700122 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 Chin2d3b7e22015-07-14 12:55:44 -0700127 }
tkchin28bae022015-07-23 12:27:02 -0700128 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 Chin2d3b7e22015-07-14 12:55:44 -0700140}
141
142#pragma mark - Private
143
Zeke Chin2d3b7e22015-07-14 12:55:44 -0700144- (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