From 09faa660064b61172f251ffb4db0b3adaa294ad7 Mon Sep 17 00:00:00 2001 From: bohu Date: Fri, 26 Oct 2018 07:11:55 -0700 Subject: [PATCH] adb: print emulator console output It is very helpful to see the output of the following adb command: adb emu Change-Id: I7905370fb955de4d457a323a7e9e780aaca5d45e --- adb/client/console.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/adb/client/console.cpp b/adb/client/console.cpp index 9563eacb3..4e8a3f8cc 100644 --- a/adb/client/console.cpp +++ b/adb/client/console.cpp @@ -135,6 +135,7 @@ int adb_send_emulator_command(int argc, const char** argv, const char* serial) { // preventing the emulator from reading the command that adb has sent. // https://code.google.com/p/android/issues/detail?id=21021 int result; + std::string emulator_output; do { char buf[BUFSIZ]; result = adb_read(fd, buf, sizeof(buf)); @@ -146,8 +147,37 @@ int adb_send_emulator_command(int argc, const char** argv, const char* serial) { // appended above, and that causes the emulator to close the socket // which should cause zero bytes (orderly/graceful shutdown) to be // returned. + if (result > 0) emulator_output.append(buf, result); } while (result > 0); + // Note: the following messages are expected to be quite stable from emulator. + // + // Emulator console will send the following message upon connection: + // + // Android Console: Authentication required + // Android Console: type 'auth ' to authenticate + // Android Console: you can find your in + // '//.emulator_console_auth_token' + // OK\r\n + // + // and the following after authentication: + // Android Console: type 'help' for a list of commands + // OK\r\n + // + // So try search and skip first two "OK\r\n", print the rest. + // + const std::string delims = "OK\r\n"; + size_t found = 0; + for (int i = 0; i < 2; ++i) { + const size_t result = emulator_output.find(delims, found); + if (result == std::string::npos) { + break; + } else { + found = result + delims.size(); + } + } + + printf("%s", emulator_output.c_str() + found); adb_close(fd); return 0;