Skip to content

Commit

Permalink
Add ABSL_ATTRIBUTE_LIFETIME_BOUND and a doc note about absl::LogAsLit…
Browse files Browse the repository at this point in the history
…eral to clarify its intended use.

PiperOrigin-RevId: 696592874
Change-Id: I6d6bc4690f891fbbdd8489b2e2f6398b43d63001
  • Loading branch information
suertreus authored and copybara-github committed Nov 14, 2024
1 parent c313056 commit 8f739d1
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions absl/log/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ cc_library(
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
"//absl/base:config",
"//absl/base:core_headers",
"//absl/log/internal:structured",
"//absl/strings",
],
Expand Down
2 changes: 2 additions & 0 deletions absl/log/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@ absl_cc_library(
${ABSL_DEFAULT_LINKOPTS}
DEPS
absl::config
absl::core_headers
absl::log_internal_message
absl::strings
)
Expand All @@ -680,6 +681,7 @@ absl_cc_library(
${ABSL_DEFAULT_LINKOPTS}
DEPS
absl::config
absl::core_headers
absl::log_internal_structured
absl::strings
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 @@ -281,6 +281,7 @@ cc_library(
deps = [
":log_message",
"//absl/base:config",
"//absl/base:core_headers",
"//absl/strings",
],
)
Expand Down
7 changes: 5 additions & 2 deletions absl/log/internal/structured.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <ostream>

#include "absl/base/config.h"
#include "absl/base/attributes.h"
#include "absl/log/internal/log_message.h"
#include "absl/strings/string_view.h"

Expand All @@ -31,14 +32,16 @@ namespace log_internal {

class ABSL_MUST_USE_RESULT AsLiteralImpl final {
public:
explicit AsLiteralImpl(absl::string_view str) : str_(str) {}
explicit AsLiteralImpl(absl::string_view str ABSL_ATTRIBUTE_LIFETIME_BOUND)
: str_(str) {}
AsLiteralImpl(const AsLiteralImpl&) = default;
AsLiteralImpl& operator=(const AsLiteralImpl&) = default;

private:
absl::string_view str_;

friend std::ostream& operator<<(std::ostream& os, AsLiteralImpl as_literal) {
friend std::ostream& operator<<(std::ostream& os,
AsLiteralImpl&& as_literal) {
return os << as_literal.str_;
}
void AddToMessage(log_internal::LogMessage& m) {
Expand Down
7 changes: 6 additions & 1 deletion absl/log/structured.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include <ostream>

#include "absl/base/attributes.h"
#include "absl/base/config.h"
#include "absl/log/internal/structured.h"
#include "absl/strings/string_view.h"
Expand Down Expand Up @@ -60,7 +61,11 @@ ABSL_NAMESPACE_BEGIN
// int line) {
// LOG(LEVEL(severity)).AtLocation(file, line) << absl::LogAsLiteral(str);
// }
inline log_internal::AsLiteralImpl LogAsLiteral(absl::string_view s) {
//
// `LogAsLiteral` should only be used as a streaming operand and not, for
// example, as a local variable initializer.
inline log_internal::AsLiteralImpl LogAsLiteral(
absl::string_view s ABSL_ATTRIBUTE_LIFETIME_BOUND) {
return log_internal::AsLiteralImpl(s);
}

Expand Down

0 comments on commit 8f739d1

Please sign in to comment.