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

Side by Side Diff: talk/app/webrtc/objc/RTCFileLogger.mm

Issue 1217473011: AppRTCDemo file logging. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc@master
Patch Set: Created 5 years, 5 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 * 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"
31 #include "webrtc/base/logging.h"
32 #include "webrtc/base/scoped_ptr.h"
33 #include "webrtc/base/stream.h"
34
35 NSString *const kDefaultLogFileName = @"webrtc.log";
36 NSUInteger const kDefaultMaxFileSize = 10 * 1024 * 1024; // 10MB.
37
38 namespace rtc {
39
40 class StreamLogSink : public LogSink {
41 public:
42 // Creates a log sink that writes to the given stream. This log sink takes own ership of |stream|.
43 StreamLogSink(StreamInterface *stream) {
44 DCHECK(stream);
45 _stream.reset(stream);
46 }
47
48 ~StreamLogSink() override {}
49
50 void OnLogMessage(const std::string &message) override {
51 if (_stream) {
52 _stream->WriteAll(message.data(), message.size(), nullptr, nullptr);
53 }
54 }
55
56 private:
57 scoped_ptr<StreamInterface> _stream;
58 };
59
60 } // namespace rtc
61
62 @implementation RTCFileLogger {
63 NSString *_filePath;
64 NSUInteger _maxFileSize;
65 rtc::scoped_ptr<rtc::StreamLogSink> _logSink;
66 }
67
68 - (instancetype)init {
69 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUs erDomainMask, YES);
70 NSString *documentsDirPath = [paths firstObject];
71 NSString *defaultFilePath = [documentsDirPath stringByAppendingPathComponent:k DefaultLogFileName];
jiayl2 2015/07/06 18:18:14 Is this file private to the application? We should
tkchin 2015/07/06 20:36:49 Yes, it's private to the app bundle. Applications
72 return [self initWithFilePath:defaultFilePath maxFileSize:kDefaultMaxFileSize] ;
73 }
74
75 - (instancetype)initWithFilePath:(NSString *)filePath maxFileSize:(NSUInteger)ma xFileSize {
76 NSParameterAssert(filePath.length);
77 NSParameterAssert(maxFileSize);
78 if (self = [super init]) {
79 _filePath = filePath;
80 _maxFileSize = maxFileSize;
81 }
82 return self;
83 }
84
85 - (void)dealloc {
86 [self stop];
87 }
88
89 - (void)start {
90 if (_logSink) {
91 return;
92 }
93 rtc::scoped_ptr<rtc::CircularFileStream> stream;
94 stream.reset(new rtc::CircularFileStream(kDefaultMaxFileSize));
95 if (!stream->Open([_filePath UTF8String], "wb", nullptr)) {
96 LOG(LS_ERROR) << "Failed to open log file at path: " << [_filePath UTF8Strin g];
97 return;
98 }
99 _logSink.reset(new rtc::StreamLogSink(stream.release()));
100
101 rtc::LogMessage::LogThreads(true);
102 rtc::LogMessage::LogTimestamps(true);
103 rtc::LogMessage::AddLogToStream(_logSink.get(), rtc::LS_INFO);
jiayl2 2015/07/06 18:18:14 FYI we'll need a switch to turn on verbose logging
tkchin 2015/07/06 20:36:50 What logs do we get in verbose?
jiayl2 2015/07/06 20:57:56 Verbose webrtc logs contain detailed information a
tkchin_webrtc 2015/07/07 23:30:10 Ok. Making that the default then if it contains us
jiayl2 2015/07/08 16:32:12 We should default to "info". Verbose is too noisy
tkchin_webrtc 2015/07/08 18:18:23 Done.
104 }
105
106 - (void)stop {
107 if (!_logSink) {
108 return;
109 }
110 rtc::LogMessage::RemoveLogToStream(_logSink.get());
111 _logSink.reset();
112 }
113
114 - (NSString *)log {
115 return [[self class] contentsOfFileAtPath:_filePath];
116 }
117
118 #pragma mark - Private
119
120 + (NSString *)contentsOfFileAtPath:(NSString *)path {
121 NSError *error = nil;
122 NSString *contents =
123 [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding erro r:&error];
124 if (error) {
125 LOG(LS_ERROR) << "Failed to read contents of file at path: " << [path UTF8St ring]
126 << " Error: " << [error.localizedDescription UTF8String];
127 return nil;
128 }
129 return contents;
130 }
131
132 @end
OLDNEW
« no previous file with comments | « no previous file | talk/app/webrtc/objc/public/RTCFileLogger.h » ('j') | talk/examples/objc/AppRTCDemo/common/ARDLogging.mm » ('J')

Powered by Google App Engine
This is Rietveld 408576698