Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-11-15 09:01:05

0001 // Copyright 2022 The Abseil Authors.
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //      https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 //
0015 // -----------------------------------------------------------------------------
0016 // File: log/internal/nullstream.h
0017 // -----------------------------------------------------------------------------
0018 //
0019 // Classes `NullStream`, `NullStreamMaybeFatal ` and `NullStreamFatal`
0020 // implement a subset of the `LogMessage` API and are used instead when logging
0021 // of messages has been disabled.
0022 
0023 #ifndef ABSL_LOG_INTERNAL_NULLSTREAM_H_
0024 #define ABSL_LOG_INTERNAL_NULLSTREAM_H_
0025 
0026 #ifdef _WIN32
0027 #include <cstdlib>
0028 #else
0029 #include <unistd.h>
0030 #endif
0031 #include <ios>
0032 #include <ostream>
0033 
0034 #include "absl/base/attributes.h"
0035 #include "absl/base/config.h"
0036 #include "absl/base/log_severity.h"
0037 #include "absl/strings/string_view.h"
0038 
0039 namespace absl {
0040 ABSL_NAMESPACE_BEGIN
0041 namespace log_internal {
0042 
0043 // A `NullStream` implements the API of `LogMessage` (a few methods and
0044 // `operator<<`) but does nothing.  All methods are defined inline so the
0045 // compiler can eliminate the whole instance and discard anything that's
0046 // streamed in.
0047 class NullStream {
0048  public:
0049   NullStream& AtLocation(absl::string_view, int) { return *this; }
0050   template <typename SourceLocationType>
0051   NullStream& AtLocation(SourceLocationType) {
0052     return *this;
0053   }
0054   NullStream& NoPrefix() { return *this; }
0055   NullStream& WithVerbosity(int) { return *this; }
0056   template <typename TimeType>
0057   NullStream& WithTimestamp(TimeType) {
0058     return *this;
0059   }
0060   template <typename Tid>
0061   NullStream& WithThreadID(Tid) {
0062     return *this;
0063   }
0064   template <typename LogEntryType>
0065   NullStream& WithMetadataFrom(const LogEntryType&) {
0066     return *this;
0067   }
0068   NullStream& WithPerror() { return *this; }
0069   template <typename LogSinkType>
0070   NullStream& ToSinkAlso(LogSinkType*) {
0071     return *this;
0072   }
0073   template <typename LogSinkType>
0074   NullStream& ToSinkOnly(LogSinkType*) {
0075     return *this;
0076   }
0077   template <typename LogSinkType>
0078   NullStream& OutputToSink(LogSinkType*, bool) {
0079     return *this;
0080   }
0081   NullStream& InternalStream() { return *this; }
0082 };
0083 template <typename T>
0084 inline NullStream& operator<<(NullStream& str, const T&) {
0085   return str;
0086 }
0087 inline NullStream& operator<<(NullStream& str,
0088                               std::ostream& (*)(std::ostream& os)) {
0089   return str;
0090 }
0091 inline NullStream& operator<<(NullStream& str,
0092                               std::ios_base& (*)(std::ios_base& os)) {
0093   return str;
0094 }
0095 
0096 // `NullStreamMaybeFatal` implements the process termination semantics of
0097 // `LogMessage`, which is used for `DFATAL` severity and expression-defined
0098 // severity e.g. `LOG(LEVEL(HowBadIsIt()))`.  Like `LogMessage`, it terminates
0099 // the process when destroyed if the passed-in severity equals `FATAL`.
0100 class NullStreamMaybeFatal final : public NullStream {
0101  public:
0102   explicit NullStreamMaybeFatal(absl::LogSeverity severity)
0103       : fatal_(severity == absl::LogSeverity::kFatal) {}
0104   ~NullStreamMaybeFatal() {
0105     if (fatal_) {
0106       _exit(1);
0107     }
0108   }
0109 
0110  private:
0111   bool fatal_;
0112 };
0113 
0114 // `NullStreamFatal` implements the process termination semantics of
0115 // `LogMessageFatal`, which means it always terminates the process.  `DFATAL`
0116 // and expression-defined severity use `NullStreamMaybeFatal` above.
0117 class NullStreamFatal final : public NullStream {
0118  public:
0119   NullStreamFatal() = default;
0120   // ABSL_ATTRIBUTE_NORETURN doesn't seem to work on destructors with msvc, so
0121   // disable msvc's warning about the d'tor never returning.
0122 #if defined(_MSC_VER) && !defined(__clang__)
0123 #pragma warning(push)
0124 #pragma warning(disable : 4722)
0125 #endif
0126   ABSL_ATTRIBUTE_NORETURN ~NullStreamFatal() { _exit(1); }
0127 #if defined(_MSC_VER) && !defined(__clang__)
0128 #pragma warning(pop)
0129 #endif
0130 };
0131 
0132 }  // namespace log_internal
0133 ABSL_NAMESPACE_END
0134 }  // namespace absl
0135 
0136 #endif  // ABSL_LOG_INTERNAL_GLOBALS_H_