From 11a64eb0e8753a210c678a2e2ec32e95298b8ac2 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Wed, 6 Jun 2018 12:54:41 -0700 Subject: [PATCH] libbase: fix libbase_test.logging.StdioLogger. I don't think the StdioLogger test ever passed. Move GetFileBasename to where we can use it from StdioLogger, and undo the mix of anonymous namespace vs static to consistently use static (which is the majority) while we're touching those lines. Bug: N/A Test: ran tests Change-Id: I95b3966cdb8af642bed71752bd7d4e3a86ac84ca --- base/logging.cpp | 51 ++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/base/logging.cpp b/base/logging.cpp index 978d56d07..35054ac82 100644 --- a/base/logging.cpp +++ b/base/logging.cpp @@ -58,13 +58,15 @@ #include #include -namespace { +namespace android { +namespace base { + +// BSD-based systems like Android/macOS have getprogname(). Others need us to provide one. +#if defined(__GLIBC__) || defined(_WIN32) +static const char* getprogname() { #if defined(__GLIBC__) -const char* getprogname() { return program_invocation_short_name; -} #elif defined(_WIN32) -const char* getprogname() { static bool first = true; static char progname[MAX_PATH] = {}; @@ -81,11 +83,28 @@ const char* getprogname() { } return progname; +#endif } #endif +static const char* GetFileBasename(const char* file) { + // We can't use basename(3) even on Unix because the Mac doesn't + // have a non-modifying basename. + const char* last_slash = strrchr(file, '/'); + if (last_slash != nullptr) { + return last_slash + 1; + } +#if defined(_WIN32) + const char* last_backslash = strrchr(file, '\\'); + if (last_backslash != nullptr) { + return last_backslash + 1; + } +#endif + return file; +} + #if defined(__linux__) -int OpenKmsg() { +static int OpenKmsg() { #if defined(__ANDROID__) // pick up 'file w /dev/kmsg' environment from daemon's init rc file const auto val = getenv("ANDROID_FILE__dev_kmsg"); @@ -100,10 +119,6 @@ int OpenKmsg() { return TEMP_FAILURE_RETRY(open("/dev/kmsg", O_WRONLY | O_CLOEXEC)); } #endif -} // namespace - -namespace android { -namespace base { static std::mutex& LoggingLock() { static auto& logging_lock = *new std::mutex(); @@ -216,7 +231,7 @@ void StdioLogger(LogId, LogSeverity severity, const char* /*tag*/, const char* / unsigned int /*line*/, const char* message) { if (severity >= WARNING) { fflush(stdout); - fprintf(stderr, "%s: %s\n", getprogname(), message); + fprintf(stderr, "%s: %s\n", GetFileBasename(getprogname()), message); } else { fprintf(stdout, "%s\n", message); } @@ -336,22 +351,6 @@ void SetAborter(AbortFunction&& aborter) { Aborter() = std::move(aborter); } -static const char* GetFileBasename(const char* file) { - // We can't use basename(3) even on Unix because the Mac doesn't - // have a non-modifying basename. - const char* last_slash = strrchr(file, '/'); - if (last_slash != nullptr) { - return last_slash + 1; - } -#if defined(_WIN32) - const char* last_backslash = strrchr(file, '\\'); - if (last_backslash != nullptr) { - return last_backslash + 1; - } -#endif - return file; -} - // This indirection greatly reduces the stack impact of having lots of // checks/logging in a function. class LogMessageData {