Skip to content

Commit

Permalink
Add LogSink: Add FileLogSink to peerconnection (#56)
Browse files Browse the repository at this point in the history
* Add LogSink: Add FileLogSink to peerconnection

* Update FileLogSink to accept custom filepath

* Change Logger to const std::string&

* FileLogSink: don't close file if it's empty

* FileLogSink: Do not remove log if it's empty

* LogFileSink: Don't add log to stream if it's empty
  • Loading branch information
Xia Zhongyang authored Jan 14, 2021
1 parent c24ca8b commit fad8291
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 18 deletions.
2 changes: 2 additions & 0 deletions examples/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,8 @@ if (is_linux || is_win) {
testonly = true
sources = [
"peerconnection/serverless/main.cc",
"peerconnection/serverless/logger.cc",
"peerconnection/serverless/logger.h",
"peerconnection/serverless/conductor.cc",
"peerconnection/serverless/conductor.h",
"peerconnection/serverless/defaults.cc",
Expand Down
33 changes: 33 additions & 0 deletions examples/peerconnection/serverless/logger.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "logger.h"
#include <iostream>

FileLogSink::FileLogSink(const std::string& log_filepath)
: log_filepath_(log_filepath) {
log_file_ = fopen(log_filepath_.c_str(), "a");
if (log_file_ != NULL) {
rtc::LogMessage::AddLogToStream(this, rtc::LoggingSeverity::INFO);
}
}

FileLogSink::~FileLogSink() {
if (log_file_ != NULL) {
fclose(log_file_);
rtc::LogMessage::RemoveLogToStream(this);
}
}

void FileLogSink::OnLogMessage(const std::string& msg,
rtc::LoggingSeverity severity,
const char* tag) {
OnLogMessage(tag + (": " + msg), severity);
}

void FileLogSink::OnLogMessage(const std::string& msg,
rtc::LoggingSeverity /* severity */) {
OnLogMessage(msg);
}

void FileLogSink::OnLogMessage(const std::string& message) {
fwrite(message.c_str(), message.length(), 1, log_file_);
fflush(log_file_);
}
17 changes: 17 additions & 0 deletions examples/peerconnection/serverless/logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "rtc_base/logging.h"

class FileLogSink : public rtc::LogSink {
public:
FileLogSink(const std::string& log_filepath);
~FileLogSink();
void OnLogMessage(const std::string& message);
void OnLogMessage(const std::string& msg,
rtc::LoggingSeverity severity,
const char* tag);
void OnLogMessage(const std::string& msg,
rtc::LoggingSeverity /* severity */);

private:
std::string log_filepath_;
FILE* log_file_;
};
33 changes: 15 additions & 18 deletions examples/peerconnection/serverless/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@
*/

#include "conductor.h"
#include "peer_connection_client.h"
#include "defaults.h"
#include "logger.h"
#include "peer_connection_client.h"

#ifdef WIN32
#include "rtc_base/win32_socket_init.h"
#include "rtc_base/win32_socket_server.h"
#endif

#include "api/alphacc_config.h"
#include "rtc_base/ssl_adapter.h"
#include "rtc_base/string_utils.h" // For ToUtf8
#include "system_wrappers/include/field_trial.h"
#include "api/alphacc_config.h"

#include <chrono>
#include <functional>
Expand Down Expand Up @@ -58,12 +60,11 @@ class MainWindowMock : public MainWindow {
int close_time_;

public:
MainWindowMock(std::shared_ptr<rtc::AutoSocketServerThread> socket_thread) :
callback_(NULL),
socket_thread_(socket_thread),
config_(webrtc::GetAlphaCCConfig()),
close_time_(rtc::Thread::kForever)
{}
MainWindowMock(std::shared_ptr<rtc::AutoSocketServerThread> socket_thread)
: callback_(NULL),
socket_thread_(socket_thread),
config_(webrtc::GetAlphaCCConfig()),
close_time_(rtc::Thread::kForever) {}
void RegisterObserver(MainWndCallback* callback) override {
callback_ = callback;
}
Expand All @@ -89,9 +90,7 @@ class MainWindowMock : public MainWindow {
remote_renderer_.reset(new VideoRenderer(remote_video, callback_));
}

void StopRemoteRenderer() override {
remote_renderer_.reset();
}
void StopRemoteRenderer() override { remote_renderer_.reset(); }

void QueueUIThreadCallback(int msg_id, void* data) override {
callback_->UIThreadCallback(msg_id, data);
Expand Down Expand Up @@ -129,15 +128,14 @@ int main(int argc, char* argv[]) {
exit(EINVAL);
}

rtc::LogMessage::LogToDebug(rtc::LS_INFO);

auto config = webrtc::GetAlphaCCConfig();
std::unique_ptr<FileLogSink> sink;

if (config->save_log_to_file) {
// Temporary Fix
// TODO(zhongyang xia): Try to leverage rtc::LogMessage::AddLogToStream
// rtc::LogMessage::SetIfLogToFile(true);
// rtc::LogMessage::SetLogFileName(config->log_output_path);
sink = std::make_unique<FileLogSink>(config->log_output_path);
}
rtc::LogMessage::LogToDebug(rtc::LS_INFO);

webrtc::field_trial::InitFieldTrialsFromString(
"WebRTC-KeepAbsSendTimeExtension/Enabled/"); // Config for
Expand All @@ -163,8 +161,7 @@ int main(int argc, char* argv[]) {

if (config->is_receiver) {
client.StartListen(config->listening_ip, config->listening_port);
}
else if (config->is_sender) {
} else if (config->is_sender) {
client.StartConnect(config->dest_ip, config->dest_port);
}

Expand Down

0 comments on commit fad8291

Please sign in to comment.