From 8f82d1c95def2669e46005fec9e32c0bb3bcb28d Mon Sep 17 00:00:00 2001 From: Tom Bursch Date: Thu, 6 Mar 2025 10:31:25 +0100 Subject: [PATCH 1/2] Add RichConsoleLogHook --- .../de/se_rwth/commons/logging/Finding.java | 20 +++++++++-- .../de/se_rwth/commons/logging/ILogHook.java | 30 ++++++++++++---- .../commons/logging/RichConsoleLogHook.java | 34 +++++++++++++++++++ .../de/se_rwth/commons/SourcePosition.java | 17 +++++++--- 4 files changed, 88 insertions(+), 13 deletions(-) create mode 100644 se-commons-logging/src/main/java/de/se_rwth/commons/logging/RichConsoleLogHook.java diff --git a/se-commons-logging/src/main/java/de/se_rwth/commons/logging/Finding.java b/se-commons-logging/src/main/java/de/se_rwth/commons/logging/Finding.java index 92ab0fa..55dcbcc 100644 --- a/se-commons-logging/src/main/java/de/se_rwth/commons/logging/Finding.java +++ b/se-commons-logging/src/main/java/de/se_rwth/commons/logging/Finding.java @@ -75,14 +75,30 @@ public Finding(Finding.Type type, String msg) { */ public String buildMsg() { if (this.sourcePositionEnd.isPresent() && this.sourcePosition.isPresent()) { - return String.format(FORMAT_WITH_TWO_SOURCE_POSITIONS, this.sourcePosition.get(), this.sourcePositionEnd.get(), this.msg); + return String.format(FORMAT_WITH_TWO_SOURCE_POSITIONS, this.sourcePosition.get().toString(), this.sourcePositionEnd.get().toString(), this.msg); } else if (this.sourcePosition.isPresent()) { - return String.format(FORMAT_WITH_SOURCE_POSITION, this.sourcePosition.get(), this.msg); + return String.format(FORMAT_WITH_SOURCE_POSITION, this.sourcePosition.get().toString(), this.msg); } else { return this.msg; } } + + /** + * Formats the message in the GNU format. + * See GNU reference for more information. + * + * @return the formatted message + */ + public String buildMsgGNU() { + if (this.sourcePositionEnd.isPresent() && this.sourcePosition.isPresent()) { + return String.format(FORMAT_WITH_TWO_SOURCE_POSITIONS, this.sourcePosition.get().toStringFullPath(), this.sourcePositionEnd.get().getLine() + ":" + this.sourcePositionEnd.get().getColumn(), this.msg); + } else if (this.sourcePosition.isPresent()) { + return String.format(FORMAT_WITH_SOURCE_POSITION, this.sourcePosition.get().toStringFullPath(), this.msg); + } else { + return this.msg; + } + } /** * @return sourcePosition diff --git a/se-commons-logging/src/main/java/de/se_rwth/commons/logging/ILogHook.java b/se-commons-logging/src/main/java/de/se_rwth/commons/logging/ILogHook.java index 1dbc533..c5711b2 100644 --- a/se-commons-logging/src/main/java/de/se_rwth/commons/logging/ILogHook.java +++ b/se-commons-logging/src/main/java/de/se_rwth/commons/logging/ILogHook.java @@ -45,7 +45,7 @@ default void doDebug(String msg, Throwable t, String logName) { } default void doInfo(String msg, String logName) { - doPrintln(String.format("[INFO] %s %s", logName, msg)); + doPrintln(formatInfo(msg, logName)); } default void doInfo(String msg, Throwable t, String logName) { @@ -53,33 +53,49 @@ default void doInfo(String msg, Throwable t, String logName) { doPrintStackTrace(t); } + default String formatInfo(String msg, String logName) { + return String.format("[INFO] %s %s", logName, msg); + } + default void doWarn(Finding warn) { - doPrintln(String.format("[WARN] %s", warn)); + doPrintln(formatWarn(warn)); } default void doWarn(Finding warn, Throwable t) { - doPrintln(String.format("[WARN] %s", warn)); + doPrintln(formatWarn(warn)); doPrintStackTrace(t); } + default String formatWarn(Finding warn) { + return String.format("[WARN] %s", warn); + } + default void doError(Finding error) { - doPrintln(String.format("[ERROR] %s", error)); + doPrintln(formatError(error)); } default void doError(Finding error, Throwable t) { - doPrintln(String.format("[ERROR] %s", error)); + doPrintln(formatError(error)); doErrPrintStackTrace(t); } + default String formatError(Finding error) { + return String.format("[ERROR] %s", error); + } + default void doErrorUser(Finding error) { - doPrintln(String.format("[USER-ERROR] %s", error)); + doPrintln(formatErrorUser(error)); } default void doErrorUser(Finding error, Throwable t) { - doPrintln(String.format("[USER-ERROR] %s", error)); + doPrintln(formatErrorUser(error)); doErrPrintStackTrace(t); } + default String formatErrorUser(Finding error) { + return String.format("[USER-ERROR] %s", error); + } + void doPrintln(String msg); void doErrPrint(String msg); diff --git a/se-commons-logging/src/main/java/de/se_rwth/commons/logging/RichConsoleLogHook.java b/se-commons-logging/src/main/java/de/se_rwth/commons/logging/RichConsoleLogHook.java new file mode 100644 index 0000000..4653097 --- /dev/null +++ b/se-commons-logging/src/main/java/de/se_rwth/commons/logging/RichConsoleLogHook.java @@ -0,0 +1,34 @@ +/* (c) https://github.com/MontiCore/monticore */ +package de.se_rwth.commons.logging; + +/** + * A logger that supports rich console output, including colors and hyperlinks to files. + */ +public class RichConsoleLogHook extends ConsoleLogHook { + + public static final String RESET = "\033[0m"; + public static final String BLUE = "\033[0;34m"; + public static final String YELLOW = "\033[0;33m"; + public static final String RED = "\033[0;31m"; + public static final String RED_BOLD = "\033[1;31m"; + + @Override + public String formatInfo(String msg, String logName) { + return String.format("%s[INFO]%s %s %s", BLUE, RESET, logName, msg); + } + + @Override + public String formatWarn(Finding warn) { + return String.format("%s[WARN]%s %s", YELLOW, RESET, warn.buildMsgGNU()); + } + + @Override + public String formatError(Finding error) { + return String.format("%s[ERROR]%s %s", RED_BOLD, RESET, error.buildMsgGNU()); + } + + @Override + public String formatErrorUser(Finding error) { + return String.format("%s[USER-ERROR]%s %s", RED, RESET, error.buildMsgGNU()); + } +} diff --git a/se-commons-utilities/src/main/java/de/se_rwth/commons/SourcePosition.java b/se-commons-utilities/src/main/java/de/se_rwth/commons/SourcePosition.java index 3d8957e..0b00184 100644 --- a/se-commons-utilities/src/main/java/de/se_rwth/commons/SourcePosition.java +++ b/se-commons-utilities/src/main/java/de/se_rwth/commons/SourcePosition.java @@ -12,7 +12,7 @@ * */ public final class SourcePosition implements Comparable, Cloneable { - + /* A default source position at line 0 and column 0. */ static SourcePosition defaultPosition = new SourcePosition(0, 0); @@ -54,9 +54,7 @@ public SourcePosition(int line, int column) { /** * Constructor for mc.ast.SourcePosition - * - * @param line - * @param column + * * @param fileName */ public SourcePosition(String fileName) { @@ -161,6 +159,17 @@ public String toString() { + "<" + this.line + "," + this.column + ">"; } + + /** + * The String representation of this source position of the form: + * "filepath:a:b" where "a" denotes the line component + * and "b" denotes the column component. + * + */ + public String toStringFullPath() { + return (this.fileName.isPresent() ? this.fileName.get() : "") + + ":" + this.line + ":" + this.column; + } @Override public int compareTo(SourcePosition o) { From 27b789a95e8663889233c2c70cd015919b200b69 Mon Sep 17 00:00:00 2001 From: Tom Bursch Date: Thu, 6 Mar 2025 14:46:38 +0100 Subject: [PATCH 2/2] Allow clearing LogHooks --- .../src/main/java/de/se_rwth/commons/logging/Log.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/se-commons-logging/src/main/java/de/se_rwth/commons/logging/Log.java b/se-commons-logging/src/main/java/de/se_rwth/commons/logging/Log.java index b00f0a7..c35a0dd 100755 --- a/se-commons-logging/src/main/java/de/se_rwth/commons/logging/Log.java +++ b/se-commons-logging/src/main/java/de/se_rwth/commons/logging/Log.java @@ -1052,6 +1052,10 @@ public static void removeLogHook(ILogHook hook) { getLog().logHooks.remove(hook); } + public static void clearLogHooks() { + getLog().logHooks.clear(); + } + public static void setErrorHook(IErrorHook hook) { getLog().errorHook = hook; }