File size: 4,334 Bytes
5f4cece | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | // Copyright 2022 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// -----------------------------------------------------------------------------
// File: log/internal/nullstream.h
// -----------------------------------------------------------------------------
//
// Classes `NullStream`, `NullStreamMaybeFatal ` and `NullStreamFatal`
// implement a subset of the `LogMessage` API and are used instead when logging
// of messages has been disabled.
#ifndef ABSL_LOG_INTERNAL_NULLSTREAM_H_
#define ABSL_LOG_INTERNAL_NULLSTREAM_H_
#ifdef _WIN32
#include <cstdlib>
#else
#include <unistd.h>
#endif
#include <ios>
#include <ostream>
#include "absl/base/attributes.h"
#include "absl/base/config.h"
#include "absl/base/log_severity.h"
#include "absl/strings/string_view.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace log_internal {
// A `NullStream` implements the API of `LogMessage` (a few methods and
// `operator<<`) but does nothing. All methods are defined inline so the
// compiler can eliminate the whole instance and discard anything that's
// streamed in.
class NullStream {
public:
NullStream& AtLocation(absl::string_view, int) { return *this; }
template <typename SourceLocationType>
NullStream& AtLocation(SourceLocationType) {
return *this;
}
NullStream& NoPrefix() { return *this; }
NullStream& WithVerbosity(int) { return *this; }
template <typename TimeType>
NullStream& WithTimestamp(TimeType) {
return *this;
}
template <typename Tid>
NullStream& WithThreadID(Tid) {
return *this;
}
template <typename LogEntryType>
NullStream& WithMetadataFrom(const LogEntryType&) {
return *this;
}
NullStream& WithPerror() { return *this; }
template <typename LogSinkType>
NullStream& ToSinkAlso(LogSinkType*) {
return *this;
}
template <typename LogSinkType>
NullStream& ToSinkOnly(LogSinkType*) {
return *this;
}
template <typename LogSinkType>
NullStream& OutputToSink(LogSinkType*, bool) {
return *this;
}
NullStream& InternalStream() { return *this; }
};
template <typename T>
inline NullStream& operator<<(NullStream& str, const T&) {
return str;
}
inline NullStream& operator<<(NullStream& str,
std::ostream& (*)(std::ostream& os)) {
return str;
}
inline NullStream& operator<<(NullStream& str,
std::ios_base& (*)(std::ios_base& os)) {
return str;
}
// `NullStreamMaybeFatal` implements the process termination semantics of
// `LogMessage`, which is used for `DFATAL` severity and expression-defined
// severity e.g. `LOG(LEVEL(HowBadIsIt()))`. Like `LogMessage`, it terminates
// the process when destroyed if the passed-in severity equals `FATAL`.
class NullStreamMaybeFatal final : public NullStream {
public:
explicit NullStreamMaybeFatal(absl::LogSeverity severity)
: fatal_(severity == absl::LogSeverity::kFatal) {}
~NullStreamMaybeFatal() {
if (fatal_) {
_exit(1);
}
}
private:
bool fatal_;
};
// `NullStreamFatal` implements the process termination semantics of
// `LogMessageFatal`, which means it always terminates the process. `DFATAL`
// and expression-defined severity use `NullStreamMaybeFatal` above.
class NullStreamFatal final : public NullStream {
public:
NullStreamFatal() = default;
// ABSL_ATTRIBUTE_NORETURN doesn't seem to work on destructors with msvc, so
// disable msvc's warning about the d'tor never returning.
#if defined(_MSC_VER) && !defined(__clang__)
#pragma warning(push)
#pragma warning(disable : 4722)
#endif
ABSL_ATTRIBUTE_NORETURN ~NullStreamFatal() { _exit(1); }
#if defined(_MSC_VER) && !defined(__clang__)
#pragma warning(pop)
#endif
};
} // namespace log_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_LOG_INTERNAL_GLOBALS_H_
|