|
| 1 | +/* |
| 2 | + * Source++, the continuous feedback platform for developers. |
| 3 | + * Copyright (C) 2022-2023 CodeBrig, Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package spp.jetbrains.view.window |
| 18 | + |
| 19 | +import com.intellij.codeInsight.lookup.impl.LookupCellRenderer |
| 20 | +import com.intellij.execution.ui.ConsoleView |
| 21 | +import com.intellij.execution.ui.ConsoleViewContentType |
| 22 | +import com.intellij.openapi.editor.markup.TextAttributes |
| 23 | +import io.vertx.core.json.JsonObject |
| 24 | +import spp.jetbrains.view.ResumableView |
| 25 | +import spp.protocol.artifact.ArtifactNameUtils |
| 26 | +import spp.protocol.artifact.log.Log |
| 27 | +import spp.protocol.view.LiveView |
| 28 | +import spp.protocol.view.LiveViewEvent |
| 29 | +import java.awt.Font |
| 30 | +import java.time.LocalTime |
| 31 | +import java.time.ZoneId |
| 32 | + |
| 33 | +/** |
| 34 | + * todo: description. |
| 35 | + * |
| 36 | + * @since 0.7.6 |
| 37 | + * @author [Brandon Fergerson](mailto:[email protected]) |
| 38 | + */ |
| 39 | +interface LiveLogWindow : ResumableView { |
| 40 | + |
| 41 | + companion object { |
| 42 | + val LIVE_OUTPUT_TYPE = ConsoleViewContentType( |
| 43 | + "LIVE_OUTPUT", |
| 44 | + TextAttributes(LookupCellRenderer.MATCHED_FOREGROUND_COLOR, null, null, null, Font.PLAIN) |
| 45 | + ) |
| 46 | + } |
| 47 | + |
| 48 | + val liveView: LiveView |
| 49 | + val console: ConsoleView |
| 50 | + |
| 51 | + fun handleEvent(viewEvent: LiveViewEvent) { |
| 52 | + val rawLog = Log(JsonObject(viewEvent.metricsData).getJsonObject("log")) |
| 53 | + val localTime = LocalTime.ofInstant(rawLog.timestamp, ZoneId.systemDefault()) |
| 54 | + val logLine = buildString { |
| 55 | + append(localTime) |
| 56 | + append(" [").append(rawLog.thread).append("] ") |
| 57 | + append(rawLog.level.uppercase()).append(" - ") |
| 58 | + rawLog.logger?.let { append(ArtifactNameUtils.getShortQualifiedClassName(it)).append(" - ") } |
| 59 | + append(rawLog.toFormattedMessage()) |
| 60 | + appendLine() |
| 61 | + } |
| 62 | + |
| 63 | + when (rawLog.level.uppercase()) { |
| 64 | + "LIVE" -> console.print(logLine, LIVE_OUTPUT_TYPE) |
| 65 | + "WARN", "ERROR" -> console.print(logLine, ConsoleViewContentType.ERROR_OUTPUT) |
| 66 | + else -> console.print(logLine, ConsoleViewContentType.NORMAL_OUTPUT) |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments