summaryrefslogtreecommitdiffstats
path: root/logcat
diff options
context:
space:
mode:
authorWei Wang <wvw@google.com>2018-09-05 11:05:57 -0700
committerWei Wang <wvw@google.com>2018-09-05 14:11:18 -0700
commitc27d48169529bb3fe534ba21c9badcd07ab015a0 (patch)
tree4a01408206d4b85a8e893eb5c803ef6dbc12eaae /logcat
parentf33d19ff211317017bf7be936b32dafebc286ef9 (diff)
downloadsystem_core-c27d48169529bb3fe534ba21c9badcd07ab015a0.tar.gz
system_core-c27d48169529bb3fe534ba21c9badcd07ab015a0.tar.bz2
system_core-c27d48169529bb3fe534ba21c9badcd07ab015a0.zip
logcat: fix print of logcat -g
The units should be byte instead of bit and multiplier are kibibyte mebibyte gibibyte respectively. Bug: 114062206 Test: Build and logcat -g Test: ./logcat-unit-tests Change-Id: I995faa9a340bb63f48d117a1b8590e5d74ac1bba
Diffstat (limited to 'logcat')
-rw-r--r--logcat/logcat.cpp28
-rw-r--r--logcat/tests/logcat_test.cpp34
2 files changed, 28 insertions, 34 deletions
diff --git a/logcat/logcat.cpp b/logcat/logcat.cpp
index 0f563371c..0f1badb15 100644
--- a/logcat/logcat.cpp
+++ b/logcat/logcat.cpp
@@ -41,6 +41,7 @@
#include <atomic>
#include <memory>
#include <string>
+#include <utility>
#include <vector>
#include <android-base/file.h>
@@ -565,23 +566,14 @@ static int setLogFormat(android_logcat_context_internal* context,
return android_log_setPrintFormat(context->logformat, format);
}
-static const char multipliers[][2] = { { "" }, { "K" }, { "M" }, { "G" } };
-
-static unsigned long value_of_size(unsigned long value) {
- for (unsigned i = 0;
- (i < sizeof(multipliers) / sizeof(multipliers[0])) && (value >= 1024);
- value /= 1024, ++i)
- ;
- return value;
-}
-
-static const char* multiplier_of_size(unsigned long value) {
- unsigned i;
+static std::pair<unsigned long, const char*> format_of_size(unsigned long value) {
+ static const char multipliers[][3] = {{""}, {"Ki"}, {"Mi"}, {"Gi"}};
+ size_t i;
for (i = 0;
(i < sizeof(multipliers) / sizeof(multipliers[0])) && (value >= 1024);
value /= 1024, ++i)
;
- return multipliers[i];
+ return std::make_pair(value, multipliers[i]);
}
// String to unsigned int, returns -1 if it fails
@@ -1472,12 +1464,14 @@ static int __logcat(android_logcat_context_internal* context) {
if ((size < 0) || (readable < 0)) {
reportErrorName(&getSizeFail, dev->device, allSelected);
} else {
+ auto size_format = format_of_size(size);
+ auto readable_format = format_of_size(readable);
std::string str = android::base::StringPrintf(
- "%s: ring buffer is %ld%sb (%ld%sb consumed),"
- " max entry is %db, max payload is %db\n",
+ "%s: ring buffer is %lu %sB (%lu %sB consumed),"
+ " max entry is %d B, max payload is %d B\n",
dev->device,
- value_of_size(size), multiplier_of_size(size),
- value_of_size(readable), multiplier_of_size(readable),
+ size_format.first, size_format.second,
+ readable_format.first, readable_format.second,
(int)LOGGER_ENTRY_MAX_LEN,
(int)LOGGER_ENTRY_MAX_PAYLOAD);
TEMP_FAILURE_RETRY(write(context->output_fd,
diff --git a/logcat/tests/logcat_test.cpp b/logcat/tests/logcat_test.cpp
index cc1632a83..c44e4416b 100644
--- a/logcat/tests/logcat_test.cpp
+++ b/logcat/tests/logcat_test.cpp
@@ -557,20 +557,17 @@ static int get_groups(const char* cmd) {
while (fgets(buffer, sizeof(buffer), fp)) {
int size, consumed, max, payload;
- char size_mult[3], consumed_mult[3];
+ char size_mult[4], consumed_mult[4];
long full_size, full_consumed;
size = consumed = max = payload = 0;
// NB: crash log can be very small, not hit a Kb of consumed space
// doubly lucky we are not including it.
- if (6 != sscanf(buffer,
- "%*s ring buffer is %d%2s (%d%2s consumed),"
- " max entry is %db, max payload is %db",
- &size, size_mult, &consumed, consumed_mult, &max,
- &payload)) {
- fprintf(stderr, "WARNING: Parse error: %s", buffer);
- continue;
- }
+ EXPECT_EQ(6, sscanf(buffer,
+ "%*s ring buffer is %d %3s (%d %3s consumed),"
+ " max entry is %d B, max payload is %d B",
+ &size, size_mult, &consumed, consumed_mult, &max, &payload))
+ << "Parse error on: " << buffer;
full_size = size;
switch (size_mult[0]) {
case 'G':
@@ -582,8 +579,10 @@ static int get_groups(const char* cmd) {
case 'K':
full_size *= 1024;
/* FALLTHRU */
- case 'b':
+ case 'B':
break;
+ default:
+ ADD_FAILURE() << "Parse error on multiplier: " << size_mult;
}
full_consumed = consumed;
switch (consumed_mult[0]) {
@@ -596,8 +595,10 @@ static int get_groups(const char* cmd) {
case 'K':
full_consumed *= 1024;
/* FALLTHRU */
- case 'b':
+ case 'B':
break;
+ default:
+ ADD_FAILURE() << "Parse error on multiplier: " << consumed_mult;
}
EXPECT_GT((full_size * 9) / 4, full_consumed);
EXPECT_GT(full_size, max);
@@ -1232,10 +1233,9 @@ TEST(logcat, blocking_clear) {
char size_mult[3], consumed_mult[3];
size = consumed = max = payload = 0;
if (6 == sscanf(buffer,
- "events: ring buffer is %d%2s (%d%2s consumed),"
- " max entry is %db, max payload is %db",
- &size, size_mult, &consumed, consumed_mult, &max,
- &payload)) {
+ "events: ring buffer is %d %3s (%d %3s consumed),"
+ " max entry is %d B, max payload is %d B",
+ &size, size_mult, &consumed, consumed_mult, &max, &payload)) {
long full_size = size, full_consumed = consumed;
switch (size_mult[0]) {
@@ -1248,7 +1248,7 @@ TEST(logcat, blocking_clear) {
case 'K':
full_size *= 1024;
/* FALLTHRU */
- case 'b':
+ case 'B':
break;
}
switch (consumed_mult[0]) {
@@ -1261,7 +1261,7 @@ TEST(logcat, blocking_clear) {
case 'K':
full_consumed *= 1024;
/* FALLTHRU */
- case 'b':
+ case 'B':
break;
}
EXPECT_GT(full_size, full_consumed);