1#!/usr/bin/env python3 2# Copyright (C) 2023 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 a 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 16from python.generators.diff_tests.testing import Path, DataPath, Metric 17from python.generators.diff_tests.testing import Csv, Json, TextProto 18from python.generators.diff_tests.testing import DiffTestBlueprint 19from python.generators.diff_tests.testing import TestSuite 20 21 22class ChromeParser(TestSuite): 23 # Log messages. 24 def test_chrome_log_message(self): 25 return DiffTestBlueprint( 26 trace=TextProto(r""" 27 packet { 28 timestamp: 0 29 incremental_state_cleared: true 30 trusted_packet_sequence_id: 1 31 track_descriptor { 32 uuid: 12345 33 thread { 34 pid: 123 35 tid: 345 36 } 37 parent_uuid: 0 38 chrome_thread { 39 thread_type: THREAD_POOL_FG_WORKER 40 } 41 } 42 } 43 44 packet { 45 trusted_packet_sequence_id: 1 46 timestamp: 10 47 track_event { 48 track_uuid: 12345 49 categories: "cat1" 50 type: TYPE_INSTANT 51 name: "slice1" 52 log_message { 53 body_iid: 1 54 source_location_iid: 3 55 } 56 } 57 interned_data { 58 log_message_body { 59 iid: 1 60 body: "log message" 61 } 62 source_locations { 63 iid: 3 64 function_name: "func" 65 file_name: "foo.cc" 66 line_number: 123 67 } 68 } 69 } 70 """), 71 query=""" 72 SELECT utid, tag, msg, prio FROM android_logs; 73 """, 74 # If the log_message_body doesn't have any priority, a default 4 (i.e. 75 # INFO) is assumed (otherwise the UI will not show the message). 76 out=Csv(""" 77 "utid","tag","msg","prio" 78 1,"foo.cc:123","log message",4 79 """)) 80 81 def test_chrome_log_message_priority(self): 82 return DiffTestBlueprint( 83 trace=TextProto(r""" 84 packet { 85 timestamp: 0 86 incremental_state_cleared: true 87 trusted_packet_sequence_id: 1 88 track_descriptor { 89 uuid: 12345 90 thread { 91 pid: 123 92 tid: 345 93 } 94 parent_uuid: 0 95 chrome_thread { 96 thread_type: THREAD_POOL_FG_WORKER 97 } 98 } 99 } 100 101 packet { 102 trusted_packet_sequence_id: 1 103 timestamp: 10 104 track_event { 105 track_uuid: 12345 106 categories: "cat1" 107 type: TYPE_INSTANT 108 name: "slice1" 109 log_message { 110 body_iid: 1 111 source_location_iid: 3 112 prio: PRIO_WARN 113 } 114 } 115 interned_data { 116 log_message_body { 117 iid: 1 118 body: "log message" 119 } 120 source_locations { 121 iid: 3 122 function_name: "func" 123 file_name: "foo.cc" 124 line_number: 123 125 } 126 } 127 } 128 """), 129 query=""" 130 SELECT utid, tag, msg, prio FROM android_logs; 131 """, 132 out=Csv(""" 133 "utid","tag","msg","prio" 134 1,"foo.cc:123","log message",5 135 """)) 136 137 def test_chrome_log_message_args(self): 138 return DiffTestBlueprint( 139 trace=TextProto(r""" 140 packet { 141 timestamp: 0 142 incremental_state_cleared: true 143 trusted_packet_sequence_id: 1 144 track_descriptor { 145 uuid: 12345 146 thread { 147 pid: 123 148 tid: 345 149 } 150 parent_uuid: 0 151 chrome_thread { 152 thread_type: THREAD_POOL_FG_WORKER 153 } 154 } 155 } 156 157 packet { 158 trusted_packet_sequence_id: 1 159 timestamp: 10 160 track_event { 161 track_uuid: 12345 162 categories: "cat1" 163 type: TYPE_INSTANT 164 name: "slice1" 165 log_message { 166 body_iid: 1 167 source_location_iid: 3 168 } 169 } 170 interned_data { 171 log_message_body { 172 iid: 1 173 body: "log message" 174 } 175 source_locations { 176 iid: 3 177 function_name: "func" 178 file_name: "foo.cc" 179 line_number: 123 180 } 181 } 182 } 183 """), 184 query=Path('chrome_log_message_args_test.sql'), 185 out=Csv(""" 186 "log_message","function_name","file_name","line_number" 187 "log message","func","foo.cc",123 188 """))