From 1be0d1481b26281bf699238d5699b38a52b31382 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Wed, 23 May 2018 09:16:46 -0700 Subject: Add StdioLogger for command-line tools. Bug: N/A Test: ran tests Change-Id: If366a4ea25aea1becdd3e443eba225e9bd52ebba --- base/include/android-base/logging.h | 9 +++++++++ base/include/android-base/test_utils.h | 26 +++++++++++++++++++------- 2 files changed, 28 insertions(+), 7 deletions(-) (limited to 'base/include') diff --git a/base/include/android-base/logging.h b/base/include/android-base/logging.h index cc7aaf68c..05a12e71a 100644 --- a/base/include/android-base/logging.h +++ b/base/include/android-base/logging.h @@ -100,8 +100,17 @@ using LogFunction = std::function; using AbortFunction = std::function; +// Loggers for use with InitLogging/SetLogger. + +// Log to the kernel log (dmesg). void KernelLogger(LogId, LogSeverity, const char*, const char*, unsigned int, const char*); +// Log to stderr in the full logcat format (with pid/tid/time/tag details). void StderrLogger(LogId, LogSeverity, const char*, const char*, unsigned int, const char*); +// Log just the message to stdout/stderr (without pid/tid/time/tag details). +// The choice of stdout versus stderr is based on the severity. +// Errors are also prefixed by the program name (as with err(3)/error(3)). +// Useful for replacing printf(3)/perror(3)/err(3)/error(3) in command-line tools. +void StdioLogger(LogId, LogSeverity, const char*, const char*, unsigned int, const char*); void DefaultAborter(const char* abort_message); diff --git a/base/include/android-base/test_utils.h b/base/include/android-base/test_utils.h index b95fa07ce..b29676f7c 100644 --- a/base/include/android-base/test_utils.h +++ b/base/include/android-base/test_utils.h @@ -58,21 +58,33 @@ class TemporaryDir { DISALLOW_COPY_AND_ASSIGN(TemporaryDir); }; -class CapturedStderr { +class CapturedStdFd { public: - CapturedStderr(); - ~CapturedStderr(); + CapturedStdFd(int std_fd); + ~CapturedStdFd(); int fd() const; + std::string str(); private: - void init(); - void reset(); + void Init(); + void Reset(); TemporaryFile temp_file_; - int old_stderr_; + int std_fd_; + int old_fd_; - DISALLOW_COPY_AND_ASSIGN(CapturedStderr); + DISALLOW_COPY_AND_ASSIGN(CapturedStdFd); +}; + +class CapturedStderr : public CapturedStdFd { + public: + CapturedStderr() : CapturedStdFd(STDERR_FILENO) {} +}; + +class CapturedStdout : public CapturedStdFd { + public: + CapturedStdout() : CapturedStdFd(STDOUT_FILENO) {} }; #define ASSERT_MATCH(str, pattern) \ -- cgit v1.2.3-54-g00ecf