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/remote_bitrate_estimator:remote_bitrate_estimator",
"../modules/rtp_rtcp:rtp_rtcp_format", "../modules/rtp_rtcp:rtp_rtcp_format",
"../rtc_base:rtc_base_approved", "../rtc_base:rtc_base_approved",
"../system_wrappers",
] ]
# TODO(eladalon): Remove this. # TODO(eladalon): Remove this.

View File

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

View File

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