Commit 2d04ec1d by Abseil Team Committed by Copybara-Service

Add configurability of the log Tag value on Android

PiperOrigin-RevId: 525178448
Change-Id: I0f35a38f23316b25621148b7fe59becf72bafeff
parent f3618360
...@@ -109,6 +109,7 @@ cc_library( ...@@ -109,6 +109,7 @@ cc_library(
"//absl/base:config", "//absl/base:config",
"//absl/base:core_headers", "//absl/base:core_headers",
"//absl/base:log_severity", "//absl/base:log_severity",
"//absl/base:raw_logging_internal",
"//absl/hash", "//absl/hash",
"//absl/strings", "//absl/strings",
], ],
......
...@@ -501,6 +501,7 @@ absl_cc_library( ...@@ -501,6 +501,7 @@ absl_cc_library(
absl::core_headers absl::core_headers
absl::hash absl::hash
absl::log_severity absl::log_severity
absl::raw_logging_internal
absl::strings absl::strings
) )
......
...@@ -14,14 +14,17 @@ ...@@ -14,14 +14,17 @@
#include "absl/log/globals.h" #include "absl/log/globals.h"
#include <stddef.h>
#include <stdint.h>
#include <atomic> #include <atomic>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <string>
#include "absl/base/attributes.h" #include "absl/base/attributes.h"
#include "absl/base/config.h" #include "absl/base/config.h"
#include "absl/base/internal/atomic_hook.h" #include "absl/base/internal/atomic_hook.h"
#include "absl/base/internal/raw_logging.h"
#include "absl/base/log_severity.h" #include "absl/base/log_severity.h"
#include "absl/hash/hash.h" #include "absl/hash/hash.h"
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
...@@ -43,6 +46,9 @@ ABSL_CONST_INIT std::atomic<int> stderrthreshold{ ...@@ -43,6 +46,9 @@ ABSL_CONST_INIT std::atomic<int> stderrthreshold{
ABSL_CONST_INIT std::atomic<size_t> log_backtrace_at_hash{0}; ABSL_CONST_INIT std::atomic<size_t> log_backtrace_at_hash{0};
ABSL_CONST_INIT std::atomic<bool> prepend_log_prefix{true}; ABSL_CONST_INIT std::atomic<bool> prepend_log_prefix{true};
constexpr char kDefaultAndroidTag[] = "native";
ABSL_CONST_INIT std::atomic<const char*> android_log_tag{kDefaultAndroidTag};
ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES
absl::base_internal::AtomicHook<log_internal::LoggingGlobalsListener> absl::base_internal::AtomicHook<log_internal::LoggingGlobalsListener>
logging_globals_listener; logging_globals_listener;
...@@ -121,6 +127,26 @@ ScopedStderrThreshold::~ScopedStderrThreshold() { ...@@ -121,6 +127,26 @@ ScopedStderrThreshold::~ScopedStderrThreshold() {
namespace log_internal { namespace log_internal {
const char* GetAndroidNativeTag() {
return android_log_tag.load(std::memory_order_acquire);
}
} // namespace log_internal
void SetAndroidNativeTag(const char* tag) {
ABSL_CONST_INIT static std::atomic<const std::string*> user_log_tag(nullptr);
ABSL_INTERNAL_CHECK(tag, "tag must be non-null.");
const std::string* tag_str = new std::string(tag);
ABSL_INTERNAL_CHECK(
android_log_tag.exchange(tag_str->c_str(), std::memory_order_acq_rel) ==
kDefaultAndroidTag,
"SetAndroidNativeTag() must only be called once per process!");
user_log_tag.store(tag_str, std::memory_order_relaxed);
}
namespace log_internal {
bool ShouldLogBacktraceAt(absl::string_view file, int line) { bool ShouldLogBacktraceAt(absl::string_view file, int line) {
const size_t flag_hash = const size_t flag_hash =
log_backtrace_at_hash.load(std::memory_order_relaxed); log_backtrace_at_hash.load(std::memory_order_relaxed);
......
...@@ -152,6 +152,29 @@ ABSL_MUST_USE_RESULT bool ShouldPrependLogPrefix(); ...@@ -152,6 +152,29 @@ ABSL_MUST_USE_RESULT bool ShouldPrependLogPrefix();
// This function is async-signal-safe. // This function is async-signal-safe.
void EnableLogPrefix(bool on_off); void EnableLogPrefix(bool on_off);
//------------------------------------------------------------------------------
// Configure Android Native Log Tag
//------------------------------------------------------------------------------
//
// The logging library forwards to the Android system log API when built for
// Android. That API takes a string "tag" value in addition to a message and
// severity level. The tag is used to identify the source of messages and to
// filter them. This library uses the tag "native" by default.
// SetAndroidNativeTag()
//
// Stores a copy of the string pointed to by `tag` and uses it as the Android
// logging tag thereafter. `tag` must not be null.
// This function must not be called more than once!
void SetAndroidNativeTag(const char* tag);
namespace log_internal {
// GetAndroidNativeTag()
//
// Returns the configured Android logging tag.
const char* GetAndroidNativeTag();
} // namespace log_internal
namespace log_internal { namespace log_internal {
using LoggingGlobalsListener = void (*)(); using LoggingGlobalsListener = void (*)();
......
...@@ -15,8 +15,6 @@ ...@@ -15,8 +15,6 @@
#include "absl/log/globals.h" #include "absl/log/globals.h"
#include <string>
#include "gmock/gmock.h" #include "gmock/gmock.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "absl/base/attributes.h" #include "absl/base/attributes.h"
...@@ -27,6 +25,8 @@ ...@@ -27,6 +25,8 @@
#include "absl/log/scoped_mock_log.h" #include "absl/log/scoped_mock_log.h"
namespace { namespace {
using ::testing::_;
using ::testing::StrEq;
auto* test_env ABSL_ATTRIBUTE_UNUSED = ::testing::AddGlobalTestEnvironment( auto* test_env ABSL_ATTRIBUTE_UNUSED = ::testing::AddGlobalTestEnvironment(
new absl::log_internal::LogTestEnvironment); new absl::log_internal::LogTestEnvironment);
...@@ -88,4 +88,17 @@ TEST(TestGlobals, LogPrefix) { ...@@ -88,4 +88,17 @@ TEST(TestGlobals, LogPrefix) {
EXPECT_TRUE(absl::ShouldPrependLogPrefix()); EXPECT_TRUE(absl::ShouldPrependLogPrefix());
} }
TEST(TestGlobals, AndroidLogTag) {
// Verify invalid tags result in a check failure.
EXPECT_DEATH_IF_SUPPORTED(absl::SetAndroidNativeTag(nullptr), ".*");
// Verify valid tags applied.
EXPECT_THAT(absl::log_internal::GetAndroidNativeTag(), StrEq("native"));
absl::SetAndroidNativeTag("test_tag");
EXPECT_THAT(absl::log_internal::GetAndroidNativeTag(), StrEq("test_tag"));
// Verify that additional calls (more than 1) result in a check failure.
EXPECT_DEATH_IF_SUPPORTED(absl::SetAndroidNativeTag("test_tag_fail"), ".*");
}
} // namespace } // namespace
...@@ -122,11 +122,11 @@ class AndroidLogSink final : public LogSink { ...@@ -122,11 +122,11 @@ class AndroidLogSink final : public LogSink {
void Send(const absl::LogEntry& entry) override { void Send(const absl::LogEntry& entry) override {
const int level = AndroidLogLevel(entry); const int level = AndroidLogLevel(entry);
// TODO(b/37587197): make the tag ("native") configurable. const char* const tag = GetAndroidNativeTag();
__android_log_write(level, "native", __android_log_write(level, tag,
entry.text_message_with_prefix_and_newline_c_str()); entry.text_message_with_prefix_and_newline_c_str());
if (entry.log_severity() == absl::LogSeverity::kFatal) if (entry.log_severity() == absl::LogSeverity::kFatal)
__android_log_write(ANDROID_LOG_FATAL, "native", "terminating.\n"); __android_log_write(ANDROID_LOG_FATAL, tag, "terminating.\n");
} }
private: private:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment