log.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /**
  2. * Copyright (c) 2019-2020 Paul-Louis Ageneau
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "log.hpp"
  19. #include "plog/Appenders/ColorConsoleAppender.h"
  20. #include "plog/Formatters/TxtFormatter.h"
  21. #include "plog/Init.h"
  22. #include "plog/Log.h"
  23. #include "plog/Logger.h"
  24. #include <mutex>
  25. namespace rtc {
  26. void InitLogger(LogLevel level) { InitLogger(static_cast<plog::Severity>(level)); }
  27. void InitLogger(plog::Severity severity, plog::IAppender *appender) {
  28. static plog::ColorConsoleAppender<plog::TxtFormatter> consoleAppender;
  29. static plog::Logger<0> *logger = nullptr;
  30. static std::mutex mutex;
  31. std::lock_guard lock(mutex);
  32. if (!logger) {
  33. logger = &plog::init(severity, appender ? appender : &consoleAppender);
  34. PLOG_DEBUG << "Logger initialized";
  35. } else {
  36. logger->setMaxSeverity(severity);
  37. if (appender)
  38. logger->addAppender(appender);
  39. }
  40. }
  41. } // namespace rtc