diff --git a/include/rtc/include.hpp b/include/rtc/include.hpp index 3ed9eff..5bba735 100644 --- a/include/rtc/include.hpp +++ b/include/rtc/include.hpp @@ -25,6 +25,8 @@ #endif #endif +#include "log.hpp" + #include #include #include @@ -33,9 +35,6 @@ #include #include -#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 consoleAppender; - if (!appender) - appender = &consoleAppender; - plog::init(severity, appender); - PLOG_DEBUG << "Logger initialized"; -} - -inline void InitLogger(LogLevel level) { InitLogger(static_cast(level)); } - -// Utils template struct overloaded : Ts... { using Ts::operator()...; }; template overloaded(Ts...)->overloaded; diff --git a/include/rtc/log.hpp b/include/rtc/log.hpp new file mode 100644 index 0000000..6c8f1e5 --- /dev/null +++ b/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 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(level)); } + +} + +#endif diff --git a/include/rtc/rtc.hpp b/include/rtc/rtc.hpp index 7bc4ba8..f58abfb 100644 --- a/include/rtc/rtc.hpp +++ b/include/rtc/rtc.hpp @@ -18,6 +18,7 @@ // C++ API #include "datachannel.hpp" +#include "log.hpp" #include "peerconnection.hpp" // C API diff --git a/test/main.cpp b/test/main.cpp index 5adb30c..6b1605b 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -33,7 +33,7 @@ using namespace std; template weak_ptr make_weak_ptr(shared_ptr ptr) { return ptr; } int main(int argc, char **argv) { - InitLogger(LogLevel::Warning); + InitLogger(LogLevel::Debug); #ifdef _WIN32 WSADATA wsaData; diff --git a/test/p2p/answerer.cpp b/test/p2p/answerer.cpp index 2861d73..b1c5e13 100644 --- a/test/p2p/answerer.cpp +++ b/test/p2p/answerer.cpp @@ -33,7 +33,7 @@ using namespace std; template weak_ptr make_weak_ptr(shared_ptr ptr) { return ptr; } int main(int argc, char **argv) { - InitLogger(LogLevel::Debug); + InitLogger(LogLevel::Warning); #ifdef _WIN32 WSADATA wsaData;