1 /*
<lambda>null2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.protolog.tool
18 
19 import com.android.json.stream.JsonWriter
20 import com.android.protolog.tool.Constants.VERSION
21 import java.io.StringWriter
22 
23 class ViewerConfigJsonBuilder : ProtoLogTool.ProtologViewerConfigBuilder {
24     override fun build(statements: Map<ProtoLogTool.LogCall, Long>): ByteArray {
25         val groups = statements.map { it.key.logGroup }.toSet()
26         val stringWriter = StringWriter()
27         val writer = JsonWriter(stringWriter)
28         writer.setIndent("  ")
29         writer.beginObject()
30         writer.name("version")
31         writer.value(VERSION)
32         writer.name("messages")
33         writer.beginObject()
34         statements.forEach { (log, key) ->
35             writer.name(key.toString())
36             writer.beginObject()
37             writer.name("message")
38             writer.value(log.messageString)
39             writer.name("level")
40             writer.value(log.logLevel.name)
41             writer.name("group")
42             writer.value(log.logGroup.name)
43             writer.name("at")
44             writer.value(log.position)
45             writer.endObject()
46         }
47         writer.endObject()
48         writer.name("groups")
49         writer.beginObject()
50         groups.toSortedSet { o1, o2 -> o1.name.compareTo(o2.name) }.forEach { group ->
51             writer.name(group.name)
52             writer.beginObject()
53             writer.name("tag")
54             writer.value(group.tag)
55             writer.endObject()
56         }
57         writer.endObject()
58         writer.endObject()
59         stringWriter.buffer.append('\n')
60         return stringWriter.toString().toByteArray()
61     }
62 }
63