Browse Source

Merge pull request #24 from paullouisageneau/integrate-plog

Integrate plog
Paul-Louis Ageneau 5 years ago
parent
commit
aeb777aa49
10 changed files with 92 additions and 26 deletions
  1. 3 0
      .gitmodules
  2. 2 0
      CMakeLists.txt
  3. 1 0
      deps/plog
  4. 24 0
      include/rtc/include.hpp
  5. 16 4
      include/rtc/rtc.h
  6. 4 4
      src/dtlstransport.cpp
  7. 26 8
      src/icetransport.cpp
  8. 7 3
      src/rtc.cpp
  9. 5 5
      src/sctptransport.cpp
  10. 4 2
      test/main.cpp

+ 3 - 0
.gitmodules

@@ -1,3 +1,6 @@
 [submodule "usrsctp"]
 [submodule "usrsctp"]
 	path = deps/usrsctp
 	path = deps/usrsctp
 	url = https://github.com/sctplab/usrsctp.git
 	url = https://github.com/sctplab/usrsctp.git
+[submodule "deps/plog"]
+	path = deps/plog
+	url = https://github.com/SergiusTheBest/plog

+ 2 - 0
CMakeLists.txt

@@ -62,6 +62,7 @@ set_target_properties(datachannel PROPERTIES
 target_include_directories(datachannel PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
 target_include_directories(datachannel PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
 target_include_directories(datachannel PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include/rtc)
 target_include_directories(datachannel PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include/rtc)
 target_include_directories(datachannel PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
 target_include_directories(datachannel PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
+target_include_directories(datachannel PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/deps/plog/include)
 target_link_libraries(datachannel usrsctp-static LibNice::LibNice)
 target_link_libraries(datachannel usrsctp-static LibNice::LibNice)
 
 
 add_library(datachannel-static STATIC EXCLUDE_FROM_ALL ${LIBDATACHANNEL_SOURCES})
 add_library(datachannel-static STATIC EXCLUDE_FROM_ALL ${LIBDATACHANNEL_SOURCES})
@@ -72,6 +73,7 @@ set_target_properties(datachannel-static PROPERTIES
 target_include_directories(datachannel-static PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
 target_include_directories(datachannel-static PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
 target_include_directories(datachannel-static PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include/rtc)
 target_include_directories(datachannel-static PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include/rtc)
 target_include_directories(datachannel-static PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
 target_include_directories(datachannel-static PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
+target_include_directories(datachannel-static PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/deps/plog/include)
 target_link_libraries(datachannel-static usrsctp-static LibNice::LibNice)
 target_link_libraries(datachannel-static usrsctp-static LibNice::LibNice)
 
 
 if (USE_GNUTLS)
 if (USE_GNUTLS)

+ 1 - 0
deps/plog

@@ -0,0 +1 @@
+Subproject commit 293164468998b2ff8d4d13684c065f736ec68fb4

+ 24 - 0
include/rtc/include.hpp

@@ -27,6 +27,9 @@
 #include <string>
 #include <string>
 #include <vector>
 #include <vector>
 
 
+#include <plog/Appenders/ColorConsoleAppender.h>
+#include <plog/Log.h>
+
 namespace rtc {
 namespace rtc {
 
 
 using std::byte;
 using std::byte;
@@ -48,6 +51,27 @@ 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 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
 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,
+	Fatal = 1,
+	Error = 2,
+	Warning = 3,
+	Info = 4,
+	Debug = 5,
+	Verbose = 6
+};
+
+inline void InitLogger(LogLevel level) { InitLogger(static_cast<plog::Severity>(level)); }
+
 template <class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
 template <class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
 template <class... Ts> overloaded(Ts...)->overloaded<Ts...>;
 template <class... Ts> overloaded(Ts...)->overloaded<Ts...>;
 
 

+ 16 - 4
include/rtc/rtc.h

@@ -23,7 +23,7 @@
 extern "C" {
 extern "C" {
 #endif
 #endif
 
 
-// libdatachannel rtc C API
+// libdatachannel C API
 
 
 typedef enum {
 typedef enum {
 	RTC_NEW = 0,
 	RTC_NEW = 0,
@@ -41,6 +41,19 @@ typedef enum {
 	RTC_GATHERING_COMPLETE = 2
 	RTC_GATHERING_COMPLETE = 2
 } rtc_gathering_state_t;
 } rtc_gathering_state_t;
 
 
+// Don't change, it must match plog severity
+typedef enum {
+	RTC_LOG_NONE = 0,
+	RTC_LOG_FATAL = 1,
+	RTC_LOG_ERROR = 2,
+	RTC_LOG_WARNING = 3,
+	RTC_LOG_INFO = 4,
+	RTC_LOG_DEBUG = 5,
+	RTC_LOG_VERBOSE = 6
+} rtc_log_level_t;
+
+void rtcInitLogger(rtc_log_level_t level);
+
 int rtcCreatePeerConnection(const char **iceServers, int iceServersCount);
 int rtcCreatePeerConnection(const char **iceServers, int iceServersCount);
 void rtcDeletePeerConnection(int pc);
 void rtcDeletePeerConnection(int pc);
 int rtcCreateDataChannel(int pc, const char *label);
 int rtcCreateDataChannel(int pc, const char *label);
@@ -52,8 +65,8 @@ void rtcSetLocalCandidateCallback(int pc,
                                   void (*candidateCallback)(const char *, const char *, void *));
                                   void (*candidateCallback)(const char *, const char *, void *));
 void rtcSetStateChangeCallback(int pc, void (*stateCallback)(rtc_state_t state, void *));
 void rtcSetStateChangeCallback(int pc, void (*stateCallback)(rtc_state_t state, void *));
 void rtcSetGatheringStateChangeCallback(int pc,
 void rtcSetGatheringStateChangeCallback(int pc,
-                                         void (*gatheringStateCallback)(rtc_gathering_state_t state,
-                                                                        void *));
+                                        void (*gatheringStateCallback)(rtc_gathering_state_t state,
+                                                                       void *));
 void rtcSetRemoteDescription(int pc, const char *sdp, const char *type);
 void rtcSetRemoteDescription(int pc, const char *sdp, const char *type);
 void rtcAddRemoteCandidate(int pc, const char *candidate, const char *mid);
 void rtcAddRemoteCandidate(int pc, const char *candidate, const char *mid);
 int rtcGetDataChannelLabel(int dc, char *data, int size);
 int rtcGetDataChannelLabel(int dc, char *data, int size);
@@ -68,4 +81,3 @@ void rtcSetUserPointer(int i, void *ptr);
 #endif
 #endif
 
 
 #endif
 #endif
-

+ 4 - 4
src/dtlstransport.cpp

@@ -146,7 +146,7 @@ void DtlsTransport::runRecvLoop() {
 		gnutls_dtls_set_mtu(mSession, maxMtu + 1);
 		gnutls_dtls_set_mtu(mSession, maxMtu + 1);
 
 
 	} catch (const std::exception &e) {
 	} catch (const std::exception &e) {
-		std::cerr << "DTLS handshake: " << e.what() << std::endl;
+		PLOG_ERROR << "DTLS handshake: " << e.what();
 		changeState(State::Failed);
 		changeState(State::Failed);
 		return;
 		return;
 	}
 	}
@@ -179,7 +179,7 @@ void DtlsTransport::runRecvLoop() {
 		}
 		}
 
 
 	} catch (const std::exception &e) {
 	} catch (const std::exception &e) {
-		std::cerr << "DTLS recv: " << e.what() << std::endl;
+		PLOG_ERROR << "DTLS recv: " << e.what();
 	}
 	}
 
 
 	changeState(State::Disconnected);
 	changeState(State::Disconnected);
@@ -449,7 +449,7 @@ void DtlsTransport::runRecvLoop() {
 				recv(decrypted);
 				recv(decrypted);
 		}
 		}
 	} catch (const std::exception &e) {
 	} catch (const std::exception &e) {
-		std::cerr << "DTLS recv: " << e.what() << std::endl;
+		PLOG_ERROR << "DTLS recv: " << e.what();
 	}
 	}
 
 
 	if (mState == State::Connected) {
 	if (mState == State::Connected) {
@@ -478,7 +478,7 @@ void DtlsTransport::InfoCallback(const SSL *ssl, int where, int ret) {
 
 
 	if (where & SSL_CB_ALERT) {
 	if (where & SSL_CB_ALERT) {
 		if (ret != 256) // Close Notify
 		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
 		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)),
       mStateChangeCallback(std::move(stateChangeCallback)),
       mGatheringStateChangeCallback(std::move(gatheringStateChangeCallback)) {
       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);
 	mMainLoop = decltype(mMainLoop)(g_main_loop_new(nullptr, FALSE), g_main_loop_unref);
 	if (!mMainLoop)
 	if (!mMainLoop)
@@ -308,7 +310,7 @@ void IceTransport::CandidateCallback(NiceAgent *agent, NiceCandidate *candidate,
 	try {
 	try {
 		iceTransport->processCandidate(cand);
 		iceTransport->processCandidate(cand);
 	} catch (const std::exception &e) {
 	} catch (const std::exception &e) {
-		std::cerr << "ICE candidate: " << e.what() << std::endl;
+		PLOG_WARNING << e.what();
 	}
 	}
 	g_free(cand);
 	g_free(cand);
 }
 }
@@ -318,7 +320,7 @@ void IceTransport::GatheringDoneCallback(NiceAgent *agent, guint streamId, gpoin
 	try {
 	try {
 		iceTransport->processGatheringDone();
 		iceTransport->processGatheringDone();
 	} catch (const std::exception &e) {
 	} 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 {
 	try {
 		iceTransport->processStateChange(state);
 		iceTransport->processStateChange(state);
 	} catch (const std::exception &e) {
 	} 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 {
 	try {
 		iceTransport->incoming(reinterpret_cast<byte *>(buf), len);
 		iceTransport->incoming(reinterpret_cast<byte *>(buf), len);
 	} catch (const std::exception &e) {
 	} 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,
 void IceTransport::LogCallback(const gchar *logDomain, GLogLevelFlags logLevel,
                                const gchar *message, gpointer userData) {
                                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
 } // namespace rtc

+ 7 - 3
src/rtc.cpp

@@ -17,12 +17,15 @@
  */
  */
 
 
 #include "datachannel.hpp"
 #include "datachannel.hpp"
+#include "include.hpp"
 #include "peerconnection.hpp"
 #include "peerconnection.hpp"
 
 
 #include <rtc.h>
 #include <rtc.h>
 
 
 #include <unordered_map>
 #include <unordered_map>
 
 
+#include <plog/Appenders/ColorConsoleAppender.h>
+
 using namespace rtc;
 using namespace rtc;
 using std::shared_ptr;
 using std::shared_ptr;
 using std::string;
 using std::string;
@@ -41,6 +44,8 @@ void *getUserPointer(int id) {
 
 
 } // namespace
 } // namespace
 
 
+void rtcInitLogger(rtc_log_level_t level) { InitLogger(static_cast<LogLevel>(level)); }
+
 int rtcCreatePeerConnection(const char **iceServers, int iceServersCount) {
 int rtcCreatePeerConnection(const char **iceServers, int iceServersCount) {
 	Configuration config;
 	Configuration config;
 	for (int i = 0; i < iceServersCount; ++i) {
 	for (int i = 0; i < iceServersCount; ++i) {
@@ -112,8 +117,8 @@ void rtcSetStateChangeCallback(int pc, void (*stateCallback)(rtc_state_t state,
 }
 }
 
 
 void rtcSetGatheringStateChangeCallback(int pc,
 void rtcSetGatheringStateChangeCallback(int pc,
-                                         void (*gatheringStateCallback)(rtc_gathering_state_t state,
-                                                                        void *)) {
+                                        void (*gatheringStateCallback)(rtc_gathering_state_t state,
+                                                                       void *)) {
 	auto it = peerConnectionMap.find(pc);
 	auto it = peerConnectionMap.find(pc);
 	if (it == peerConnectionMap.end())
 	if (it == peerConnectionMap.end())
 		return;
 		return;
@@ -209,4 +214,3 @@ void rtcSetUserPointer(int i, void *ptr) {
 	else
 	else
 		userPointerMap.erase(i);
 		userPointerMap.erase(i);
 }
 }
-

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

+ 4 - 2
test/main.cpp

@@ -29,12 +29,14 @@ using namespace std;
 template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
 template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
 
 
 int main(int argc, char **argv) {
 int main(int argc, char **argv) {
-	rtc::Configuration config;
+	InitLogger(LogLevel::Debug);
+	Configuration config;
+
 	// config.iceServers.emplace_back("stun.l.google.com:19302");
 	// config.iceServers.emplace_back("stun.l.google.com:19302");
 	// config.enableIceTcp = true;
 	// config.enableIceTcp = true;
 
 
 	// Add TURN Server Example
 	// Add TURN Server Example
-	// IceServer turnServer("TURN_SERVER_URL", "PORT_NO", "USERNAME", "PASSWORD", 
+	// IceServer turnServer("TURN_SERVER_URL", "PORT_NO", "USERNAME", "PASSWORD",
 	//							IceServer::RelayType::TurnTls);
 	//							IceServer::RelayType::TurnTls);
 	// config.iceServers.push_back(turnServer);
 	// config.iceServers.push_back(turnServer);