Change RtcEventLogOutputFile to use FILE* for i/o.

Eliminates a dependency on system_wrappers and the FileWrapper class.

Bug: None
Change-Id: I2cbbf4d6c3bf50e9b3b0b6d140da6d5d7e54167e
Reviewed-on: https://webrtc-review.googlesource.com/29821
Commit-Queue: Niels Moller <nisse@webrtc.org>
Reviewed-by: Elad Alon <eladalon@webrtc.org>
Reviewed-by: Björn Terelius <terelius@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#21208}
This commit is contained in:
Niels Möller 2017-12-07 09:35:23 +01:00 committed by Commit Bot
parent aaa746672f
commit d4d399081b
3 changed files with 29 additions and 45 deletions

View File

@ -79,7 +79,6 @@ rtc_source_set("rtc_event_log_api") {
"../modules/remote_bitrate_estimator:remote_bitrate_estimator",
"../modules/rtp_rtcp:rtp_rtcp_format",
"../rtc_base:rtc_base_approved",
"../system_wrappers",
]
# TODO(eladalon): Remove this.

View File

@ -14,7 +14,6 @@
#include "logging/rtc_event_log/rtc_event_log.h"
#include "rtc_base/checks.h"
#include "rtc_base/logging.h"
#include "system_wrappers/include/file_wrapper.h"
namespace webrtc {
@ -30,48 +29,39 @@ RtcEventLogOutputFile::RtcEventLogOutputFile(const std::string& file_name)
RtcEventLogOutputFile::RtcEventLogOutputFile(const std::string& file_name,
size_t max_size_bytes)
: max_size_bytes_(max_size_bytes), file_(FileWrapper::Create()) {
RTC_CHECK_LE(max_size_bytes_, kMaxReasonableFileSize);
if (!file_->OpenFile(file_name.c_str(), false)) {
RTC_LOG(LS_ERROR) << "Can't open file. WebRTC event log not started.";
file_.reset();
return;
}
}
// Unlike plain fopen, CreatePlatformFile takes care of filename utf8 ->
// wchar conversion on windows.
: RtcEventLogOutputFile(rtc::CreatePlatformFile(file_name),
max_size_bytes) {}
RtcEventLogOutputFile::RtcEventLogOutputFile(rtc::PlatformFile file)
: RtcEventLogOutputFile(file, RtcEventLog::kUnlimitedOutput) {}
RtcEventLogOutputFile::RtcEventLogOutputFile(rtc::PlatformFile file,
RtcEventLogOutputFile::RtcEventLogOutputFile(rtc::PlatformFile platform_file,
size_t max_size_bytes)
: max_size_bytes_(max_size_bytes), file_(FileWrapper::Create()) {
: max_size_bytes_(max_size_bytes) {
RTC_CHECK_LE(max_size_bytes_, kMaxReasonableFileSize);
FILE* file_handle = rtc::FdopenPlatformFileForWriting(file);
if (!file_handle) {
// Handle errors from the CreatePlatformFile call in above constructor.
if (platform_file == rtc::kInvalidPlatformFileValue) {
RTC_LOG(LS_ERROR) << "Invalid file. WebRTC event log not started.";
return;
}
file_ = rtc::FdopenPlatformFileForWriting(platform_file);
if (!file_) {
RTC_LOG(LS_ERROR) << "Can't open file. WebRTC event log not started.";
// Even though we failed to open a FILE*, the file is still open
// and needs to be closed.
if (!rtc::ClosePlatformFile(file)) {
if (!rtc::ClosePlatformFile(platform_file)) {
RTC_LOG(LS_ERROR) << "Can't close file.";
}
file_.reset();
return;
}
if (!file_->OpenFromFileHandle(file_handle)) {
RTC_LOG(LS_ERROR) << "Can't open file. WebRTC event log not started.";
file_.reset();
return;
}
}
RtcEventLogOutputFile::~RtcEventLogOutputFile() {
if (file_) {
RTC_DCHECK(IsActiveInternal());
file_->CloseFile();
file_.reset();
fclose(file_);
}
}
@ -85,29 +75,27 @@ bool RtcEventLogOutputFile::Write(const std::string& output) {
// calculation of (written_bytes_ + output.length()).
RTC_DCHECK_LT(output.length(), kMaxReasonableFileSize);
bool written = false;
if (max_size_bytes_ == RtcEventLog::kUnlimitedOutput ||
written_bytes_ + output.length() <= max_size_bytes_) {
written = file_->Write(output.c_str(), output.size());
if (!written) {
RTC_LOG(LS_ERROR) << "FileWrapper failed to write WebRtcEventLog file.";
if (fwrite(output.c_str(), 1, output.size(), file_) == output.size()) {
written_bytes_ += output.size();
return true;
} else {
RTC_LOG(LS_ERROR) << "Write to WebRtcEventLog file failed.";
}
} else {
RTC_LOG(LS_VERBOSE) << "Max file size reached.";
}
if (written) {
written_bytes_ += output.size();
} else {
file_->CloseFile();
file_.reset();
}
return written;
// Failed, for one of above reasons. Close output file.
fclose(file_);
file_ = nullptr;
return false;
}
// Internal non-virtual method.
bool RtcEventLogOutputFile::IsActiveInternal() const {
return file_ && file_->is_open();
return file_ != nullptr;
}
} // namespace webrtc

View File

@ -12,6 +12,7 @@
#define LOGGING_RTC_EVENT_LOG_OUTPUT_RTC_EVENT_LOG_OUTPUT_FILE_H_
#include <stddef.h>
#include <stdio.h>
#include <memory>
#include <string>
@ -21,8 +22,6 @@
namespace webrtc {
class FileWrapper;
class RtcEventLogOutputFile final : public RtcEventLogOutput {
public:
static const size_t kMaxReasonableFileSize; // Explanation at declaration.
@ -47,12 +46,10 @@ class RtcEventLogOutputFile final : public RtcEventLogOutput {
// some other function of this class.
inline bool IsActiveInternal() const;
// TODO(eladalon): We're still discussing whether to use FileWrapper or not.
// If we end up keeping FileWrapper, we should use its own max-size logic,
// rather than duplicate it.
// Maximum size, or zero for no limit.
const size_t max_size_bytes_;
size_t written_bytes_{0};
std::unique_ptr<FileWrapper> file_;
FILE* file_{nullptr};
};
} // namespace webrtc