Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1395)

Side by Side Diff: webrtc/sdk/objc/Framework/Classes/RTCFileLogger.mm

Issue 2890513002: Revert of Split iOS sdk in to separate targets (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright 2015 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #import "WebRTC/RTCFileLogger.h"
12
13 #include <memory>
14
15 #include "webrtc/base/checks.h"
16 #include "webrtc/base/filerotatingstream.h"
17 #include "webrtc/base/logging.h"
18 #include "webrtc/base/logsinks.h"
19
20 NSString *const kDefaultLogDirName = @"webrtc_logs";
21 NSUInteger const kDefaultMaxFileSize = 10 * 1024 * 1024; // 10MB.
22 const char *kRTCFileLoggerRotatingLogPrefix = "rotating_log";
23
24 @implementation RTCFileLogger {
25 BOOL _hasStarted;
26 NSString *_dirPath;
27 NSUInteger _maxFileSize;
28 std::unique_ptr<rtc::FileRotatingLogSink> _logSink;
29 }
30
31 @synthesize severity = _severity;
32 @synthesize rotationType = _rotationType;
33 @synthesize shouldDisableBuffering = _shouldDisableBuffering;
34
35 - (instancetype)init {
36 NSArray *paths = NSSearchPathForDirectoriesInDomains(
37 NSDocumentDirectory, NSUserDomainMask, YES);
38 NSString *documentsDirPath = [paths firstObject];
39 NSString *defaultDirPath =
40 [documentsDirPath stringByAppendingPathComponent:kDefaultLogDirName];
41 return [self initWithDirPath:defaultDirPath
42 maxFileSize:kDefaultMaxFileSize];
43 }
44
45 - (instancetype)initWithDirPath:(NSString *)dirPath
46 maxFileSize:(NSUInteger)maxFileSize {
47 return [self initWithDirPath:dirPath
48 maxFileSize:maxFileSize
49 rotationType:RTCFileLoggerTypeCall];
50 }
51
52 - (instancetype)initWithDirPath:(NSString *)dirPath
53 maxFileSize:(NSUInteger)maxFileSize
54 rotationType:(RTCFileLoggerRotationType)rotationType {
55 NSParameterAssert(dirPath.length);
56 NSParameterAssert(maxFileSize);
57 if (self = [super init]) {
58 BOOL isDir = NO;
59 NSFileManager *fileManager = [NSFileManager defaultManager];
60 if ([fileManager fileExistsAtPath:dirPath isDirectory:&isDir]) {
61 if (!isDir) {
62 // Bail if something already exists there.
63 return nil;
64 }
65 } else {
66 if (![fileManager createDirectoryAtPath:dirPath
67 withIntermediateDirectories:NO
68 attributes:nil
69 error:nil]) {
70 // Bail if we failed to create a directory.
71 return nil;
72 }
73 }
74 _dirPath = dirPath;
75 _maxFileSize = maxFileSize;
76 _severity = RTCFileLoggerSeverityInfo;
77 }
78 return self;
79 }
80
81 - (void)dealloc {
82 [self stop];
83 }
84
85 - (void)start {
86 if (_hasStarted) {
87 return;
88 }
89 switch (_rotationType) {
90 case RTCFileLoggerTypeApp:
91 _logSink.reset(
92 new rtc::FileRotatingLogSink(_dirPath.UTF8String,
93 kRTCFileLoggerRotatingLogPrefix,
94 _maxFileSize,
95 _maxFileSize / 10));
96 break;
97 case RTCFileLoggerTypeCall:
98 _logSink.reset(
99 new rtc::CallSessionFileRotatingLogSink(_dirPath.UTF8String,
100 _maxFileSize));
101 break;
102 }
103 if (!_logSink->Init()) {
104 LOG(LS_ERROR) << "Failed to open log files at path: "
105 << _dirPath.UTF8String;
106 _logSink.reset();
107 return;
108 }
109 if (_shouldDisableBuffering) {
110 _logSink->DisableBuffering();
111 }
112 rtc::LogMessage::LogThreads(true);
113 rtc::LogMessage::LogTimestamps(true);
114 rtc::LogMessage::AddLogToStream(_logSink.get(), [self rtcSeverity]);
115 _hasStarted = YES;
116 }
117
118 - (void)stop {
119 if (!_hasStarted) {
120 return;
121 }
122 RTC_DCHECK(_logSink);
123 rtc::LogMessage::RemoveLogToStream(_logSink.get());
124 _hasStarted = NO;
125 _logSink.reset();
126 }
127
128 - (NSData *)logData {
129 if (_hasStarted) {
130 return nil;
131 }
132 NSMutableData* logData = [NSMutableData data];
133 std::unique_ptr<rtc::FileRotatingStream> stream;
134 switch(_rotationType) {
135 case RTCFileLoggerTypeApp:
136 stream.reset(
137 new rtc::FileRotatingStream(_dirPath.UTF8String,
138 kRTCFileLoggerRotatingLogPrefix));
139 break;
140 case RTCFileLoggerTypeCall:
141 stream.reset(new rtc::CallSessionFileRotatingStream(_dirPath.UTF8String));
142 break;
143 }
144 if (!stream->Open()) {
145 return logData;
146 }
147 size_t bufferSize = 0;
148 if (!stream->GetSize(&bufferSize) || bufferSize == 0) {
149 return logData;
150 }
151 size_t read = 0;
152 // Allocate memory using malloc so we can pass it direcly to NSData without
153 // copying.
154 std::unique_ptr<uint8_t[]> buffer(static_cast<uint8_t*>(malloc(bufferSize)));
155 stream->ReadAll(buffer.get(), bufferSize, &read, nullptr);
156 logData = [[NSMutableData alloc] initWithBytesNoCopy:buffer.release()
157 length:read];
158 return logData;
159 }
160
161 #pragma mark - Private
162
163 - (rtc::LoggingSeverity)rtcSeverity {
164 switch (_severity) {
165 case RTCFileLoggerSeverityVerbose:
166 return rtc::LS_VERBOSE;
167 case RTCFileLoggerSeverityInfo:
168 return rtc::LS_INFO;
169 case RTCFileLoggerSeverityWarning:
170 return rtc::LS_WARNING;
171 case RTCFileLoggerSeverityError:
172 return rtc::LS_ERROR;
173 }
174 }
175
176 @end
OLDNEW
« no previous file with comments | « webrtc/sdk/objc/Framework/Classes/RTCFieldTrials.mm ('k') | webrtc/sdk/objc/Framework/Classes/RTCI420Shader.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698