Use JsonWriter to convert to JSON

Before the change the output was JSON-like but not valid because it had commas
after objects/arrays.

PiperOrigin-RevId: 585929345
This commit is contained in:
andrewlewis 2023-11-28 04:37:22 -08:00 committed by Copybara-Service
parent b598c96c2f
commit 7bbf72c202

View File

@ -20,6 +20,7 @@ import static androidx.media3.common.util.Assertions.checkNotNull;
import static androidx.media3.common.util.Util.formatInvariant; import static androidx.media3.common.util.Util.formatInvariant;
import static java.lang.annotation.ElementType.TYPE_USE; import static java.lang.annotation.ElementType.TYPE_USE;
import android.util.JsonWriter;
import androidx.annotation.GuardedBy; import androidx.annotation.GuardedBy;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.StringDef; import androidx.annotation.StringDef;
@ -30,6 +31,7 @@ import androidx.media3.common.util.Util;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import java.io.IOException; import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer; import java.io.Writer;
import java.lang.annotation.Documented; import java.lang.annotation.Documented;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
@ -203,26 +205,32 @@ public final class DebugTraceUtil {
/** /**
* Generate a summary of the logged events, containing the total number of times an event happened * Generate a summary of the logged events, containing the total number of times an event happened
* and the detailed log on the first and last {@link #MAX_FIRST_LAST_LOGS} times. * and the detailed log of a window of the oldest and newest events.
*/ */
public static synchronized String generateTraceSummary() { public static synchronized String generateTraceSummary() {
if (!enableTracing) { if (!enableTracing) {
return "Tracing disabled"; return "\"Tracing disabled\"";
} }
StringBuilder stringBuilder = new StringBuilder().append('{'); StringWriter stringWriter = new StringWriter();
JsonWriter jsonWriter = new JsonWriter(stringWriter);
try {
jsonWriter.beginObject();
for (int i = 0; i < EVENT_TYPES.size(); i++) { for (int i = 0; i < EVENT_TYPES.size(); i++) {
String eventType = EVENT_TYPES.get(i); String eventType = EVENT_TYPES.get(i);
jsonWriter.name(eventType);
if (!events.containsKey(eventType)) { if (!events.containsKey(eventType)) {
stringBuilder.append(formatInvariant("\"%s\": \"No events logged\",", eventType)); jsonWriter.value("No events");
continue; } else {
checkNotNull(events.get(eventType)).toJson(jsonWriter);
} }
stringBuilder
.append(formatInvariant("\"%s\":{", eventType))
.append(checkNotNull(events.get(eventType)))
.append("},");
} }
stringBuilder.append('}'); jsonWriter.endObject();
return stringBuilder.toString(); return stringWriter.toString();
} catch (IOException e) {
return "\"Error generating trace summary\"";
} finally {
Util.closeQuietly(jsonWriter);
}
} }
/** Dumps all the logged events to a tsv file. */ /** Dumps all the logged events to a tsv file. */
@ -270,13 +278,8 @@ public final class DebugTraceUtil {
@Override @Override
public String toString() { public String toString() {
StringBuilder stringBuilder = new StringBuilder(); return formatInvariant("%s@%d", presentationTimeToString(presentationTimeUs), eventTimeMs)
stringBuilder.append( + (extra != null ? formatInvariant("(%s)", extra) : "");
formatInvariant("\"%s@%d", presentationTimeToString(presentationTimeUs), eventTimeMs));
if (extra != null) {
stringBuilder.append(formatInvariant("(%s)", extra));
}
return stringBuilder.append('"').toString();
} }
} }
@ -307,24 +310,16 @@ public final class DebugTraceUtil {
return new ImmutableList.Builder<EventLog>().addAll(firstLogs).addAll(lastLogs).build(); return new ImmutableList.Builder<EventLog>().addAll(firstLogs).addAll(lastLogs).build();
} }
@Override public void toJson(JsonWriter jsonWriter) throws IOException {
public String toString() { jsonWriter.beginObject().name("count").value(totalCount).name("first").beginArray();
StringBuilder stringBuilder = for (EventLog eventLog : firstLogs) {
new StringBuilder().append("\"Count\": ").append(totalCount).append(", \"first\":["); jsonWriter.value(eventLog.toString());
for (int i = 0; i < firstLogs.size(); i++) {
stringBuilder.append(firstLogs.get(i)).append(",");
} }
stringBuilder.append("],"); jsonWriter.endArray().name("last").beginArray();
if (lastLogs.isEmpty()) { for (EventLog eventLog : lastLogs) {
return stringBuilder.toString(); jsonWriter.value(eventLog.toString());
} }
ImmutableList<EventLog> lastLogsList = ImmutableList.copyOf(lastLogs); jsonWriter.endArray().endObject();
stringBuilder.append("\"last\":[");
for (int i = 0; i < lastLogsList.size(); i++) {
stringBuilder.append(lastLogsList.get(i)).append(",");
}
stringBuilder.append(']');
return stringBuilder.toString();
} }
} }
} }