Skip to content

Commit 1f28b77

Browse files
tpruvotJean-Baptiste Queru
authored and
Jean-Baptiste Queru
committed
logwrapper: reduce verbosity and fix usage
- no info on normal process exit (0) - basename as tag, not the full path - fix usage, bad "-x" parameter vs "-d" in code Change-Id: Ife72729eaee2a366bd4226f3fbe3ba074219c974
1 parent 1ea9fce commit 1f28b77

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

logwrapper/logwrapper.c

+11-6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <unistd.h>
2323
#include <errno.h>
2424
#include <fcntl.h>
25+
#include <libgen.h>
2526

2627
#include "private/android_filesystem_config.h"
2728
#include "cutils/log.h"
@@ -34,13 +35,13 @@ void fatal(const char *msg) {
3435

3536
void usage() {
3637
fatal(
37-
"Usage: logwrapper [-x] BINARY [ARGS ...]\n"
38+
"Usage: logwrapper [-d] BINARY [ARGS ...]\n"
3839
"\n"
3940
"Forks and executes BINARY ARGS, redirecting stdout and stderr to\n"
4041
"the Android logging system. Tag is set to BINARY, priority is\n"
4142
"always LOG_INFO.\n"
4243
"\n"
43-
"-x: Causes logwrapper to SIGSEGV when BINARY terminates\n"
44+
"-d: Causes logwrapper to SIGSEGV when BINARY terminates\n"
4445
" fault address is set to the status of wait()\n");
4546
}
4647

@@ -51,6 +52,10 @@ void parent(const char *tag, int seg_fault_on_exit, int parent_read) {
5152
int a = 0; // start index of unprocessed data
5253
int b = 0; // end index of unprocessed data
5354
int sz;
55+
56+
char *btag = basename(tag);
57+
if (!btag) btag = (char*) tag;
58+
5459
while ((sz = read(parent_read, &buffer[b], sizeof(buffer) - 1 - b)) > 0) {
5560

5661
sz += b;
@@ -60,15 +65,15 @@ void parent(const char *tag, int seg_fault_on_exit, int parent_read) {
6065
buffer[b] = '\0';
6166
} else if (buffer[b] == '\n') {
6267
buffer[b] = '\0';
63-
ALOG(LOG_INFO, tag, "%s", &buffer[a]);
68+
ALOG(LOG_INFO, btag, "%s", &buffer[a]);
6469
a = b + 1;
6570
}
6671
}
6772

6873
if (a == 0 && b == sizeof(buffer) - 1) {
6974
// buffer is full, flush
7075
buffer[b] = '\0';
71-
ALOG(LOG_INFO, tag, "%s", &buffer[a]);
76+
ALOG(LOG_INFO, btag, "%s", &buffer[a]);
7277
b = 0;
7378
} else if (a != b) {
7479
// Keep left-overs
@@ -84,11 +89,11 @@ void parent(const char *tag, int seg_fault_on_exit, int parent_read) {
8489
// Flush remaining data
8590
if (a != b) {
8691
buffer[b] = '\0';
87-
ALOG(LOG_INFO, tag, "%s", &buffer[a]);
92+
ALOG(LOG_INFO, btag, "%s", &buffer[a]);
8893
}
8994
status = 0xAAAA;
9095
if (wait(&status) != -1) { // Wait for child
91-
if (WIFEXITED(status))
96+
if (WIFEXITED(status) && WEXITSTATUS(status))
9297
ALOG(LOG_INFO, "logwrapper", "%s terminated by exit(%d)", tag,
9398
WEXITSTATUS(status));
9499
else if (WIFSIGNALED(status))

0 commit comments

Comments
 (0)