summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorElliott Hughes2018-06-06 14:54:41 -0500
committerElliott Hughes2018-06-06 14:54:41 -0500
commit11a64eb0e8753a210c678a2e2ec32e95298b8ac2 (patch)
tree04019da3c0b3f79d360ff9bd27f125e1f0fe4007 /base
parentd580c441ab45a6b7278da003e19af64e9ea2cc71 (diff)
downloadplatform-system-core-11a64eb0e8753a210c678a2e2ec32e95298b8ac2.tar.gz
platform-system-core-11a64eb0e8753a210c678a2e2ec32e95298b8ac2.tar.xz
platform-system-core-11a64eb0e8753a210c678a2e2ec32e95298b8ac2.zip
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
Diffstat (limited to 'base')
-rw-r--r--base/logging.cpp51
1 files 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 @@
58#include <android-base/strings.h> 58#include <android-base/strings.h>
59#include <android-base/threads.h> 59#include <android-base/threads.h>
60 60
61namespace { 61namespace android {
62namespace base {
63
64// BSD-based systems like Android/macOS have getprogname(). Others need us to provide one.
65#if defined(__GLIBC__) || defined(_WIN32)
66static const char* getprogname() {
62#if defined(__GLIBC__) 67#if defined(__GLIBC__)
63const char* getprogname() {
64 return program_invocation_short_name; 68 return program_invocation_short_name;
65}
66#elif defined(_WIN32) 69#elif defined(_WIN32)
67const char* getprogname() {
68 static bool first = true; 70 static bool first = true;
69 static char progname[MAX_PATH] = {}; 71 static char progname[MAX_PATH] = {};
70 72
@@ -81,11 +83,28 @@ const char* getprogname() {
81 } 83 }
82 84
83 return progname; 85 return progname;
86#endif
84} 87}
85#endif 88#endif
86 89
90static const char* GetFileBasename(const char* file) {
91 // We can't use basename(3) even on Unix because the Mac doesn't
92 // have a non-modifying basename.
93 const char* last_slash = strrchr(file, '/');
94 if (last_slash != nullptr) {
95 return last_slash + 1;
96 }
97#if defined(_WIN32)
98 const char* last_backslash = strrchr(file, '\\');
99 if (last_backslash != nullptr) {
100 return last_backslash + 1;
101 }
102#endif
103 return file;
104}
105
87#if defined(__linux__) 106#if defined(__linux__)
88int OpenKmsg() { 107static int OpenKmsg() {
89#if defined(__ANDROID__) 108#if defined(__ANDROID__)
90 // pick up 'file w /dev/kmsg' environment from daemon's init rc file 109 // pick up 'file w /dev/kmsg' environment from daemon's init rc file
91 const auto val = getenv("ANDROID_FILE__dev_kmsg"); 110 const auto val = getenv("ANDROID_FILE__dev_kmsg");
@@ -100,10 +119,6 @@ int OpenKmsg() {
100 return TEMP_FAILURE_RETRY(open("/dev/kmsg", O_WRONLY | O_CLOEXEC)); 119 return TEMP_FAILURE_RETRY(open("/dev/kmsg", O_WRONLY | O_CLOEXEC));
101} 120}
102#endif 121#endif
103} // namespace
104
105namespace android {
106namespace base {
107 122
108static std::mutex& LoggingLock() { 123static std::mutex& LoggingLock() {
109 static auto& logging_lock = *new std::mutex(); 124 static auto& logging_lock = *new std::mutex();
@@ -216,7 +231,7 @@ void StdioLogger(LogId, LogSeverity severity, const char* /*tag*/, const char* /
216 unsigned int /*line*/, const char* message) { 231 unsigned int /*line*/, const char* message) {
217 if (severity >= WARNING) { 232 if (severity >= WARNING) {
218 fflush(stdout); 233 fflush(stdout);
219 fprintf(stderr, "%s: %s\n", getprogname(), message); 234 fprintf(stderr, "%s: %s\n", GetFileBasename(getprogname()), message);
220 } else { 235 } else {
221 fprintf(stdout, "%s\n", message); 236 fprintf(stdout, "%s\n", message);
222 } 237 }
@@ -336,22 +351,6 @@ void SetAborter(AbortFunction&& aborter) {
336 Aborter() = std::move(aborter); 351 Aborter() = std::move(aborter);
337} 352}
338 353
339static const char* GetFileBasename(const char* file) {
340 // We can't use basename(3) even on Unix because the Mac doesn't
341 // have a non-modifying basename.
342 const char* last_slash = strrchr(file, '/');
343 if (last_slash != nullptr) {
344 return last_slash + 1;
345 }
346#if defined(_WIN32)
347 const char* last_backslash = strrchr(file, '\\');
348 if (last_backslash != nullptr) {
349 return last_backslash + 1;
350 }
351#endif
352 return file;
353}
354
355// This indirection greatly reduces the stack impact of having lots of 354// This indirection greatly reduces the stack impact of having lots of
356// checks/logging in a function. 355// checks/logging in a function.
357class LogMessageData { 356class LogMessageData {