Index: talk/app/webrtc/objc/RTCFileLogger.mm |
diff --git a/talk/app/webrtc/objc/RTCFileLogger.mm b/talk/app/webrtc/objc/RTCFileLogger.mm |
new file mode 100644 |
index 0000000000000000000000000000000000000000..05e77da72ef6f16bbf7147ac8165515b616afb37 |
--- /dev/null |
+++ b/talk/app/webrtc/objc/RTCFileLogger.mm |
@@ -0,0 +1,132 @@ |
+/* |
+ * libjingle |
+ * Copyright 2015 Google Inc. |
+ * |
+ * Redistribution and use in source and binary forms, with or without |
+ * modification, are permitted provided that the following conditions are met: |
+ * |
+ * 1. Redistributions of source code must retain the above copyright notice, |
+ * this list of conditions and the following disclaimer. |
+ * 2. Redistributions in binary form must reproduce the above copyright notice, |
+ * this list of conditions and the following disclaimer in the documentation |
+ * and/or other materials provided with the distribution. |
+ * 3. The name of the author may not be used to endorse or promote products |
+ * derived from this software without specific prior written permission. |
+ * |
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
+ * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
+ */ |
+ |
+#import "RTCFileLogger.h" |
+ |
+#include "webrtc/base/checks.h" |
+#include "webrtc/base/logging.h" |
+#include "webrtc/base/scoped_ptr.h" |
+#include "webrtc/base/stream.h" |
+ |
+NSString *const kDefaultLogFileName = @"webrtc.log"; |
+NSUInteger const kDefaultMaxFileSize = 10 * 1024 * 1024; // 10MB. |
+ |
+namespace rtc { |
+ |
+class StreamLogSink : public LogSink { |
+ public: |
+ // Creates a log sink that writes to the given stream. This log sink takes ownership of |stream|. |
+ StreamLogSink(StreamInterface *stream) { |
+ DCHECK(stream); |
+ _stream.reset(stream); |
+ } |
+ |
+ ~StreamLogSink() override {} |
+ |
+ void OnLogMessage(const std::string &message) override { |
+ if (_stream) { |
+ _stream->WriteAll(message.data(), message.size(), nullptr, nullptr); |
+ } |
+ } |
+ |
+ private: |
+ scoped_ptr<StreamInterface> _stream; |
+}; |
+ |
+} // namespace rtc |
+ |
+@implementation RTCFileLogger { |
+ NSString *_filePath; |
+ NSUInteger _maxFileSize; |
+ rtc::scoped_ptr<rtc::StreamLogSink> _logSink; |
+} |
+ |
+- (instancetype)init { |
+ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); |
+ NSString *documentsDirPath = [paths firstObject]; |
+ NSString *defaultFilePath = [documentsDirPath stringByAppendingPathComponent:kDefaultLogFileName]; |
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
|
+ return [self initWithFilePath:defaultFilePath maxFileSize:kDefaultMaxFileSize]; |
+} |
+ |
+- (instancetype)initWithFilePath:(NSString *)filePath maxFileSize:(NSUInteger)maxFileSize { |
+ NSParameterAssert(filePath.length); |
+ NSParameterAssert(maxFileSize); |
+ if (self = [super init]) { |
+ _filePath = filePath; |
+ _maxFileSize = maxFileSize; |
+ } |
+ return self; |
+} |
+ |
+- (void)dealloc { |
+ [self stop]; |
+} |
+ |
+- (void)start { |
+ if (_logSink) { |
+ return; |
+ } |
+ rtc::scoped_ptr<rtc::CircularFileStream> stream; |
+ stream.reset(new rtc::CircularFileStream(kDefaultMaxFileSize)); |
+ if (!stream->Open([_filePath UTF8String], "wb", nullptr)) { |
+ LOG(LS_ERROR) << "Failed to open log file at path: " << [_filePath UTF8String]; |
+ return; |
+ } |
+ _logSink.reset(new rtc::StreamLogSink(stream.release())); |
+ |
+ rtc::LogMessage::LogThreads(true); |
+ rtc::LogMessage::LogTimestamps(true); |
+ 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.
|
+} |
+ |
+- (void)stop { |
+ if (!_logSink) { |
+ return; |
+ } |
+ rtc::LogMessage::RemoveLogToStream(_logSink.get()); |
+ _logSink.reset(); |
+} |
+ |
+- (NSString *)log { |
+ return [[self class] contentsOfFileAtPath:_filePath]; |
+} |
+ |
+#pragma mark - Private |
+ |
++ (NSString *)contentsOfFileAtPath:(NSString *)path { |
+ NSError *error = nil; |
+ NSString *contents = |
+ [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error]; |
+ if (error) { |
+ LOG(LS_ERROR) << "Failed to read contents of file at path: " << [path UTF8String] |
+ << " Error: " << [error.localizedDescription UTF8String]; |
+ return nil; |
+ } |
+ return contents; |
+} |
+ |
+@end |