nullstream.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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/nullstream.h
  17. // -----------------------------------------------------------------------------
  18. //
  19. // Classes `NullStream`, `NullStreamMaybeFatal ` and `NullStreamFatal`
  20. // implement a subset of the `LogMessage` API and are used instead when logging
  21. // of messages has been disabled.
  22. #ifndef Y_ABSL_LOG_INTERNAL_NULLSTREAM_H_
  23. #define Y_ABSL_LOG_INTERNAL_NULLSTREAM_H_
  24. #ifdef _WIN32
  25. #include <cstdlib>
  26. #else
  27. #include <unistd.h>
  28. #endif
  29. #include <ios>
  30. #include <ostream>
  31. #include "y_absl/base/attributes.h"
  32. #include "y_absl/base/config.h"
  33. #include "y_absl/base/log_severity.h"
  34. #include "y_absl/strings/string_view.h"
  35. namespace y_absl {
  36. Y_ABSL_NAMESPACE_BEGIN
  37. namespace log_internal {
  38. // A `NullStream` implements the API of `LogMessage` (a few methods and
  39. // `operator<<`) but does nothing. All methods are defined inline so the
  40. // compiler can eliminate the whole instance and discard anything that's
  41. // streamed in.
  42. class NullStream {
  43. public:
  44. NullStream& AtLocation(y_absl::string_view, int) { return *this; }
  45. template <typename SourceLocationType>
  46. NullStream& AtLocation(SourceLocationType) {
  47. return *this;
  48. }
  49. NullStream& NoPrefix() { return *this; }
  50. NullStream& WithVerbosity(int) { return *this; }
  51. template <typename TimeType>
  52. NullStream& WithTimestamp(TimeType) {
  53. return *this;
  54. }
  55. template <typename Tid>
  56. NullStream& WithThreadID(Tid) {
  57. return *this;
  58. }
  59. template <typename LogEntryType>
  60. NullStream& WithMetadataFrom(const LogEntryType&) {
  61. return *this;
  62. }
  63. NullStream& WithPerror() { return *this; }
  64. template <typename LogSinkType>
  65. NullStream& ToSinkAlso(LogSinkType*) {
  66. return *this;
  67. }
  68. template <typename LogSinkType>
  69. NullStream& ToSinkOnly(LogSinkType*) {
  70. return *this;
  71. }
  72. template <typename LogSinkType>
  73. NullStream& OutputToSink(LogSinkType*, bool) {
  74. return *this;
  75. }
  76. NullStream& InternalStream() { return *this; }
  77. };
  78. template <typename T>
  79. inline NullStream& operator<<(NullStream& str, const T&) {
  80. return str;
  81. }
  82. inline NullStream& operator<<(NullStream& str,
  83. std::ostream& (*)(std::ostream& os)) {
  84. return str;
  85. }
  86. inline NullStream& operator<<(NullStream& str,
  87. std::ios_base& (*)(std::ios_base& os)) {
  88. return str;
  89. }
  90. // `NullStreamMaybeFatal` implements the process termination semantics of
  91. // `LogMessage`, which is used for `DFATAL` severity and expression-defined
  92. // severity e.g. `LOG(LEVEL(HowBadIsIt()))`. Like `LogMessage`, it terminates
  93. // the process when destroyed if the passed-in severity equals `FATAL`.
  94. class NullStreamMaybeFatal final : public NullStream {
  95. public:
  96. explicit NullStreamMaybeFatal(y_absl::LogSeverity severity)
  97. : fatal_(severity == y_absl::LogSeverity::kFatal) {}
  98. ~NullStreamMaybeFatal() {
  99. if (fatal_) {
  100. _exit(1);
  101. }
  102. }
  103. private:
  104. bool fatal_;
  105. };
  106. // `NullStreamFatal` implements the process termination semantics of
  107. // `LogMessageFatal`, which means it always terminates the process. `DFATAL`
  108. // and expression-defined severity use `NullStreamMaybeFatal` above.
  109. class NullStreamFatal final : public NullStream {
  110. public:
  111. NullStreamFatal() = default;
  112. [[noreturn]] ~NullStreamFatal() { _exit(1); }
  113. };
  114. } // namespace log_internal
  115. Y_ABSL_NAMESPACE_END
  116. } // namespace y_absl
  117. #endif // Y_ABSL_LOG_INTERNAL_GLOBALS_H_