Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 11a64eb0 authored by Elliott Hughes's avatar Elliott Hughes
Browse files

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
parent d580c441
Loading
Loading
Loading
Loading
+25 −26
Original line number Original line Diff line number Diff line
@@ -58,13 +58,15 @@
#include <android-base/strings.h>
#include <android-base/strings.h>
#include <android-base/threads.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__)
#if defined(__GLIBC__)
const char* getprogname() {
  return program_invocation_short_name;
  return program_invocation_short_name;
}
#elif defined(_WIN32)
#elif defined(_WIN32)
const char* getprogname() {
  static bool first = true;
  static bool first = true;
  static char progname[MAX_PATH] = {};
  static char progname[MAX_PATH] = {};


@@ -81,11 +83,28 @@ const char* getprogname() {
  }
  }


  return progname;
  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
#endif
  return file;
}


#if defined(__linux__)
#if defined(__linux__)
int OpenKmsg() {
static int OpenKmsg() {
#if defined(__ANDROID__)
#if defined(__ANDROID__)
  // pick up 'file w /dev/kmsg' environment from daemon's init rc file
  // pick up 'file w /dev/kmsg' environment from daemon's init rc file
  const auto val = getenv("ANDROID_FILE__dev_kmsg");
  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));
  return TEMP_FAILURE_RETRY(open("/dev/kmsg", O_WRONLY | O_CLOEXEC));
}
}
#endif
#endif
} // namespace

namespace android {
namespace base {


static std::mutex& LoggingLock() {
static std::mutex& LoggingLock() {
  static auto& logging_lock = *new std::mutex();
  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) {
                 unsigned int /*line*/, const char* message) {
  if (severity >= WARNING) {
  if (severity >= WARNING) {
    fflush(stdout);
    fflush(stdout);
    fprintf(stderr, "%s: %s\n", getprogname(), message);
    fprintf(stderr, "%s: %s\n", GetFileBasename(getprogname()), message);
  } else {
  } else {
    fprintf(stdout, "%s\n", message);
    fprintf(stdout, "%s\n", message);
  }
  }
@@ -336,22 +351,6 @@ void SetAborter(AbortFunction&& aborter) {
  Aborter() = std::move(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
// This indirection greatly reduces the stack impact of having lots of
// checks/logging in a function.
// checks/logging in a function.
class LogMessageData {
class LogMessageData {