summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author beiyifb <beiyi@meta.com> 2023-12-19 16:35:02 -0800
committer beiyifb <beiyi@meta.com> 2024-01-02 15:28:23 -0800
commita9de3aa8e71373f6ec2d312b0662ded7664bee07 (patch)
treea65707e1242125567df2e7df868c0a625c23b1fd
parentb3acd62209a67b9ff839c75c08ab76fd5a8d95e1 (diff)
Flush output buffers when command is complete
The PrintWriters in ShellCommand are not autoflush, so they need to be manually flushed. Rather than put the onus on subclasses, this automatically flushes them when the command exits cleanly Test: Run a command that has an exception. See the exception get printed to the console after this change. Change-Id: I6f27a4d1183008191ee97071c2e238af7a2edd5f
-rw-r--r--core/java/com/android/internal/os/BaseCommand.java10
1 files changed, 9 insertions, 1 deletions
diff --git a/core/java/com/android/internal/os/BaseCommand.java b/core/java/com/android/internal/os/BaseCommand.java
index c85b5d7aa7a6..af763e4c5fa9 100644
--- a/core/java/com/android/internal/os/BaseCommand.java
+++ b/core/java/com/android/internal/os/BaseCommand.java
@@ -58,15 +58,23 @@ public abstract class BaseCommand {
mRawArgs = args;
mArgs.init(null, null, null, null, args, 0);
+ int status = 1;
try {
onRun();
+ status = 0;
} catch (IllegalArgumentException e) {
onShowUsage(System.err);
System.err.println();
System.err.println("Error: " + e.getMessage());
+ status = 0;
} catch (Exception e) {
e.printStackTrace(System.err);
- System.exit(1);
+ } finally {
+ System.out.flush();
+ System.err.flush();
+ }
+ if (status != 0) {
+ System.exit(status);
}
}