Coverage Report

Created: 2025-04-27 06:20

/src/LPM/external.protobuf/include/absl/log/internal/nullstream.h
Line
Count
Source (jump to first uncovered line)
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
23
#ifndef ABSL_LOG_INTERNAL_NULLSTREAM_H_
24
#define ABSL_LOG_INTERNAL_NULLSTREAM_H_
25
26
#ifdef _WIN32
27
#include <cstdlib>
28
#else
29
#include <unistd.h>
30
#endif
31
#include <ios>
32
#include <ostream>
33
34
#include "absl/base/attributes.h"
35
#include "absl/base/config.h"
36
#include "absl/base/log_severity.h"
37
#include "absl/strings/string_view.h"
38
39
namespace absl {
40
ABSL_NAMESPACE_BEGIN
41
namespace log_internal {
42
43
// A `NullStream` implements the API of `LogMessage` (a few methods and
44
// `operator<<`) but does nothing.  All methods are defined inline so the
45
// compiler can eliminate the whole instance and discard anything that's
46
// streamed in.
47
class NullStream {
48
 public:
49
0
  NullStream& AtLocation(absl::string_view, int) { return *this; }
50
  template <typename SourceLocationType>
51
  NullStream& AtLocation(SourceLocationType) {
52
    return *this;
53
  }
54
0
  NullStream& NoPrefix() { return *this; }
55
0
  NullStream& WithVerbosity(int) { return *this; }
56
  template <typename TimeType>
57
  NullStream& WithTimestamp(TimeType) {
58
    return *this;
59
  }
60
  template <typename Tid>
61
  NullStream& WithThreadID(Tid) {
62
    return *this;
63
  }
64
  template <typename LogEntryType>
65
  NullStream& WithMetadataFrom(const LogEntryType&) {
66
    return *this;
67
  }
68
0
  NullStream& WithPerror() { return *this; }
69
  template <typename LogSinkType>
70
  NullStream& ToSinkAlso(LogSinkType*) {
71
    return *this;
72
  }
73
  template <typename LogSinkType>
74
  NullStream& ToSinkOnly(LogSinkType*) {
75
    return *this;
76
  }
77
  template <typename LogSinkType>
78
  NullStream& OutputToSink(LogSinkType*, bool) {
79
    return *this;
80
  }
81
0
  NullStream& InternalStream() { return *this; }
82
};
83
template <typename T>
84
0
inline NullStream& operator<<(NullStream& str, const T&) {
85
0
  return str;
86
0
}
Unexecuted instantiation: absl::lts_20240116::log_internal::NullStream& absl::lts_20240116::log_internal::operator<< <char [48]>(absl::lts_20240116::log_internal::NullStream&, char const (&) [48])
Unexecuted instantiation: absl::lts_20240116::log_internal::NullStream& absl::lts_20240116::log_internal::operator<< <char [59]>(absl::lts_20240116::log_internal::NullStream&, char const (&) [59])
87
inline NullStream& operator<<(NullStream& str,
88
0
                              std::ostream& (*)(std::ostream& os)) {
89
0
  return str;
90
0
}
91
inline NullStream& operator<<(NullStream& str,
92
0
                              std::ios_base& (*)(std::ios_base& os)) {
93
0
  return str;
94
0
}
95
96
// `NullStreamMaybeFatal` implements the process termination semantics of
97
// `LogMessage`, which is used for `DFATAL` severity and expression-defined
98
// severity e.g. `LOG(LEVEL(HowBadIsIt()))`.  Like `LogMessage`, it terminates
99
// the process when destroyed if the passed-in severity equals `FATAL`.
100
class NullStreamMaybeFatal final : public NullStream {
101
 public:
102
  explicit NullStreamMaybeFatal(absl::LogSeverity severity)
103
0
      : fatal_(severity == absl::LogSeverity::kFatal) {}
104
0
  ~NullStreamMaybeFatal() {
105
0
    if (fatal_) {
106
0
      _exit(1);
107
0
    }
108
0
  }
109
110
 private:
111
  bool fatal_;
112
};
113
114
// `NullStreamFatal` implements the process termination semantics of
115
// `LogMessageFatal`, which means it always terminates the process.  `DFATAL`
116
// and expression-defined severity use `NullStreamMaybeFatal` above.
117
class NullStreamFatal final : public NullStream {
118
 public:
119
  NullStreamFatal() = default;
120
  // ABSL_ATTRIBUTE_NORETURN doesn't seem to work on destructors with msvc, so
121
  // disable msvc's warning about the d'tor never returning.
122
#if defined(_MSC_VER) && !defined(__clang__)
123
#pragma warning(push)
124
#pragma warning(disable : 4722)
125
#endif
126
0
  ABSL_ATTRIBUTE_NORETURN ~NullStreamFatal() { _exit(1); }
127
#if defined(_MSC_VER) && !defined(__clang__)
128
#pragma warning(pop)
129
#endif
130
};
131
132
}  // namespace log_internal
133
ABSL_NAMESPACE_END
134
}  // namespace absl
135
136
#endif  // ABSL_LOG_INTERNAL_GLOBALS_H_