From 0f7732d70810cd66cd2c69a40311785d67044c2c Mon Sep 17 00:00:00 2001 From: Casey Dahlin Date: Thu, 17 Mar 2016 16:18:55 -0700 Subject: [PATCH] logcat: Add --regex option (cherry pick from commit dc42a8734e5394eef022984de528718e195a0e8e) You can now filter log output by a regex on the messages. Test: New unit test passes Bug: 27706081 Change-Id: Idfa637f62a25fb1b92e9b49b89572dff0fec6e08 --- logcat/Android.mk | 2 +- logcat/logcat.cpp | 30 ++++++++++++++++++++++++++++-- logcat/tests/logcat_test.cpp | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 3 deletions(-) diff --git a/logcat/Android.mk b/logcat/Android.mk index b828a9fc0..1dacbe1ad 100644 --- a/logcat/Android.mk +++ b/logcat/Android.mk @@ -5,7 +5,7 @@ include $(CLEAR_VARS) LOCAL_SRC_FILES:= logcat.cpp event.logtags -LOCAL_SHARED_LIBRARIES := liblog libbase libcutils +LOCAL_SHARED_LIBRARIES := liblog libbase libcutils libpcrecpp LOCAL_MODULE := logcat diff --git a/logcat/logcat.cpp b/logcat/logcat.cpp index ddc91caae..d1dab229f 100644 --- a/logcat/logcat.cpp +++ b/logcat/logcat.cpp @@ -37,6 +37,8 @@ #include #include +#include + #define DEFAULT_MAX_ROTATED_LOGS 4 static AndroidLogFormat * g_logformat; @@ -76,6 +78,7 @@ static int g_outFD = -1; static size_t g_outByteCount = 0; static int g_printBinary = 0; static int g_devCount = 0; // >1 means multiple +static pcrecpp::RE* g_regex; __noreturn static void logcat_panic(bool showHelp, const char *fmt, ...) __printflike(2,3); @@ -143,6 +146,21 @@ void printBinary(struct log_msg *buf) TEMP_FAILURE_RETRY(write(g_outFD, buf, size)); } +static bool regexOk(const AndroidLogEntry& entry, log_id_t id) +{ + if (! g_regex) { + return true; + } + + if (id == LOG_ID_EVENTS || id == LOG_ID_SECURITY) { + return false; + } + + std::string messageString(entry.message, entry.messageLen); + + return g_regex->PartialMatch(messageString); +} + static void processBuffer(log_device_t* dev, struct log_msg *buf) { int bytesWritten = 0; @@ -171,7 +189,8 @@ static void processBuffer(log_device_t* dev, struct log_msg *buf) goto error; } - if (android_log_shouldPrintLine(g_logformat, entry.tag, entry.priority)) { + if (android_log_shouldPrintLine(g_logformat, entry.tag, entry.priority) && + regexOk(entry, buf->id())) { bytesWritten = android_log_printLogLine(g_logformat, g_outFD, &entry); if (bytesWritten < 0) { @@ -271,6 +290,8 @@ static void show_help(const char *cmd) " -c clear (flush) the entire log and exit\n" " --clear\n" " -d dump the log and then exit (don't block)\n" + " -e only print lines where the log message matches \n" + " --regex where is a regular expression\n" " -t print only the most recent lines (implies -d)\n" " -t '