1 /*
<lambda>null2  * Copyright (C) 2019 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 perfetto.protos.PerfettoTrace.ProtoLogLevel
20 import perfetto.protos.PerfettoTrace.ProtoLogViewerConfig
21 
22 /**
23  * A builder class to construct the viewer configuration (i.e. mappings of protolog hashes to log
24  * message information used to decode the protolog messages) encoded as a proto message.
25  */
26 class ViewerConfigProtoBuilder : ProtoLogTool.ProtologViewerConfigBuilder {
27     /**
28      * @return a byte array of a ProtoLogViewerConfig proto message encoding all the viewer
29      * configurations mapping protolog hashes to message information and log group information.
30      */
31     override fun build(statements: Map<ProtoLogTool.LogCall, Long>): ByteArray {
32         val configBuilder = ProtoLogViewerConfig.newBuilder()
33 
34         val groups = statements.map { it.key.logGroup }.toSet()
35         val groupIds = mutableMapOf<LogGroup, Int>()
36         groups.forEach {
37             groupIds.putIfAbsent(it, groupIds.size + 1)
38         }
39 
40         groupIds.forEach { (group, id) ->
41             configBuilder.addGroups(ProtoLogViewerConfig.Group.newBuilder()
42                     .setId(id)
43                     .setName(group.name)
44                     .setTag(group.tag)
45                     .build())
46         }
47 
48         statements.forEach { (log, key) ->
49             val groupId = groupIds[log.logGroup] ?: error("missing group id")
50 
51             configBuilder.addMessages(
52                 ProtoLogViewerConfig.MessageData.newBuilder()
53                         .setMessageId(key)
54                         .setMessage(log.messageString)
55                         .setLevel(
56                             ProtoLogLevel.forNumber(log.logLevel.ordinal + 1))
57                         .setGroupId(groupId)
58             )
59         }
60 
61         return configBuilder.build().toByteArray()
62     }
63 }
64