Skip to content

Commit

Permalink
Add some nullability annotations in logging and tidy up some NOLINTs …
Browse files Browse the repository at this point in the history
…and comments.

PiperOrigin-RevId: 694512830
Change-Id: Ibeb8eb5fd18af5c49fe45a903e5cb129ee15974d
  • Loading branch information
suertreus authored and copybara-github committed Nov 8, 2024
1 parent 0c7c676 commit dee1b6c
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 58 deletions.
1 change: 1 addition & 0 deletions absl/log/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ cc_library(
deps = [
":log_sink",
"//absl/base:config",
"//absl/base:nullability",
"//absl/log/internal:log_sink_set",
],
)
Expand Down
8 changes: 5 additions & 3 deletions absl/log/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ absl_cc_library(
absl::config
absl::core_headers
absl::errno_saver
absl::inlined_vector
absl::examine_stack
absl::inlined_vector
absl::log_internal_append_truncated
absl::log_internal_format
absl::log_internal_globals
Expand All @@ -210,11 +210,12 @@ absl_cc_library(
absl::log_sink
absl::log_sink_registry
absl::memory
absl::nullability
absl::raw_logging_internal
absl::strings
absl::span
absl::strerror
absl::strings
absl::time
absl::span
)

absl_cc_library(
Expand Down Expand Up @@ -603,6 +604,7 @@ absl_cc_library(
absl::config
absl::log_sink
absl::log_internal_log_sink_set
absl::nullability
PUBLIC
)

Expand Down
1 change: 1 addition & 0 deletions absl/log/internal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ cc_library(
"//absl/base:core_headers",
"//absl/base:errno_saver",
"//absl/base:log_severity",
"//absl/base:nullability",
"//absl/base:raw_logging_internal",
"//absl/base:strerror",
"//absl/container:inlined_vector",
Expand Down
17 changes: 10 additions & 7 deletions absl/log/internal/log_message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -418,23 +418,26 @@ LogMessage& LogMessage::operator<<(std::ios_base& (*m)(std::ios_base& os)) {
data_->manipulated << m;
return *this;
}
// NOLINTBEGIN(runtime/int)
// NOLINTBEGIN(google-runtime-int)
template LogMessage& LogMessage::operator<<(const char& v);
template LogMessage& LogMessage::operator<<(const signed char& v);
template LogMessage& LogMessage::operator<<(const unsigned char& v);
template LogMessage& LogMessage::operator<<(const short& v); // NOLINT
template LogMessage& LogMessage::operator<<(const unsigned short& v); // NOLINT
template LogMessage& LogMessage::operator<<(const short& v);
template LogMessage& LogMessage::operator<<(const unsigned short& v);
template LogMessage& LogMessage::operator<<(const int& v);
template LogMessage& LogMessage::operator<<(const unsigned int& v);
template LogMessage& LogMessage::operator<<(const long& v); // NOLINT
template LogMessage& LogMessage::operator<<(const unsigned long& v); // NOLINT
template LogMessage& LogMessage::operator<<(const long long& v); // NOLINT
template LogMessage& LogMessage::operator<<(
const unsigned long long& v); // NOLINT
template LogMessage& LogMessage::operator<<(const long& v);
template LogMessage& LogMessage::operator<<(const unsigned long& v);
template LogMessage& LogMessage::operator<<(const long long& v);
template LogMessage& LogMessage::operator<<(const unsigned long long& v);
template LogMessage& LogMessage::operator<<(void* const& v);
template LogMessage& LogMessage::operator<<(const void* const& v);
template LogMessage& LogMessage::operator<<(const float& v);
template LogMessage& LogMessage::operator<<(const double& v);
template LogMessage& LogMessage::operator<<(const bool& v);
// NOLINTEND(google-runtime-int)
// NOLINTEND(runtime/int)

void LogMessage::Flush() {
if (data_->entry.log_severity() < absl::MinLogLevel()) return;
Expand Down
109 changes: 63 additions & 46 deletions absl/log/internal/log_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,19 @@
#ifndef ABSL_LOG_INTERNAL_LOG_MESSAGE_H_
#define ABSL_LOG_INTERNAL_LOG_MESSAGE_H_

#include <cstddef>
#include <ios>
#include <memory>
#include <ostream>
#include <streambuf>
#include <string>
#include <type_traits>

#include "absl/base/attributes.h"
#include "absl/base/config.h"
#include "absl/base/internal/errno_saver.h"
#include "absl/base/log_severity.h"
#include "absl/base/nullability.h"
#include "absl/log/internal/nullguard.h"
#include "absl/log/log_entry.h"
#include "absl/log/log_sink.h"
Expand All @@ -56,15 +59,15 @@ class LogMessage {
struct ErrorTag {};

// Used for `LOG`.
LogMessage(const char* file, int line,
LogMessage(absl::Nonnull<const char*> file, int line,
absl::LogSeverity severity) ABSL_ATTRIBUTE_COLD;
// These constructors are slightly smaller/faster to call; the severity is
// curried into the function pointer.
LogMessage(const char* file, int line,
LogMessage(absl::Nonnull<const char*> file, int line,
InfoTag) ABSL_ATTRIBUTE_COLD ABSL_ATTRIBUTE_NOINLINE;
LogMessage(const char* file, int line,
LogMessage(absl::Nonnull<const char*> file, int line,
WarningTag) ABSL_ATTRIBUTE_COLD ABSL_ATTRIBUTE_NOINLINE;
LogMessage(const char* file, int line,
LogMessage(absl::Nonnull<const char*> file, int line,
ErrorTag) ABSL_ATTRIBUTE_COLD ABSL_ATTRIBUTE_NOINLINE;
LogMessage(const LogMessage&) = delete;
LogMessage& operator=(const LogMessage&) = delete;
Expand Down Expand Up @@ -95,59 +98,67 @@ class LogMessage {
// of `errno`.
LogMessage& WithPerror();
// Sends this message to `*sink` in addition to whatever other sinks it would
// otherwise have been sent to. `sink` must not be null.
LogMessage& ToSinkAlso(absl::LogSink* sink);
// Sends this message to `*sink` and no others. `sink` must not be null.
LogMessage& ToSinkOnly(absl::LogSink* sink);
// otherwise have been sent to.
LogMessage& ToSinkAlso(absl::Nonnull<absl::LogSink*> sink);
// Sends this message to `*sink` and no others.
LogMessage& ToSinkOnly(absl::Nonnull<absl::LogSink*> sink);

// Don't call this method from outside this library.
LogMessage& InternalStream() { return *this; }

// By-value overloads for small, common types let us overlook common failures
// to define globals and static data members (i.e. in a .cc file).
// clang-format off
// The CUDA toolchain cannot handle these <<<'s:
// NOLINTBEGIN(runtime/int)
// NOLINTBEGIN(google-runtime-int)
// clang-format off: The CUDA toolchain cannot handle these <<<'s
LogMessage& operator<<(char v) { return operator<< <char>(v); }
LogMessage& operator<<(signed char v) { return operator<< <signed char>(v); }
LogMessage& operator<<(unsigned char v) {
return operator<< <unsigned char>(v);
}
LogMessage& operator<<(signed short v) { // NOLINT
return operator<< <signed short>(v); // NOLINT
LogMessage& operator<<(signed short v) {
return operator<< <signed short>(v);
}
LogMessage& operator<<(signed int v) { return operator<< <signed int>(v); }
LogMessage& operator<<(signed long v) { // NOLINT
return operator<< <signed long>(v); // NOLINT
LogMessage& operator<<(signed long v) {
return operator<< <signed long>(v);
}
LogMessage& operator<<(signed long long v) { // NOLINT
return operator<< <signed long long>(v); // NOLINT
LogMessage& operator<<(signed long long v) {
return operator<< <signed long long>(v);
}
LogMessage& operator<<(unsigned short v) { // NOLINT
return operator<< <unsigned short>(v); // NOLINT
LogMessage& operator<<(unsigned short v) {
return operator<< <unsigned short>(v);
}
LogMessage& operator<<(unsigned int v) {
return operator<< <unsigned int>(v);
}
LogMessage& operator<<(unsigned long v) { // NOLINT
return operator<< <unsigned long>(v); // NOLINT
LogMessage& operator<<(unsigned long v) {
return operator<< <unsigned long>(v);
}
LogMessage& operator<<(unsigned long long v) { // NOLINT
return operator<< <unsigned long long>(v); // NOLINT
LogMessage& operator<<(unsigned long long v) {
return operator<< <unsigned long long>(v);
}
LogMessage& operator<<(absl::Nullable<void*> v) {
return operator<< <void*>(v);
}
LogMessage& operator<<(absl::Nullable<const void*> v) {
return operator<< <const void*>(v);
}
LogMessage& operator<<(void* v) { return operator<< <void*>(v); }
LogMessage& operator<<(const void* v) { return operator<< <const void*>(v); }
LogMessage& operator<<(float v) { return operator<< <float>(v); }
LogMessage& operator<<(double v) { return operator<< <double>(v); }
LogMessage& operator<<(bool v) { return operator<< <bool>(v); }
// clang-format on
// NOLINTEND(google-runtime-int)
// NOLINTEND(runtime/int)

// These overloads are more efficient since no `ostream` is involved.
LogMessage& operator<<(const std::string& v);
LogMessage& operator<<(absl::string_view v);

// Handle stream manipulators e.g. std::endl.
LogMessage& operator<<(std::ostream& (*m)(std::ostream& os));
LogMessage& operator<<(std::ios_base& (*m)(std::ios_base& os));
LogMessage& operator<<(absl::Nonnull<std::ostream& (*)(std::ostream & os)> m);
LogMessage& operator<<(
absl::Nonnull<std::ios_base& (*)(std::ios_base & os)> m);

// Literal strings. This allows us to record C string literals as literals in
// the logging.proto.Value.
Expand Down Expand Up @@ -255,7 +266,7 @@ class LogMessage {

// We keep the data in a separate struct so that each instance of `LogMessage`
// uses less stack space.
std::unique_ptr<LogMessageData> data_;
absl::Nonnull<std::unique_ptr<LogMessageData>> data_;
};

// Helper class so that `AbslStringify()` can modify the LogMessage.
Expand All @@ -273,7 +284,8 @@ class StringifySink final {
}

// For types that implement `AbslStringify` using `absl::Format()`.
friend void AbslFormatFlush(StringifySink* sink, absl::string_view v) {
friend void AbslFormatFlush(absl::Nonnull<StringifySink*> sink,
absl::string_view v) {
sink->Append(v);
}

Expand Down Expand Up @@ -315,27 +327,28 @@ LogMessage& LogMessage::operator<<(char (&buf)[SIZE]) {
// We instantiate these specializations in the library's TU to save space in
// other TUs. Since the template is marked `ABSL_ATTRIBUTE_NOINLINE` we will be
// emitting a function call either way.
// NOLINTBEGIN(runtime/int)
// NOLINTBEGIN(google-runtime-int)
extern template LogMessage& LogMessage::operator<<(const char& v);
extern template LogMessage& LogMessage::operator<<(const signed char& v);
extern template LogMessage& LogMessage::operator<<(const unsigned char& v);
extern template LogMessage& LogMessage::operator<<(const short& v); // NOLINT
extern template LogMessage& LogMessage::operator<<(
const unsigned short& v); // NOLINT
extern template LogMessage& LogMessage::operator<<(const short& v);
extern template LogMessage& LogMessage::operator<<(const unsigned short& v);
extern template LogMessage& LogMessage::operator<<(const int& v);
extern template LogMessage& LogMessage::operator<<(const unsigned int& v);
extern template LogMessage& LogMessage::operator<<(const long& v);
extern template LogMessage& LogMessage::operator<<(const unsigned long& v);
extern template LogMessage& LogMessage::operator<<(const long long& v);
extern template LogMessage& LogMessage::operator<<(const unsigned long long& v);
extern template LogMessage& LogMessage::operator<<(
const unsigned int& v); // NOLINT
extern template LogMessage& LogMessage::operator<<(const long& v); // NOLINT
extern template LogMessage& LogMessage::operator<<(
const unsigned long& v); // NOLINT
extern template LogMessage& LogMessage::operator<<(
const long long& v); // NOLINT
absl::Nullable<void*> const& v);
extern template LogMessage& LogMessage::operator<<(
const unsigned long long& v); // NOLINT
extern template LogMessage& LogMessage::operator<<(void* const& v);
extern template LogMessage& LogMessage::operator<<(const void* const& v);
absl::Nullable<const void*> const& v);
extern template LogMessage& LogMessage::operator<<(const float& v);
extern template LogMessage& LogMessage::operator<<(const double& v);
extern template LogMessage& LogMessage::operator<<(const bool& v);
// NOLINTEND(google-runtime-int)
// NOLINTEND(runtime/int)

extern template void LogMessage::CopyToEncodedBuffer<
LogMessage::StringType::kLiteral>(absl::string_view str);
Expand All @@ -351,8 +364,9 @@ extern template void LogMessage::CopyToEncodedBuffer<
// message.
class LogMessageFatal final : public LogMessage {
public:
LogMessageFatal(const char* file, int line) ABSL_ATTRIBUTE_COLD;
LogMessageFatal(const char* file, int line,
LogMessageFatal(absl::Nonnull<const char*> file,
int line) ABSL_ATTRIBUTE_COLD;
LogMessageFatal(absl::Nonnull<const char*> file, int line,
absl::string_view failure_msg) ABSL_ATTRIBUTE_COLD;
[[noreturn]] ~LogMessageFatal();
};
Expand All @@ -362,7 +376,8 @@ class LogMessageFatal final : public LogMessage {
// for DLOG(FATAL) variants.
class LogMessageDebugFatal final : public LogMessage {
public:
LogMessageDebugFatal(const char* file, int line) ABSL_ATTRIBUTE_COLD;
LogMessageDebugFatal(absl::Nonnull<const char*> file,
int line) ABSL_ATTRIBUTE_COLD;
~LogMessageDebugFatal();
};

Expand All @@ -371,15 +386,17 @@ class LogMessageQuietlyDebugFatal final : public LogMessage {
// DLOG(QFATAL) calls this instead of LogMessageQuietlyFatal to make sure the
// destructor is not [[noreturn]] even if this is always FATAL as this is only
// invoked when DLOG() is enabled.
LogMessageQuietlyDebugFatal(const char* file, int line) ABSL_ATTRIBUTE_COLD;
LogMessageQuietlyDebugFatal(absl::Nonnull<const char*> file,
int line) ABSL_ATTRIBUTE_COLD;
~LogMessageQuietlyDebugFatal();
};

// Used for LOG(QFATAL) to make sure it's properly understood as [[noreturn]].
class LogMessageQuietlyFatal final : public LogMessage {
public:
LogMessageQuietlyFatal(const char* file, int line) ABSL_ATTRIBUTE_COLD;
LogMessageQuietlyFatal(const char* file, int line,
LogMessageQuietlyFatal(absl::Nonnull<const char*> file,
int line) ABSL_ATTRIBUTE_COLD;
LogMessageQuietlyFatal(absl::Nonnull<const char*> file, int line,
absl::string_view failure_msg) ABSL_ATTRIBUTE_COLD;
[[noreturn]] ~LogMessageQuietlyFatal();
};
Expand Down
7 changes: 5 additions & 2 deletions absl/log/log_sink_registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#define ABSL_LOG_LOG_SINK_REGISTRY_H_

#include "absl/base/config.h"
#include "absl/base/nullability.h"
#include "absl/log/internal/log_sink_set.h"
#include "absl/log/log_sink.h"

Expand All @@ -43,8 +44,10 @@ ABSL_NAMESPACE_BEGIN
// sink instead which writes them to `stderr`.
//
// Do not call these inside `absl::LogSink::Send`.
inline void AddLogSink(absl::LogSink* sink) { log_internal::AddLogSink(sink); }
inline void RemoveLogSink(absl::LogSink* sink) {
inline void AddLogSink(absl::Nonnull<absl::LogSink*> sink) {
log_internal::AddLogSink(sink);
}
inline void RemoveLogSink(absl::Nonnull<absl::LogSink*> sink) {
log_internal::RemoveLogSink(sink);
}

Expand Down

0 comments on commit dee1b6c

Please sign in to comment.