summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorElliott Hughes2018-05-23 11:16:46 -0500
committerElliott Hughes2018-05-23 12:06:20 -0500
commit1be0d1481b26281bf699238d5699b38a52b31382 (patch)
treeabe0ba22d28d621564481ef56040e27534605a11 /base/include
parent9f6f8bf0dcb7f08c8c0afc1685f5d1a54b41b4cb (diff)
downloadplatform-system-core-1be0d1481b26281bf699238d5699b38a52b31382.tar.gz
platform-system-core-1be0d1481b26281bf699238d5699b38a52b31382.tar.xz
platform-system-core-1be0d1481b26281bf699238d5699b38a52b31382.zip
Add StdioLogger for command-line tools.
Bug: N/A Test: ran tests Change-Id: If366a4ea25aea1becdd3e443eba225e9bd52ebba
Diffstat (limited to 'base/include')
-rw-r--r--base/include/android-base/logging.h9
-rw-r--r--base/include/android-base/test_utils.h26
2 files changed, 28 insertions, 7 deletions
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<void(LogId, LogSeverity, const char*, const ch
100 unsigned int, const char*)>; 100 unsigned int, const char*)>;
101using AbortFunction = std::function<void(const char*)>; 101using AbortFunction = std::function<void(const char*)>;
102 102
103// Loggers for use with InitLogging/SetLogger.
104
105// Log to the kernel log (dmesg).
103void KernelLogger(LogId, LogSeverity, const char*, const char*, unsigned int, const char*); 106void KernelLogger(LogId, LogSeverity, const char*, const char*, unsigned int, const char*);
107// Log to stderr in the full logcat format (with pid/tid/time/tag details).
104void StderrLogger(LogId, LogSeverity, const char*, const char*, unsigned int, const char*); 108void StderrLogger(LogId, LogSeverity, const char*, const char*, unsigned int, const char*);
109// Log just the message to stdout/stderr (without pid/tid/time/tag details).
110// The choice of stdout versus stderr is based on the severity.
111// Errors are also prefixed by the program name (as with err(3)/error(3)).
112// Useful for replacing printf(3)/perror(3)/err(3)/error(3) in command-line tools.
113void StdioLogger(LogId, LogSeverity, const char*, const char*, unsigned int, const char*);
105 114
106void DefaultAborter(const char* abort_message); 115void DefaultAborter(const char* abort_message);
107 116
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 {
58 DISALLOW_COPY_AND_ASSIGN(TemporaryDir); 58 DISALLOW_COPY_AND_ASSIGN(TemporaryDir);
59}; 59};
60 60
61class CapturedStderr { 61class CapturedStdFd {
62 public: 62 public:
63 CapturedStderr(); 63 CapturedStdFd(int std_fd);
64 ~CapturedStderr(); 64 ~CapturedStdFd();
65 65
66 int fd() const; 66 int fd() const;
67 std::string str();
67 68
68 private: 69 private:
69 void init(); 70 void Init();
70 void reset(); 71 void Reset();
71 72
72 TemporaryFile temp_file_; 73 TemporaryFile temp_file_;
73 int old_stderr_; 74 int std_fd_;
75 int old_fd_;
74 76
75 DISALLOW_COPY_AND_ASSIGN(CapturedStderr); 77 DISALLOW_COPY_AND_ASSIGN(CapturedStdFd);
78};
79
80class CapturedStderr : public CapturedStdFd {
81 public:
82 CapturedStderr() : CapturedStdFd(STDERR_FILENO) {}
83};
84
85class CapturedStdout : public CapturedStdFd {
86 public:
87 CapturedStdout() : CapturedStdFd(STDOUT_FILENO) {}
76}; 88};
77 89
78#define ASSERT_MATCH(str, pattern) \ 90#define ASSERT_MATCH(str, pattern) \