Skip to content

Commit 8e94c25

Browse files
Robert GreenwaltAndroid (Google) Code Review
authored andcommitted
Merge "Record the trimmed cmd string for logs"
2 parents 1c1d1e7 + d192598 commit 8e94c25

File tree

1 file changed

+31
-40
lines changed

1 file changed

+31
-40
lines changed

services/java/com/android/server/NativeDaemonConnector.java

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -200,17 +200,15 @@ private void listenToSocket() throws IOException {
200200

201201
/**
202202
* Make command for daemon, escaping arguments as needed.
203-
*
204-
* @return the final command.
205203
*/
206-
private StringBuilder makeCommand(String cmd, Object... args)
204+
private void makeCommand(StringBuilder builder, String cmd, Object... args)
207205
throws NativeDaemonConnectorException {
208206
// TODO: eventually enforce that cmd doesn't contain arguments
209207
if (cmd.indexOf('\0') >= 0) {
210208
throw new IllegalArgumentException("unexpected command: " + cmd);
211209
}
212210

213-
final StringBuilder builder = new StringBuilder(cmd);
211+
builder.append(cmd);
214212
for (Object arg : args) {
215213
final String argString = String.valueOf(arg);
216214
if (argString.indexOf('\0') >= 0) {
@@ -220,34 +218,6 @@ private StringBuilder makeCommand(String cmd, Object... args)
220218
builder.append(' ');
221219
appendEscaped(builder, argString);
222220
}
223-
224-
return builder;
225-
}
226-
227-
private int sendCommand(StringBuilder builder)
228-
throws NativeDaemonConnectorException {
229-
230-
int sequenceNumber = mSequenceNumber.incrementAndGet();
231-
232-
builder.insert(0, Integer.toString(sequenceNumber) + " ");
233-
234-
log("SND -> {" + builder.toString() + "}");
235-
236-
builder.append('\0');
237-
238-
synchronized (mDaemonLock) {
239-
if (mOutputStream == null) {
240-
throw new NativeDaemonConnectorException("missing output stream");
241-
} else {
242-
try {
243-
mOutputStream.write(builder.toString().getBytes(Charsets.UTF_8));
244-
} catch (IOException e) {
245-
throw new NativeDaemonConnectorException("problem sending command", e);
246-
}
247-
}
248-
}
249-
250-
return sequenceNumber;
251221
}
252222

253223
/**
@@ -325,25 +295,46 @@ public NativeDaemonEvent[] executeForList(String cmd, Object... args)
325295
public NativeDaemonEvent[] execute(int timeout, String cmd, Object... args)
326296
throws NativeDaemonConnectorException {
327297
final ArrayList<NativeDaemonEvent> events = Lists.newArrayList();
328-
final StringBuilder sentCommand = makeCommand(cmd, args);
329-
final int cmdNumber = sendCommand(sentCommand);
298+
299+
final int sequenceNumber = mSequenceNumber.incrementAndGet();
300+
final StringBuilder cmdBuilder =
301+
new StringBuilder(Integer.toString(sequenceNumber)).append(' ');
302+
303+
makeCommand(cmdBuilder, cmd, args);
304+
305+
final String logCmd = cmdBuilder.toString(); /* includes cmdNum, cmd, args */
306+
log("SND -> {" + logCmd + "}");
307+
308+
cmdBuilder.append('\0');
309+
final String sentCmd = cmdBuilder.toString(); /* logCmd + \0 */
310+
311+
synchronized (mDaemonLock) {
312+
if (mOutputStream == null) {
313+
throw new NativeDaemonConnectorException("missing output stream");
314+
} else {
315+
try {
316+
mOutputStream.write(sentCmd.getBytes(Charsets.UTF_8));
317+
} catch (IOException e) {
318+
throw new NativeDaemonConnectorException("problem sending command", e);
319+
}
320+
}
321+
}
330322

331323
NativeDaemonEvent event = null;
332-
cmd = sentCommand.toString();
333324
do {
334-
event = mResponseQueue.remove(cmdNumber, timeout, cmd);
325+
event = mResponseQueue.remove(sequenceNumber, timeout, sentCmd);
335326
if (event == null) {
336-
loge("timed-out waiting for response to " + cmdNumber + " " + cmd);
337-
throw new NativeDaemonFailureException(cmd, event);
327+
loge("timed-out waiting for response to " + logCmd);
328+
throw new NativeDaemonFailureException(logCmd, event);
338329
}
339330
events.add(event);
340331
} while (event.isClassContinue());
341332

342333
if (event.isClassClientError()) {
343-
throw new NativeDaemonArgumentException(cmd, event);
334+
throw new NativeDaemonArgumentException(logCmd, event);
344335
}
345336
if (event.isClassServerError()) {
346-
throw new NativeDaemonFailureException(cmd, event);
337+
throw new NativeDaemonFailureException(logCmd, event);
347338
}
348339

349340
return events.toArray(new NativeDaemonEvent[events.size()]);

0 commit comments

Comments
 (0)