Sfoglia il codice sorgente

Moved log to its own header and prevented multiple log init

Paul-Louis Ageneau 5 anni fa
parent
commit
3367eba4fe
5 ha cambiato i file con 60 aggiunte e 30 eliminazioni
  1. 2 28
      include/rtc/include.hpp
  2. 55 0
      include/rtc/log.hpp
  3. 1 0
      include/rtc/rtc.hpp
  4. 1 1
      test/main.cpp
  5. 1 1
      test/p2p/answerer.cpp

+ 2 - 28
include/rtc/include.hpp

@@ -25,6 +25,8 @@
 #endif
 #endif
 
+#include "log.hpp"
+
 #include <cstddef>
 #include <functional>
 #include <memory>
@@ -33,9 +35,6 @@
 #include <string>
 #include <vector>
 
-#include "plog/Appenders/ColorConsoleAppender.h"
-#include "plog/Log.h"
-
 namespace rtc {
 
 using std::byte;
@@ -50,8 +49,6 @@ using std::uint32_t;
 using std::uint64_t;
 using std::uint8_t;
 
-// Constants
-
 const size_t MAX_NUMERICNODE_LEN = 48; // Max IPv6 string representation length
 const size_t MAX_NUMERICSERV_LEN = 6;  // Max port string representation length
 
@@ -59,29 +56,6 @@ 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
 
-// Log
-
-enum class LogLevel { // Don't change, it must match plog severity
-	None = 0,
-	Fatal = 1,
-	Error = 2,
-	Warning = 3,
-	Info = 4,
-	Debug = 5,
-	Verbose = 6
-};
-
-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";
-}
-
-inline void InitLogger(LogLevel level) { InitLogger(static_cast<plog::Severity>(level)); }
-
-// Utils
 
 template <class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
 template <class... Ts> overloaded(Ts...)->overloaded<Ts...>;

+ 55 - 0
include/rtc/log.hpp

@@ -0,0 +1,55 @@
+/**
+ * Copyright (c) 2019 Paul-Louis Ageneau
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef RTC_LOG_H
+#define RTC_LOG_H
+
+#include "plog/Appenders/ColorConsoleAppender.h"
+#include "plog/Log.h"
+#include "plog/Logger.h"
+
+namespace rtc {
+
+enum class LogLevel { // Don't change, it must match plog severity
+	None = 0,
+	Fatal = 1,
+	Error = 2,
+	Warning = 3,
+	Info = 4,
+	Debug = 5,
+	Verbose = 6
+};
+
+inline void InitLogger(plog::Severity severity, plog::IAppender *appender = nullptr) {
+	static plog::ColorConsoleAppender<plog::TxtFormatter> consoleAppender;
+	static plog::Logger<0> *logger = nullptr;
+	if (!logger) {
+		logger = &plog::init(severity, appender ? appender : &consoleAppender);
+		PLOG_DEBUG << "Logger initialized";
+	} else {
+		logger->setMaxSeverity(severity);
+		if (appender)
+			logger->addAppender(appender);
+	}
+}
+
+inline void InitLogger(LogLevel level) { InitLogger(static_cast<plog::Severity>(level)); }
+
+}
+
+#endif

+ 1 - 0
include/rtc/rtc.hpp

@@ -18,6 +18,7 @@
 
 // C++ API
 #include "datachannel.hpp"
+#include "log.hpp"
 #include "peerconnection.hpp"
 
 // C API

+ 1 - 1
test/main.cpp

@@ -33,7 +33,7 @@ using namespace std;
 template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
 
 int main(int argc, char **argv) {
-	InitLogger(LogLevel::Warning);
+	InitLogger(LogLevel::Debug);
 
 #ifdef _WIN32
 	WSADATA wsaData;

+ 1 - 1
test/p2p/answerer.cpp

@@ -33,7 +33,7 @@ using namespace std;
 template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
 
 int main(int argc, char **argv) {
-	InitLogger(LogLevel::Debug);
+	InitLogger(LogLevel::Warning);
 
 #ifdef _WIN32
 	WSADATA wsaData;