Coverage Report

Created: 2026-09-14 06:45

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