Skip to content

Commit

Permalink
console colors
Browse files Browse the repository at this point in the history
  • Loading branch information
paullatzelsperger committed Mar 28, 2024
1 parent 923d887 commit bc68872
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 19 deletions.
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;
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"; // Text Reset

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

// Bold
String BLACK_BOLD = "\033[1;30m"; // BLACK
String RED_BOLD = "\033[1;31m"; // RED
String GREEN_BOLD = "\033[1;32m"; // GREEN
String YELLOW_BOLD = "\033[1;33m"; // YELLOW
String BLUE_BOLD = "\033[1;34m"; // BLUE
String PURPLE_BOLD = "\033[1;35m"; // PURPLE
String CYAN_BOLD = "\033[1;36m"; // CYAN
String WHITE_BOLD = "\033[1;37m"; // WHITE

// Underline
String BLACK_UNDERLINED = "\033[4;30m"; // BLACK
String RED_UNDERLINED = "\033[4;31m"; // RED
String GREEN_UNDERLINED = "\033[4;32m"; // GREEN
String YELLOW_UNDERLINED = "\033[4;33m"; // YELLOW
String BLUE_UNDERLINED = "\033[4;34m"; // BLUE
String PURPLE_UNDERLINED = "\033[4;35m"; // PURPLE
String CYAN_UNDERLINED = "\033[4;36m"; // CYAN
String WHITE_UNDERLINED = "\033[4;37m"; // WHITE

// Background
String BLACK_BACKGROUND = "\033[40m"; // BLACK
String RED_BACKGROUND = "\033[41m"; // RED
String GREEN_BACKGROUND = "\033[42m"; // GREEN
String YELLOW_BACKGROUND = "\033[43m"; // YELLOW
String BLUE_BACKGROUND = "\033[44m"; // BLUE
String PURPLE_BACKGROUND = "\033[45m"; // PURPLE
String CYAN_BACKGROUND = "\033[46m"; // CYAN
String WHITE_BACKGROUND = "\033[47m"; // WHITE

// High Intensity
String BLACK_BRIGHT = "\033[0;90m"; // BLACK
String RED_BRIGHT = "\033[0;91m"; // RED
String GREEN_BRIGHT = "\033[0;92m"; // GREEN
String YELLOW_BRIGHT = "\033[0;93m"; // YELLOW
String BLUE_BRIGHT = "\033[0;94m"; // BLUE
String PURPLE_BRIGHT = "\033[0;95m"; // PURPLE
String CYAN_BRIGHT = "\033[0;96m"; // CYAN
String WHITE_BRIGHT = "\033[0;97m"; // WHITE

// Bold High Intensity
String BLACK_BOLD_BRIGHT = "\033[1;90m"; // BLACK
String RED_BOLD_BRIGHT = "\033[1;91m"; // RED
String GREEN_BOLD_BRIGHT = "\033[1;92m"; // GREEN
String YELLOW_BOLD_BRIGHT = "\033[1;93m";// YELLOW
String BLUE_BOLD_BRIGHT = "\033[1;94m"; // BLUE
String PURPLE_BOLD_BRIGHT = "\033[1;95m";// PURPLE
String CYAN_BOLD_BRIGHT = "\033[1;96m"; // CYAN
String WHITE_BOLD_BRIGHT = "\033[1;97m"; // WHITE

// High Intensity backgrounds
String BLACK_BACKGROUND_BRIGHT = "\033[0;100m";// BLACK
String RED_BACKGROUND_BRIGHT = "\033[0;101m";// RED
String GREEN_BACKGROUND_BRIGHT = "\033[0;102m";// GREEN
String YELLOW_BACKGROUND_BRIGHT = "\033[0;103m";// YELLOW
String BLUE_BACKGROUND_BRIGHT = "\033[0;104m";// BLUE
String PURPLE_BACKGROUND_BRIGHT = "\033[0;105m"; // PURPLE
String CYAN_BACKGROUND_BRIGHT = "\033[0;106m"; // CYAN
String WHITE_BACKGROUND_BRIGHT = "\033[0;107m"; // WHITE
}
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,35 @@ 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 = getColorCode(level);
var resetCode = useColor ? ConsoleColor.RESET : "";

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

private String getColorCode(String level) {
if (!useColor) {
return "";
}

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

0 comments on commit bc68872

Please sign in to comment.