diff options
author | Dan Albert <danalbert@google.com> | 2015-04-06 17:58:41 +0000 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2015-04-06 17:58:41 +0000 |
commit | 131bef55d139eae5e88d9334d0f8848b6cd97dd2 (patch) | |
tree | 562d346f1f5fcd3d735cb9106614f97c7eec9ec8 | |
parent | 08d719d25e855d34703165a4e41548750f518162 (diff) | |
parent | a47d32c67209d43ad3ceac91dfcb4d4a15ec6185 (diff) | |
download | system_core-131bef55d139eae5e88d9334d0f8848b6cd97dd2.zip system_core-131bef55d139eae5e88d9334d0f8848b6cd97dd2.tar.gz system_core-131bef55d139eae5e88d9334d0f8848b6cd97dd2.tar.bz2 |
am a47d32c6: am 68c384fd: am d1e90c01: Merge "Cleanup base/logging."
* commit 'a47d32c67209d43ad3ceac91dfcb4d4a15ec6185':
Cleanup base/logging.
-rw-r--r-- | base/include/base/logging.h | 12 | ||||
-rw-r--r-- | base/logging.cpp | 54 |
2 files changed, 26 insertions, 40 deletions
diff --git a/base/include/base/logging.h b/base/include/base/logging.h index 19c1b64..230adb8 100644 --- a/base/include/base/logging.h +++ b/base/include/base/logging.h @@ -79,18 +79,6 @@ extern void InitLogging(char* argv[]); // Replace the current logger. extern void SetLogger(LogFunction&& logger); -// Returns the command line used to invoke the current tool or nullptr if -// InitLogging hasn't been performed. -extern const char* GetCmdLine(); - -// The command used to start the program, such as "/system/bin/dalvikvm". If -// InitLogging hasn't been performed then just returns "unknown" -extern const char* ProgramInvocationName(); - -// A short version of the command used to start the program, such as "dalvikvm". -// If InitLogging hasn't been performed then just returns "unknown" -extern const char* ProgramInvocationShortName(); - // Logs a message to logcat on Android otherwise to stderr. If the severity is // FATAL it also causes an abort. For example: // diff --git a/base/logging.cpp b/base/logging.cpp index a36ac5f..0142b70 100644 --- a/base/logging.cpp +++ b/base/logging.cpp @@ -16,6 +16,15 @@ #include "base/logging.h" +#include <libgen.h> + +// For getprogname(3) or program_invocation_short_name. +#if defined(__ANDROID__) || defined(__APPLE__) +#include <stdlib.h> +#elif defined(__GLIBC__) +#include <errno.h> +#endif + #include <iostream> #include <limits> #include <mutex> @@ -47,25 +56,22 @@ static LogFunction gLogger = LogdLogger(); static LogFunction gLogger = StderrLogger; #endif +static bool gInitialized = false; static LogSeverity gMinimumLogSeverity = INFO; -static std::unique_ptr<std::string> gCmdLine; static std::unique_ptr<std::string> gProgramInvocationName; -static std::unique_ptr<std::string> gProgramInvocationShortName; -const char* GetCmdLine() { - return (gCmdLine.get() != nullptr) ? gCmdLine->c_str() : nullptr; +#if defined(__GLIBC__) +static const char* getprogname() { + return program_invocation_short_name; } +#endif -const char* ProgramInvocationName() { - return (gProgramInvocationName.get() != nullptr) - ? gProgramInvocationName->c_str() - : "unknown"; -} +static const char* ProgramInvocationName() { + if (gProgramInvocationName == nullptr) { + gProgramInvocationName.reset(new std::string(getprogname())); + } -const char* ProgramInvocationShortName() { - return (gProgramInvocationShortName.get() != nullptr) - ? gProgramInvocationShortName->c_str() - : "unknown"; + return gProgramInvocationName->c_str(); } void StderrLogger(LogId, LogSeverity severity, const char*, const char* file, @@ -73,7 +79,7 @@ void StderrLogger(LogId, LogSeverity severity, const char*, const char* file, static const char* log_characters = "VDIWEF"; CHECK_EQ(strlen(log_characters), FATAL + 1U); char severity_char = log_characters[severity]; - fprintf(stderr, "%s %c %5d %5d %s:%u] %s\n", ProgramInvocationShortName(), + fprintf(stderr, "%s %c %5d %5d %s:%u] %s\n", ProgramInvocationName(), severity_char, getpid(), gettid(), file, line, message); } @@ -121,27 +127,19 @@ void InitLogging(char* argv[], LogFunction&& logger) { } void InitLogging(char* argv[]) { - if (gCmdLine.get() != nullptr) { + if (gInitialized) { return; } + gInitialized = true; + // Stash the command line for later use. We can use /proc/self/cmdline on // Linux to recover this, but we don't have that luxury on the Mac, and there // are a couple of argv[0] variants that are commonly used. if (argv != nullptr) { - gCmdLine.reset(new std::string(argv[0])); - for (size_t i = 1; argv[i] != nullptr; ++i) { - gCmdLine->append(" "); - gCmdLine->append(argv[i]); - } - gProgramInvocationName.reset(new std::string(argv[0])); - const char* last_slash = strrchr(argv[0], '/'); - gProgramInvocationShortName.reset( - new std::string((last_slash != nullptr) ? last_slash + 1 : argv[0])); - } else { - // TODO: fall back to /proc/self/cmdline when argv is NULL on Linux. - gCmdLine.reset(new std::string("<unset>")); + gProgramInvocationName.reset(new std::string(basename(argv[0]))); } + const char* tags = getenv("ANDROID_LOG_TAGS"); if (tags == nullptr) { return; @@ -288,7 +286,7 @@ std::ostream& LogMessage::stream() { void LogMessage::LogLine(const char* file, unsigned int line, LogId id, LogSeverity severity, const char* message) { - const char* tag = ProgramInvocationShortName(); + const char* tag = ProgramInvocationName(); std::lock_guard<std::mutex> lock(logging_lock); gLogger(id, severity, tag, file, line, message); } |