strip.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2022 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // -----------------------------------------------------------------------------
  16. // File: log/internal/strip.h
  17. // -----------------------------------------------------------------------------
  18. //
  19. #ifndef ABSL_LOG_INTERNAL_STRIP_H_
  20. #define ABSL_LOG_INTERNAL_STRIP_H_
  21. #include "absl/base/attributes.h" // IWYU pragma: keep
  22. #include "absl/base/log_severity.h"
  23. #include "absl/log/internal/log_message.h"
  24. #include "absl/log/internal/nullstream.h"
  25. // `ABSL_LOGGING_INTERNAL_LOG_*` evaluates to a temporary `LogMessage` object or
  26. // to a related object with a compatible API but different behavior. This set
  27. // of defines comes in three flavors: vanilla, plus two variants that strip some
  28. // logging in subtly different ways for subtly different reasons (see below).
  29. #if defined(STRIP_LOG) && STRIP_LOG
  30. // Attribute for marking variables used in implementation details of logging
  31. // macros as unused, but only when `STRIP_LOG` is defined.
  32. // With `STRIP_LOG` on, not marking them triggers `-Wunused-but-set-variable`,
  33. // With `STRIP_LOG` off, marking them triggers `-Wused-but-marked-unused`.
  34. //
  35. // TODO(b/290784225): Replace this macro with attribute [[maybe_unused]] when
  36. // Abseil stops supporting C++14.
  37. #define ABSL_LOG_INTERNAL_ATTRIBUTE_UNUSED_IF_STRIP_LOG ABSL_ATTRIBUTE_UNUSED
  38. #define ABSL_LOGGING_INTERNAL_LOG_INFO ::absl::log_internal::NullStream()
  39. #define ABSL_LOGGING_INTERNAL_LOG_WARNING ::absl::log_internal::NullStream()
  40. #define ABSL_LOGGING_INTERNAL_LOG_ERROR ::absl::log_internal::NullStream()
  41. #define ABSL_LOGGING_INTERNAL_LOG_FATAL ::absl::log_internal::NullStreamFatal()
  42. #define ABSL_LOGGING_INTERNAL_LOG_QFATAL ::absl::log_internal::NullStreamFatal()
  43. #define ABSL_LOGGING_INTERNAL_LOG_DFATAL \
  44. ::absl::log_internal::NullStreamMaybeFatal(::absl::kLogDebugFatal)
  45. #define ABSL_LOGGING_INTERNAL_LOG_LEVEL(severity) \
  46. ::absl::log_internal::NullStreamMaybeFatal(absl_log_internal_severity)
  47. // Fatal `DLOG`s expand a little differently to avoid being `[[noreturn]]`.
  48. #define ABSL_LOGGING_INTERNAL_DLOG_FATAL \
  49. ::absl::log_internal::NullStreamMaybeFatal(::absl::LogSeverity::kFatal)
  50. #define ABSL_LOGGING_INTERNAL_DLOG_QFATAL \
  51. ::absl::log_internal::NullStreamMaybeFatal(::absl::LogSeverity::kFatal)
  52. #define ABSL_LOG_INTERNAL_CHECK(failure_message) ABSL_LOGGING_INTERNAL_LOG_FATAL
  53. #define ABSL_LOG_INTERNAL_QCHECK(failure_message) \
  54. ABSL_LOGGING_INTERNAL_LOG_QFATAL
  55. #else // !defined(STRIP_LOG) || !STRIP_LOG
  56. #define ABSL_LOG_INTERNAL_ATTRIBUTE_UNUSED_IF_STRIP_LOG
  57. #define ABSL_LOGGING_INTERNAL_LOG_INFO \
  58. ::absl::log_internal::LogMessage( \
  59. __FILE__, __LINE__, ::absl::log_internal::LogMessage::InfoTag{})
  60. #define ABSL_LOGGING_INTERNAL_LOG_WARNING \
  61. ::absl::log_internal::LogMessage( \
  62. __FILE__, __LINE__, ::absl::log_internal::LogMessage::WarningTag{})
  63. #define ABSL_LOGGING_INTERNAL_LOG_ERROR \
  64. ::absl::log_internal::LogMessage( \
  65. __FILE__, __LINE__, ::absl::log_internal::LogMessage::ErrorTag{})
  66. #define ABSL_LOGGING_INTERNAL_LOG_FATAL \
  67. ::absl::log_internal::LogMessageFatal(__FILE__, __LINE__)
  68. #define ABSL_LOGGING_INTERNAL_LOG_QFATAL \
  69. ::absl::log_internal::LogMessageQuietlyFatal(__FILE__, __LINE__)
  70. #define ABSL_LOGGING_INTERNAL_LOG_DFATAL \
  71. ::absl::log_internal::LogMessage(__FILE__, __LINE__, ::absl::kLogDebugFatal)
  72. #define ABSL_LOGGING_INTERNAL_LOG_LEVEL(severity) \
  73. ::absl::log_internal::LogMessage(__FILE__, __LINE__, \
  74. absl_log_internal_severity)
  75. // Fatal `DLOG`s expand a little differently to avoid being `[[noreturn]]`.
  76. #define ABSL_LOGGING_INTERNAL_DLOG_FATAL \
  77. ::absl::log_internal::LogMessageDebugFatal(__FILE__, __LINE__)
  78. #define ABSL_LOGGING_INTERNAL_DLOG_QFATAL \
  79. ::absl::log_internal::LogMessageQuietlyDebugFatal(__FILE__, __LINE__)
  80. // These special cases dispatch to special-case constructors that allow us to
  81. // avoid an extra function call and shrink non-LTO binaries by a percent or so.
  82. #define ABSL_LOG_INTERNAL_CHECK(failure_message) \
  83. ::absl::log_internal::LogMessageFatal(__FILE__, __LINE__, failure_message)
  84. #define ABSL_LOG_INTERNAL_QCHECK(failure_message) \
  85. ::absl::log_internal::LogMessageQuietlyFatal(__FILE__, __LINE__, \
  86. failure_message)
  87. #endif // !defined(STRIP_LOG) || !STRIP_LOG
  88. // This part of a non-fatal `DLOG`s expands the same as `LOG`.
  89. #define ABSL_LOGGING_INTERNAL_DLOG_INFO ABSL_LOGGING_INTERNAL_LOG_INFO
  90. #define ABSL_LOGGING_INTERNAL_DLOG_WARNING ABSL_LOGGING_INTERNAL_LOG_WARNING
  91. #define ABSL_LOGGING_INTERNAL_DLOG_ERROR ABSL_LOGGING_INTERNAL_LOG_ERROR
  92. #define ABSL_LOGGING_INTERNAL_DLOG_DFATAL ABSL_LOGGING_INTERNAL_LOG_DFATAL
  93. #define ABSL_LOGGING_INTERNAL_DLOG_LEVEL ABSL_LOGGING_INTERNAL_LOG_LEVEL
  94. #endif // ABSL_LOG_INTERNAL_STRIP_H_