Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: console colors #4058

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions DEPENDENCIES
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
maven/mavencentral/com.apicatalog/carbon-did/0.0.2, Apache-2.0, approved, #9239

Check warning on line 1 in DEPENDENCIES

View workflow job for this annotation

GitHub Actions / check / Dash-Verify-Licenses

Restricted Dependencies found

Some dependencies are marked 'restricted' - please review them
maven/mavencentral/com.apicatalog/iron-ed25519-cryptosuite-2020/0.8.1, Apache-2.0, approved, #11157
maven/mavencentral/com.apicatalog/iron-verifiable-credentials/0.8.1, Apache-2.0, approved, #9234
maven/mavencentral/com.apicatalog/titanium-json-ld/1.0.0, Apache-2.0, approved, clearlydefined
Expand Down Expand Up @@ -290,10 +290,10 @@
maven/mavencentral/org.jacoco/org.jacoco.report/0.8.9, EPL-2.0 AND Apache-2.0, approved, CQ23284
maven/mavencentral/org.javassist/javassist/3.28.0-GA, Apache-2.0 OR LGPL-2.1-or-later OR MPL-1.1, approved, #327
maven/mavencentral/org.javassist/javassist/3.29.2-GA, Apache-2.0 AND LGPL-2.1-or-later AND MPL-1.1, approved, #6023
maven/mavencentral/org.jetbrains.kotlin/kotlin-stdlib-common/1.9.10, Apache-2.0, approved, clearlydefined
maven/mavencentral/org.jetbrains.kotlin/kotlin-stdlib-jdk7/1.9.10, Apache-2.0, approved, clearlydefined
maven/mavencentral/org.jetbrains.kotlin/kotlin-stdlib-common/1.9.10, Apache-2.0, approved, #14186
maven/mavencentral/org.jetbrains.kotlin/kotlin-stdlib-jdk7/1.9.10, None, restricted, #14188
maven/mavencentral/org.jetbrains.kotlin/kotlin-stdlib-jdk8/1.8.21, Apache-2.0, approved, #8919
maven/mavencentral/org.jetbrains.kotlin/kotlin-stdlib-jdk8/1.9.10, Apache-2.0, approved, clearlydefined
maven/mavencentral/org.jetbrains.kotlin/kotlin-stdlib-jdk8/1.9.10, None, restricted, #14185
maven/mavencentral/org.jetbrains.kotlin/kotlin-stdlib/1.9.10, Apache-2.0, approved, #11827
maven/mavencentral/org.jetbrains/annotations/13.0, Apache-2.0, approved, clearlydefined
maven/mavencentral/org.jetbrains/annotations/17.0.0, Apache-2.0, approved, clearlydefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -81,14 +82,14 @@ private static Supplier<Object> getDefaultProviderInvoker(ServiceExtensionContex
};
}

public static @NotNull Monitor loadMonitor() {
public static @NotNull Monitor loadMonitor(String... programArgs) {
var loader = ServiceLoader.load(MonitorExtension.class);
return loadMonitor(loader.stream().map(ServiceLoader.Provider::get).collect(Collectors.toList()));
return loadMonitor(loader.stream().map(ServiceLoader.Provider::get).collect(Collectors.toList()), programArgs);
}

static @NotNull Monitor loadMonitor(List<MonitorExtension> availableMonitors) {
static @NotNull Monitor loadMonitor(List<MonitorExtension> availableMonitors, String... programArgs) {
if (availableMonitors.isEmpty()) {
return new ConsoleMonitor();
return new ConsoleMonitor(!Set.of(programArgs).contains("--no-color"));
}

if (availableMonitors.size() > 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
*/
public class BaseRuntime {

private static String[] programArgs = new String[0];
protected final ServiceLocator serviceLocator;
private final AtomicReference<HealthCheckResult> startupStatus = new AtomicReference<>(HealthCheckResult.failed("Startup not complete"));
private final ExtensionLoader extensionLoader;
Expand All @@ -68,6 +69,7 @@ protected BaseRuntime(ServiceLocator serviceLocator) {

public static void main(String[] args) {
BaseRuntime runtime = new BaseRuntime();
programArgs = args;
runtime.boot();
}

Expand Down Expand Up @@ -153,7 +155,7 @@ protected List<InjectionContainer<ServiceExtension>> createExtensions(ServiceExt
* Create a {@link ServiceExtensionContext} that will be used in this runtime. If e.g. a third-party dependency-injection framework were to be used,
* this would likely need to be overridden.
*
* @param monitor a Monitor
* @param monitor a Monitor
* @return a {@code ServiceExtensionContext}
*/
@NotNull
Expand Down Expand Up @@ -181,14 +183,14 @@ protected void shutdown() {
}

/**
* Hook point to instantiate a {@link Monitor}. By default, the runtime instantiates a {@code Monitor} using the Service Loader mechanism, i.e. by calling the {@link ExtensionLoader#loadMonitor()} method.
* Hook point to instantiate a {@link Monitor}. By default, the runtime instantiates a {@code Monitor} using the Service Loader mechanism, i.e. by calling the {@link ExtensionLoader#loadMonitor(String...)} method.
* <p>
* Please consider using the extension mechanism (i.e. {@link MonitorExtension}) rather than supplying a custom monitor by overriding this method.
* However, for development/testing scenarios it might be an easy solution to just override this method.
*/
@NotNull
protected Monitor createMonitor() {
return ExtensionLoader.loadMonitor();
return ExtensionLoader.loadMonitor(programArgs);
}

private void boot(boolean addShutdownHook) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ public class EdcRuntimeExtension extends EdcExtension {
/**
* Initialize an Edc runtime given a base runtime module
*
* @param baseModulePath the base runtime module path
* @param name the name.
* @param properties the properties to be used as configuration.
* @param baseModulePath the base runtime module path
* @param name the name.
* @param properties the properties to be used as configuration.
* @param additionalModules modules that will be added to the runtime.
*/
public EdcRuntimeExtension(String baseModulePath, String name, Map<String, String> properties, String... additionalModules) {
Expand All @@ -70,9 +70,9 @@ public EdcRuntimeExtension(String baseModulePath, String name, Map<String, Strin
/**
* Initialize an Edc runtime
*
* @param name the name.
* @param name the name.
* @param properties the properties to be used as configuration.
* @param modules the modules that will be used to load the runtime.
* @param modules the modules that will be used to load the runtime.
*/
public EdcRuntimeExtension(String name, Map<String, String> properties, String... modules) {
this.modules = modules;
Expand Down Expand Up @@ -159,7 +159,7 @@ public void afterTestExecution(ExtensionContext context) throws Exception {
return new Monitor() {
};
} else {
return new ConsoleMonitor(name, ConsoleMonitor.Level.DEBUG);
return new ConsoleMonitor(name, ConsoleMonitor.Level.DEBUG, true);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
*
*/

package org.eclipse.edc.spi.monitor;

public interface ConsoleColor {
// Reset
String RESET = "\033[0m";

// Regular Colors
String BLACK = "\033[0;30m";
String RED = "\033[0;31m";
String GREEN = "\033[0;32m";
String YELLOW = "\033[0;33m";
String BLUE = "\033[0;34m";
String PURPLE = "\033[0;35m";
String CYAN = "\033[0;36m";
String WHITE = "\033[0;37m";

// Bold
String BLACK_BOLD = "\033[1;30m";
String RED_BOLD = "\033[1;31m";
String GREEN_BOLD = "\033[1;32m";
String YELLOW_BOLD = "\033[1;33m";
String BLUE_BOLD = "\033[1;34m";
String PURPLE_BOLD = "\033[1;35m";
String CYAN_BOLD = "\033[1;36m";
String WHITE_BOLD = "\033[1;37m";

// Underline
String BLACK_UNDERLINED = "\033[4;30m";
String RED_UNDERLINED = "\033[4;31m";
String GREEN_UNDERLINED = "\033[4;32m";
String YELLOW_UNDERLINED = "\033[4;33m";
String BLUE_UNDERLINED = "\033[4;34m";
String PURPLE_UNDERLINED = "\033[4;35m";
String CYAN_UNDERLINED = "\033[4;36m";
String WHITE_UNDERLINED = "\033[4;37m";

// Background
String BLACK_BACKGROUND = "\033[40m";
String RED_BACKGROUND = "\033[41m";
String GREEN_BACKGROUND = "\033[42m";
String YELLOW_BACKGROUND = "\033[43m";
String BLUE_BACKGROUND = "\033[44m";
String PURPLE_BACKGROUND = "\033[45m";
String CYAN_BACKGROUND = "\033[46m";
String WHITE_BACKGROUND = "\033[47m";

// High Intensity
String BLACK_BRIGHT = "\033[0;90m";
String RED_BRIGHT = "\033[0;91m";
String GREEN_BRIGHT = "\033[0;92m";
String YELLOW_BRIGHT = "\033[0;93m";
String BLUE_BRIGHT = "\033[0;94m";
String PURPLE_BRIGHT = "\033[0;95m";
String CYAN_BRIGHT = "\033[0;96m";
String WHITE_BRIGHT = "\033[0;97m";

// Bold High Intensity
String BLACK_BOLD_BRIGHT = "\033[1;90m";
String RED_BOLD_BRIGHT = "\033[1;91m";
String GREEN_BOLD_BRIGHT = "\033[1;92m";
String YELLOW_BOLD_BRIGHT = "\033[1;93m";
String BLUE_BOLD_BRIGHT = "\033[1;94m";
String PURPLE_BOLD_BRIGHT = "\033[1;95m";
String CYAN_BOLD_BRIGHT = "\033[1;96m";
String WHITE_BOLD_BRIGHT = "\033[1;97m";

// High Intensity backgrounds
String BLACK_BACKGROUND_BRIGHT = "\033[0;100m";
String RED_BACKGROUND_BRIGHT = "\033[0;101m";
String GREEN_BACKGROUND_BRIGHT = "\033[0;102m";
String YELLOW_BACKGROUND_BRIGHT = "\033[0;103m";
String BLUE_BACKGROUND_BRIGHT = "\033[0;104m";
String PURPLE_BACKGROUND_BRIGHT = "\033[0;105m";
String CYAN_BACKGROUND_BRIGHT = "\033[0;106m";
String WHITE_BACKGROUND_BRIGHT = "\033[0;107m";
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import java.time.format.DateTimeFormatter;
import java.util.function.Supplier;

import static java.lang.String.format;

/**
* Default monitor implementation. Outputs messages to the console.
*/
Expand All @@ -32,17 +30,27 @@ public class ConsoleMonitor implements Monitor {
private static final String INFO = "INFO";
private static final String DEBUG = "DEBUG";

private final boolean useColor;

private final Level level;
private final String prefix;

public ConsoleMonitor() {
this.prefix = "";
this.level = Level.DEBUG;
this(true);
}

public ConsoleMonitor(boolean useColor) {
this(null, Level.DEBUG, useColor);
}

public ConsoleMonitor(@Nullable String runtimeName, Level level) {
this.prefix = format("[%s] ", runtimeName);
this(runtimeName, level, true);
}

public ConsoleMonitor(@Nullable String runtimeName, Level level, boolean useColor) {
this.prefix = runtimeName == null ? "" : "[%s] ".formatted(runtimeName);
this.level = level;
this.useColor = useColor;
}

@Override
Expand Down Expand Up @@ -76,16 +84,31 @@ public void debug(Supplier<String> supplier, Throwable... errors) {

private void output(String level, Supplier<String> supplier, Throwable... errors) {
var time = ZonedDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
System.out.println(prefix + level + " " + time + " " + sanitizeMessage(supplier));
var colorCode = useColor ? getColorCode(level) : "";
var resetCode = useColor ? ConsoleColor.RESET : "";

System.out.println(colorCode + prefix + level + " " + time + " " + sanitizeMessage(supplier) + resetCode);
if (errors != null) {
for (Throwable error : errors) {
for (var error : errors) {
if (error != null) {
System.out.print(colorCode);
error.printStackTrace(System.out);
System.out.print(resetCode);
}
}
}
}

private String getColorCode(String level) {
return switch (level) {
case SEVERE -> ConsoleColor.RED;
case WARNING -> ConsoleColor.YELLOW;
case INFO -> ConsoleColor.GREEN;
case DEBUG -> ConsoleColor.BLUE;
default -> "";
};
}

public enum Level {
SEVERE(3), WARNING(2), INFO(1), DEBUG(0);

Expand Down