logging.cc 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "third_party/openscreen/src/platform/api/logging.h"
  5. #include <cstring>
  6. #include <sstream>
  7. #include "base/debug/debugger.h"
  8. #include "base/immediate_crash.h"
  9. #include "base/logging.h"
  10. namespace openscreen {
  11. namespace {
  12. ::logging::LogSeverity MapLogLevel(LogLevel level) {
  13. switch (level) {
  14. case LogLevel::kVerbose:
  15. return ::logging::LOG_VERBOSE;
  16. case LogLevel::kInfo:
  17. return ::logging::LOG_INFO;
  18. case LogLevel::kWarning:
  19. return ::logging::LOG_WARNING;
  20. case LogLevel::kError:
  21. return ::logging::LOG_ERROR;
  22. case LogLevel::kFatal:
  23. return ::logging::LOG_FATAL;
  24. }
  25. }
  26. } // namespace
  27. bool IsLoggingOn(LogLevel level, const char* file) {
  28. if (level == LogLevel::kVerbose) {
  29. return ::logging::GetVlogLevelHelper(file, strlen(file)) > 0;
  30. }
  31. return ::logging::ShouldCreateLogMessage(MapLogLevel(level));
  32. }
  33. void LogWithLevel(LogLevel level,
  34. const char* file,
  35. int line,
  36. std::stringstream message) {
  37. ::logging::LogMessage(file, line, MapLogLevel(level)).stream()
  38. << message.rdbuf();
  39. }
  40. void Break() {
  41. #if defined(OFFICIAL_BUILD) && defined(NDEBUG)
  42. IMMEDIATE_CRASH();
  43. #else
  44. // Chrome's base::debug::BreakDebugger is not properly annotated as
  45. // [[noreturn]], so we abort instead. This may need to be revisited
  46. // if we want MSVC support in the future.
  47. std::abort();
  48. #endif
  49. }
  50. } // namespace openscreen