blob: 76f3aa7d0ac7975a06f98842b3f1c457c53722ac [file] [log] [blame]
// Copyright (C) 2019 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import {TrackState} from '../../common/state';
import {checkerboardExcept} from '../../frontend/checkerboard';
import {globals} from '../../frontend/globals';
import {Track} from '../../frontend/track';
import {trackRegistry} from '../../frontend/track_registry';
import {ANDROID_LOGS_TRACK_KIND, Config, Data} from './common';
interface LevelCfg {
color: string;
prios: number[];
}
const LEVELS: LevelCfg[] = [
{color: 'hsl(122, 39%, 49%)', prios: [0, 1, 2, 3]}, // Up to DEBUG: Green.
{color: 'hsl(0, 0%, 70%)', prios: [4]}, // 4 (INFO) -> Gray.
{color: 'hsl(45, 100%, 51%)', prios: [5]}, // 5 (WARN) -> Amber.
{color: 'hsl(4, 90%, 58%)', prios: [6]}, // 6 (ERROR) -> Red.
{color: 'hsl(291, 64%, 42%)', prios: [7]}, // 7 (FATAL) -> Purple
];
const MARGIN_TOP = 2;
const RECT_HEIGHT = 35;
const EVT_PX = 2; // Width of an event tick in pixels.
class AndroidLogTrack extends Track<Config, Data> {
static readonly kind = ANDROID_LOGS_TRACK_KIND;
static create(trackState: TrackState): AndroidLogTrack {
return new AndroidLogTrack(trackState);
}
constructor(trackState: TrackState) {
super(trackState);
}
renderCanvas(ctx: CanvasRenderingContext2D): void {
const {timeScale, visibleWindowTime} = globals.frontendLocalState;
const data = this.data();
if (data === undefined) return; // Can't possibly draw anything.
const dataStartPx = timeScale.timeToPx(data.start);
const dataEndPx = timeScale.timeToPx(data.end);
const visibleStartPx = timeScale.timeToPx(visibleWindowTime.start);
const visibleEndPx = timeScale.timeToPx(visibleWindowTime.end);
checkerboardExcept(
ctx,
this.getHeight(),
visibleStartPx,
visibleEndPx,
dataStartPx,
dataEndPx);
const quantWidth =
Math.max(EVT_PX, timeScale.deltaTimeToPx(data.resolution));
const blockH = RECT_HEIGHT / LEVELS.length;
for (let i = 0; i < data.timestamps.length; i++) {
for (let lev = 0; lev < LEVELS.length; lev++) {
let hasEventsForCurColor = false;
for (const prio of LEVELS[lev].prios) {
if (data.priorities[i] & (1 << prio)) hasEventsForCurColor = true;
}
if (!hasEventsForCurColor) continue;
ctx.fillStyle = LEVELS[lev].color;
const px = Math.floor(timeScale.timeToPx(data.timestamps[i]));
ctx.fillRect(px, MARGIN_TOP + blockH * lev, quantWidth, blockH);
} // for(lev)
} // for (timestamps)
}
}
trackRegistry.register(AndroidLogTrack);