Jelajahi Sumber

Replaced console output with logging

Paul-Louis Ageneau 5 tahun lalu
induk
melakukan
7a552bb0fa
5 mengubah file dengan 45 tambahan dan 25 penghapusan
  1. 1 1
      CMakeLists.txt
  2. 9 7
      include/rtc/include.hpp
  3. 4 4
      src/dtlstransport.cpp
  4. 26 8
      src/icetransport.cpp
  5. 5 5
      src/sctptransport.cpp

+ 1 - 1
CMakeLists.txt

@@ -7,7 +7,7 @@ project (libdatachannel
 set(CMAKE_POSITION_INDEPENDENT_CODE ON)
 set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Modules)
 
-set(LIBDATACHANNEL_SOURCES	
+set(LIBDATACHANNEL_SOURCES
 	${CMAKE_CURRENT_SOURCE_DIR}/src/candidate.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/certificate.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/channel.cpp

+ 9 - 7
include/rtc/include.hpp

@@ -51,6 +51,14 @@ const uint16_t DEFAULT_SCTP_PORT = 5000; // SCTP port to use by default
 const size_t DEFAULT_MAX_MESSAGE_SIZE = 65536;    // Remote max message size if not specified in SDP
 const size_t LOCAL_MAX_MESSAGE_SIZE = 256 * 1024; // Local max message size
 
+inline void InitLogger(plog::Severity severity, plog::IAppender *appender = nullptr) {
+	static plog::ColorConsoleAppender<plog::TxtFormatter> consoleAppender;
+	if (!appender)
+		appender = &consoleAppender;
+	plog::init(severity, appender);
+	PLOG_DEBUG << "Logger initialized";
+}
+
 // Don't change, it must match plog severity
 enum class LogLevel {
 	None = 0,
@@ -62,13 +70,7 @@ enum class LogLevel {
 	Verbose = 6
 };
 
-inline void InitLogger(LogLevel level, plog::IAppender *appender = nullptr) {
-	static plog::ColorConsoleAppender<plog::TxtFormatter> consoleAppender;
-	if (!appender)
-		appender = &consoleAppender;
-	plog::init(static_cast<plog::Severity>(level), appender);
-	LOGD << "Logger initialized";
-}
+inline void InitLogger(LogLevel level) { InitLogger(static_cast<plog::Severity>(level)); }
 
 template <class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
 template <class... Ts> overloaded(Ts...)->overloaded<Ts...>;

+ 4 - 4
src/dtlstransport.cpp

@@ -146,7 +146,7 @@ void DtlsTransport::runRecvLoop() {
 		gnutls_dtls_set_mtu(mSession, maxMtu + 1);
 
 	} catch (const std::exception &e) {
-		std::cerr << "DTLS handshake: " << e.what() << std::endl;
+		PLOG_ERROR << "DTLS handshake: " << e.what();
 		changeState(State::Failed);
 		return;
 	}
@@ -179,7 +179,7 @@ void DtlsTransport::runRecvLoop() {
 		}
 
 	} catch (const std::exception &e) {
-		std::cerr << "DTLS recv: " << e.what() << std::endl;
+		PLOG_ERROR << "DTLS recv: " << e.what();
 	}
 
 	changeState(State::Disconnected);
@@ -449,7 +449,7 @@ void DtlsTransport::runRecvLoop() {
 				recv(decrypted);
 		}
 	} catch (const std::exception &e) {
-		std::cerr << "DTLS recv: " << e.what() << std::endl;
+		PLOG_ERROR << "DTLS recv: " << e.what();
 	}
 
 	if (mState == State::Connected) {
@@ -478,7 +478,7 @@ void DtlsTransport::InfoCallback(const SSL *ssl, int where, int ret) {
 
 	if (where & SSL_CB_ALERT) {
 		if (ret != 256) // Close Notify
-			std::cerr << "DTLS alert: " << SSL_alert_desc_string_long(ret) << std::endl;
+			PLOG_ERROR << "DTLS alert: " << SSL_alert_desc_string_long(ret);
 		t->mIncomingQueue.stop(); // Close the connection
 	}
 }

+ 26 - 8
src/icetransport.cpp

@@ -42,9 +42,11 @@ IceTransport::IceTransport(const Configuration &config, Description::Role role,
       mStateChangeCallback(std::move(stateChangeCallback)),
       mGatheringStateChangeCallback(std::move(gatheringStateChangeCallback)) {
 
-	auto logLevelFlags = GLogLevelFlags(G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION);
-	g_log_set_handler(nullptr, logLevelFlags, LogCallback, this);
-	nice_debug_enable(false);
+	g_log_set_handler("libnice", G_LOG_LEVEL_MASK, LogCallback, this);
+
+	IF_PLOG(plog::verbose) {
+		nice_debug_enable(false); // do not output STUN debug messages
+	}
 
 	mMainLoop = decltype(mMainLoop)(g_main_loop_new(nullptr, FALSE), g_main_loop_unref);
 	if (!mMainLoop)
@@ -308,7 +310,7 @@ void IceTransport::CandidateCallback(NiceAgent *agent, NiceCandidate *candidate,
 	try {
 		iceTransport->processCandidate(cand);
 	} catch (const std::exception &e) {
-		std::cerr << "ICE candidate: " << e.what() << std::endl;
+		PLOG_WARNING << e.what();
 	}
 	g_free(cand);
 }
@@ -318,7 +320,7 @@ void IceTransport::GatheringDoneCallback(NiceAgent *agent, guint streamId, gpoin
 	try {
 		iceTransport->processGatheringDone();
 	} catch (const std::exception &e) {
-		std::cerr << "ICE gathering done: " << e.what() << std::endl;
+		PLOG_WARNING << e.what();
 	}
 }
 
@@ -328,7 +330,7 @@ void IceTransport::StateChangeCallback(NiceAgent *agent, guint streamId, guint c
 	try {
 		iceTransport->processStateChange(state);
 	} catch (const std::exception &e) {
-		std::cerr << "ICE change state: " << e.what() << std::endl;
+		PLOG_WARNING << e.what();
 	}
 }
 
@@ -338,13 +340,29 @@ void IceTransport::RecvCallback(NiceAgent *agent, guint streamId, guint componen
 	try {
 		iceTransport->incoming(reinterpret_cast<byte *>(buf), len);
 	} catch (const std::exception &e) {
-		std::cerr << "ICE incoming: " << e.what() << std::endl;
+		PLOG_WARNING << e.what();
 	}
 }
 
 void IceTransport::LogCallback(const gchar *logDomain, GLogLevelFlags logLevel,
                                const gchar *message, gpointer userData) {
-	std::cout << message << std::endl;
+
+	plog::Severity severity;
+	unsigned int flags = logLevel & G_LOG_LEVEL_MASK;
+	if (flags & G_LOG_LEVEL_ERROR)
+		severity = plog::fatal;
+	else if (flags & G_LOG_LEVEL_CRITICAL)
+		severity = plog::error;
+	else if (flags & G_LOG_LEVEL_WARNING)
+		severity = plog::warning;
+	else if (flags & G_LOG_LEVEL_MESSAGE)
+		severity = plog::info;
+	else if (flags & G_LOG_LEVEL_INFO)
+		severity = plog::info;
+	else
+		severity = plog::verbose; // libnice debug as verbose
+
+	PLOG(severity) << message;
 }
 
 } // namespace rtc

+ 5 - 5
src/sctptransport.cpp

@@ -354,7 +354,7 @@ int SctpTransport::handleRecv(struct socket *sock, union sctp_sockstore addr, co
 			mPartialRecv.insert(mPartialRecv.end(), data, data + len);
 		}
 	} catch (const std::exception &e) {
-		std::cerr << "SCTP recv: " << e.what() << std::endl;
+		PLOG_ERROR << "SCTP recv: " << e.what();
 		return -1;
 	}
 	return 0; // success
@@ -365,7 +365,7 @@ int SctpTransport::handleSend(size_t free) {
 		std::lock_guard lock(mSendMutex);
 		trySendQueue();
 	} catch (const std::exception &e) {
-		std::cerr << "SCTP send: " << e.what() << std::endl;
+		PLOG_ERROR << "SCTP send: " << e.what();
 		return -1;
 	}
 	return 0; // success
@@ -379,7 +379,7 @@ int SctpTransport::handleWrite(byte *data, size_t len, uint8_t tos, uint8_t set_
 		mConnectDataSent = true;
 		mConnectCondition.notify_all();
 	} catch (const std::exception &e) {
-		std::cerr << "SCTP write: " << e.what() << std::endl;
+		PLOG_ERROR << "SCTP write: " << e.what();
 		return -1;
 	}
 	return 0; // success
@@ -440,7 +440,7 @@ void SctpTransport::processData(const byte *data, size_t len, uint16_t sid, Payl
 
 	default:
 		// Unknown
-		std::cerr << "Unknown PPID: " << uint32_t(ppid) << std::endl;
+		PLOG_WARNING << "Unknown PPID: " << uint32_t(ppid);
 		return;
 	}
 }
@@ -456,7 +456,7 @@ void SctpTransport::processNotification(const union sctp_notification *notify, s
 			changeState(State::Connected);
 		} else {
 			if (mState == State::Connecting) {
-				std::cerr << "SCTP connection failed" << std::endl;
+				PLOG_ERROR << "SCTP connection failed";
 				changeState(State::Failed);
 			} else {
 				changeState(State::Disconnected);