2015-07-14 12:55:44 -07:00
|
|
|
/*
|
2016-02-24 16:33:12 -08:00
|
|
|
* Copyright 2015 The WebRTC project authors. All Rights Reserved.
|
2015-07-14 12:55:44 -07:00
|
|
|
*
|
2016-02-24 16:33:12 -08:00
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
2015-07-14 12:55:44 -07:00
|
|
|
*/
|
|
|
|
|
|
2018-08-30 09:30:29 +02:00
|
|
|
#import "RTCFileLogger.h"
|
2015-07-14 12:55:44 -07:00
|
|
|
|
2016-05-01 14:53:46 -07:00
|
|
|
#include <memory>
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/checks.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "rtc_base/file_rotating_stream.h"
|
|
|
|
|
#include "rtc_base/log_sinks.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/logging.h"
|
2015-07-14 12:55:44 -07:00
|
|
|
|
2015-07-23 12:27:02 -07:00
|
|
|
NSString *const kDefaultLogDirName = @"webrtc_logs";
|
2025-01-08 05:45:56 -08:00
|
|
|
NSUInteger const kDefaultMaxFileSize = 10 * 1024 * 1024; // 10MB.
|
2015-12-08 13:59:05 -08:00
|
|
|
const char *kRTCFileLoggerRotatingLogPrefix = "rotating_log";
|
2015-07-14 12:55:44 -07:00
|
|
|
|
2020-05-04 16:14:32 +02:00
|
|
|
@implementation RTC_OBJC_TYPE (RTCFileLogger) {
|
2015-07-14 12:55:44 -07:00
|
|
|
BOOL _hasStarted;
|
2015-07-23 12:27:02 -07:00
|
|
|
NSString *_dirPath;
|
2015-07-14 12:55:44 -07:00
|
|
|
NSUInteger _maxFileSize;
|
2016-05-01 14:53:46 -07:00
|
|
|
std::unique_ptr<rtc::FileRotatingLogSink> _logSink;
|
2015-07-14 12:55:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@synthesize severity = _severity;
|
2015-12-08 13:59:05 -08:00
|
|
|
@synthesize rotationType = _rotationType;
|
2016-01-27 15:11:47 -08:00
|
|
|
@synthesize shouldDisableBuffering = _shouldDisableBuffering;
|
2015-07-14 12:55:44 -07:00
|
|
|
|
|
|
|
|
- (instancetype)init {
|
|
|
|
|
NSArray *paths = NSSearchPathForDirectoriesInDomains(
|
|
|
|
|
NSDocumentDirectory, NSUserDomainMask, YES);
|
|
|
|
|
NSString *documentsDirPath = [paths firstObject];
|
2015-07-23 12:27:02 -07:00
|
|
|
NSString *defaultDirPath =
|
|
|
|
|
[documentsDirPath stringByAppendingPathComponent:kDefaultLogDirName];
|
2025-01-08 05:45:56 -08:00
|
|
|
return [self initWithDirPath:defaultDirPath maxFileSize:kDefaultMaxFileSize];
|
2015-07-14 12:55:44 -07:00
|
|
|
}
|
|
|
|
|
|
2015-07-23 12:27:02 -07:00
|
|
|
- (instancetype)initWithDirPath:(NSString *)dirPath
|
|
|
|
|
maxFileSize:(NSUInteger)maxFileSize {
|
2015-12-08 13:59:05 -08:00
|
|
|
return [self initWithDirPath:dirPath
|
|
|
|
|
maxFileSize:maxFileSize
|
2016-03-31 12:08:03 -07:00
|
|
|
rotationType:RTCFileLoggerTypeCall];
|
2015-12-08 13:59:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (instancetype)initWithDirPath:(NSString *)dirPath
|
|
|
|
|
maxFileSize:(NSUInteger)maxFileSize
|
|
|
|
|
rotationType:(RTCFileLoggerRotationType)rotationType {
|
2015-07-23 12:27:02 -07:00
|
|
|
NSParameterAssert(dirPath.length);
|
2015-07-14 12:55:44 -07:00
|
|
|
NSParameterAssert(maxFileSize);
|
2024-08-23 13:51:10 -04:00
|
|
|
self = [super init];
|
|
|
|
|
if (self) {
|
2015-07-23 12:27:02 -07:00
|
|
|
BOOL isDir = NO;
|
|
|
|
|
NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
|
|
|
if ([fileManager fileExistsAtPath:dirPath isDirectory:&isDir]) {
|
|
|
|
|
if (!isDir) {
|
|
|
|
|
// Bail if something already exists there.
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (![fileManager createDirectoryAtPath:dirPath
|
|
|
|
|
withIntermediateDirectories:NO
|
|
|
|
|
attributes:nil
|
|
|
|
|
error:nil]) {
|
|
|
|
|
// Bail if we failed to create a directory.
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_dirPath = dirPath;
|
2015-07-14 12:55:44 -07:00
|
|
|
_maxFileSize = maxFileSize;
|
2016-03-31 12:08:03 -07:00
|
|
|
_severity = RTCFileLoggerSeverityInfo;
|
2015-07-14 12:55:44 -07:00
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)dealloc {
|
|
|
|
|
[self stop];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)start {
|
|
|
|
|
if (_hasStarted) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-12-08 13:59:05 -08:00
|
|
|
switch (_rotationType) {
|
2016-03-31 12:08:03 -07:00
|
|
|
case RTCFileLoggerTypeApp:
|
2015-12-08 13:59:05 -08:00
|
|
|
_logSink.reset(
|
|
|
|
|
new rtc::FileRotatingLogSink(_dirPath.UTF8String,
|
|
|
|
|
kRTCFileLoggerRotatingLogPrefix,
|
|
|
|
|
_maxFileSize,
|
|
|
|
|
_maxFileSize / 10));
|
|
|
|
|
break;
|
2016-03-31 12:08:03 -07:00
|
|
|
case RTCFileLoggerTypeCall:
|
2025-01-08 05:45:56 -08:00
|
|
|
_logSink.reset(new rtc::CallSessionFileRotatingLogSink(
|
|
|
|
|
_dirPath.UTF8String, _maxFileSize));
|
2015-12-08 13:59:05 -08:00
|
|
|
break;
|
|
|
|
|
}
|
2015-07-23 12:27:02 -07:00
|
|
|
if (!_logSink->Init()) {
|
2025-01-08 05:45:56 -08:00
|
|
|
RTC_LOG(LS_ERROR) << "Failed to open log files at path: "
|
|
|
|
|
<< _dirPath.UTF8String;
|
2015-07-14 12:55:44 -07:00
|
|
|
_logSink.reset();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-01-27 15:11:47 -08:00
|
|
|
if (_shouldDisableBuffering) {
|
|
|
|
|
_logSink->DisableBuffering();
|
|
|
|
|
}
|
2015-07-14 12:55:44 -07:00
|
|
|
rtc::LogMessage::LogThreads(true);
|
|
|
|
|
rtc::LogMessage::LogTimestamps(true);
|
|
|
|
|
rtc::LogMessage::AddLogToStream(_logSink.get(), [self rtcSeverity]);
|
|
|
|
|
_hasStarted = YES;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)stop {
|
|
|
|
|
if (!_hasStarted) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-09-17 00:24:34 -07:00
|
|
|
RTC_DCHECK(_logSink);
|
2015-07-14 12:55:44 -07:00
|
|
|
rtc::LogMessage::RemoveLogToStream(_logSink.get());
|
|
|
|
|
_hasStarted = NO;
|
2015-07-23 12:27:02 -07:00
|
|
|
_logSink.reset();
|
2015-07-14 12:55:44 -07:00
|
|
|
}
|
|
|
|
|
|
2018-01-12 16:16:18 +01:00
|
|
|
- (nullable NSData *)logData {
|
2015-07-14 12:55:44 -07:00
|
|
|
if (_hasStarted) {
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
2025-01-08 05:45:56 -08:00
|
|
|
NSMutableData *logData = [NSMutableData data];
|
2019-01-03 14:21:38 +01:00
|
|
|
std::unique_ptr<rtc::FileRotatingStreamReader> stream;
|
2025-01-08 05:45:56 -08:00
|
|
|
switch (_rotationType) {
|
2016-03-31 12:08:03 -07:00
|
|
|
case RTCFileLoggerTypeApp:
|
2025-01-08 05:45:56 -08:00
|
|
|
stream = std::make_unique<rtc::FileRotatingStreamReader>(
|
|
|
|
|
_dirPath.UTF8String, kRTCFileLoggerRotatingLogPrefix);
|
2015-12-08 13:59:05 -08:00
|
|
|
break;
|
2016-03-31 12:08:03 -07:00
|
|
|
case RTCFileLoggerTypeCall:
|
2025-01-08 05:45:56 -08:00
|
|
|
stream = std::make_unique<rtc::CallSessionFileRotatingStreamReader>(
|
|
|
|
|
_dirPath.UTF8String);
|
2015-12-08 13:59:05 -08:00
|
|
|
break;
|
|
|
|
|
}
|
2019-01-03 14:21:38 +01:00
|
|
|
size_t bufferSize = stream->GetSize();
|
|
|
|
|
if (bufferSize == 0) {
|
2015-07-23 12:27:02 -07:00
|
|
|
return logData;
|
2015-07-14 12:55:44 -07:00
|
|
|
}
|
|
|
|
|
// Allocate memory using malloc so we can pass it direcly to NSData without
|
|
|
|
|
// copying.
|
2025-01-08 05:45:56 -08:00
|
|
|
std::unique_ptr<uint8_t[]> buffer(static_cast<uint8_t *>(malloc(bufferSize)));
|
2019-01-03 14:21:38 +01:00
|
|
|
size_t read = stream->ReadAll(buffer.get(), bufferSize);
|
2015-07-23 12:27:02 -07:00
|
|
|
logData = [[NSMutableData alloc] initWithBytesNoCopy:buffer.release()
|
|
|
|
|
length:read];
|
|
|
|
|
return logData;
|
2015-07-14 12:55:44 -07:00
|
|
|
}
|
|
|
|
|
|
2015-07-23 12:27:02 -07:00
|
|
|
#pragma mark - Private
|
|
|
|
|
|
2015-07-14 12:55:44 -07:00
|
|
|
- (rtc::LoggingSeverity)rtcSeverity {
|
|
|
|
|
switch (_severity) {
|
2016-03-31 12:08:03 -07:00
|
|
|
case RTCFileLoggerSeverityVerbose:
|
2015-07-14 12:55:44 -07:00
|
|
|
return rtc::LS_VERBOSE;
|
2016-03-31 12:08:03 -07:00
|
|
|
case RTCFileLoggerSeverityInfo:
|
2015-07-14 12:55:44 -07:00
|
|
|
return rtc::LS_INFO;
|
2016-03-31 12:08:03 -07:00
|
|
|
case RTCFileLoggerSeverityWarning:
|
2015-07-14 12:55:44 -07:00
|
|
|
return rtc::LS_WARNING;
|
2016-03-31 12:08:03 -07:00
|
|
|
case RTCFileLoggerSeverityError:
|
2015-07-14 12:55:44 -07:00
|
|
|
return rtc::LS_ERROR;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|