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
This commit is contained in:
Elliott Hughes 2018-06-06 12:54:41 -07:00
parent d580c441ab
commit 11a64eb0e8
1 changed files with 25 additions and 26 deletions

View File

@ -58,13 +58,15 @@
#include <android-base/strings.h>
#include <android-base/threads.h>
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 {