1// Copyright (C) 2019 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15import {checkerboardExcept} from '../../frontend/checkerboard'; 16import {globals} from '../../frontend/globals'; 17import {NewTrackArgs, Track} from '../../frontend/track'; 18import {trackRegistry} from '../../frontend/track_registry'; 19 20import {ANDROID_LOGS_TRACK_KIND, Config, Data} from './common'; 21 22interface LevelCfg { 23 color: string; 24 prios: number[]; 25} 26 27const LEVELS: LevelCfg[] = [ 28 {color: 'hsl(122, 39%, 49%)', prios: [0, 1, 2, 3]}, // Up to DEBUG: Green. 29 {color: 'hsl(0, 0%, 70%)', prios: [4]}, // 4 (INFO) -> Gray. 30 {color: 'hsl(45, 100%, 51%)', prios: [5]}, // 5 (WARN) -> Amber. 31 {color: 'hsl(4, 90%, 58%)', prios: [6]}, // 6 (ERROR) -> Red. 32 {color: 'hsl(291, 64%, 42%)', prios: [7]}, // 7 (FATAL) -> Purple 33]; 34 35const MARGIN_TOP = 2; 36const RECT_HEIGHT = 35; 37const EVT_PX = 2; // Width of an event tick in pixels. 38 39 40class AndroidLogTrack extends Track<Config, Data> { 41 static readonly kind = ANDROID_LOGS_TRACK_KIND; 42 static create(args: NewTrackArgs): AndroidLogTrack { 43 return new AndroidLogTrack(args); 44 } 45 46 constructor(args: NewTrackArgs) { 47 super(args); 48 } 49 50 renderCanvas(ctx: CanvasRenderingContext2D): void { 51 const {timeScale, visibleWindowTime} = globals.frontendLocalState; 52 53 const data = this.data(); 54 55 if (data === undefined) return; // Can't possibly draw anything. 56 57 const dataStartPx = timeScale.timeToPx(data.start); 58 const dataEndPx = timeScale.timeToPx(data.end); 59 const visibleStartPx = timeScale.timeToPx(visibleWindowTime.start); 60 const visibleEndPx = timeScale.timeToPx(visibleWindowTime.end); 61 62 checkerboardExcept( 63 ctx, 64 this.getHeight(), 65 visibleStartPx, 66 visibleEndPx, 67 dataStartPx, 68 dataEndPx); 69 70 const quantWidth = 71 Math.max(EVT_PX, timeScale.deltaTimeToPx(data.resolution)); 72 const blockH = RECT_HEIGHT / LEVELS.length; 73 for (let i = 0; i < data.timestamps.length; i++) { 74 for (let lev = 0; lev < LEVELS.length; lev++) { 75 let hasEventsForCurColor = false; 76 for (const prio of LEVELS[lev].prios) { 77 if (data.priorities[i] & (1 << prio)) hasEventsForCurColor = true; 78 } 79 if (!hasEventsForCurColor) continue; 80 ctx.fillStyle = LEVELS[lev].color; 81 const px = Math.floor(timeScale.timeToPx(data.timestamps[i])); 82 ctx.fillRect(px, MARGIN_TOP + blockH * lev, quantWidth, blockH); 83 } // for(lev) 84 } // for (timestamps) 85 } 86} 87 88trackRegistry.register(AndroidLogTrack); 89