From efb2fcafca0d51d7d3a8f7fa18d31fad516dc9c6 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Fri, 25 Apr 2014 22:19:15 +0200 Subject: [PATCH] test framework: ability to intercept error/warn logs --- .../testFramework/LoggedErrorProcessor.java | 44 ++++++++++--------- .../intellij/testFramework/TestLogger.java | 17 ++++--- 2 files changed, 32 insertions(+), 29 deletions(-) diff --git a/platform/testFramework/src/com/intellij/testFramework/LoggedErrorProcessor.java b/platform/testFramework/src/com/intellij/testFramework/LoggedErrorProcessor.java index 652362516bc3..eb7611e4ed9e 100644 --- a/platform/testFramework/src/com/intellij/testFramework/LoggedErrorProcessor.java +++ b/platform/testFramework/src/com/intellij/testFramework/LoggedErrorProcessor.java @@ -16,33 +16,19 @@ package com.intellij.testFramework; import org.apache.log4j.Logger; -import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; -@NonNls public abstract class LoggedErrorProcessor { - private static final LoggedErrorProcessor DEFAULT = new LoggedErrorProcessor() { - @Override - public void processError(String message, Throwable t, String[] details, Logger logger) { - logger.info(message, t); - System.err.println("ERROR: " + message); - if (t != null) t.printStackTrace(); - if (details != null && details.length > 0) { - System.out.println("details: "); - for (String detail : details) { - System.out.println(detail); - } - } - - throw new AssertionError(message); - } - }; +public class LoggedErrorProcessor { + private static final LoggedErrorProcessor DEFAULT = new LoggedErrorProcessor(); private static LoggedErrorProcessor ourInstance = DEFAULT; + @NotNull public static LoggedErrorProcessor getInstance() { return ourInstance; } - public static void setNewInstance(LoggedErrorProcessor newInstance) { + public static void setNewInstance(@NotNull LoggedErrorProcessor newInstance) { ourInstance = newInstance; } @@ -50,5 +36,23 @@ import org.jetbrains.annotations.NonNls; ourInstance = DEFAULT; } - public abstract void processError(String message, Throwable t, String[] details, Logger logger); + public void processWarn(String message, Throwable t, @NotNull Logger logger) { + logger.warn(message, t); + } + + @SuppressWarnings("UseOfSystemOutOrSystemErr") + public void processError(String message, Throwable t, String[] details, @NotNull Logger logger) { + logger.info(message, t); + + System.err.println("ERROR: " + message); + if (t != null) t.printStackTrace(System.err); + if (details != null && details.length > 0) { + System.out.println("details: "); + for (String detail : details) { + System.out.println(detail); + } + } + + throw new AssertionError(message); + } } diff --git a/platform/testFramework/src/com/intellij/testFramework/TestLogger.java b/platform/testFramework/src/com/intellij/testFramework/TestLogger.java index e0d531e05137..16016ee0d8d4 100644 --- a/platform/testFramework/src/com/intellij/testFramework/TestLogger.java +++ b/platform/testFramework/src/com/intellij/testFramework/TestLogger.java @@ -17,7 +17,6 @@ package com.intellij.testFramework; import org.apache.log4j.Level; import org.apache.log4j.Logger; -import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -44,15 +43,10 @@ public class TestLogger extends com.intellij.openapi.diagnostic.Logger { } @Override - public void debug(@NonNls String message, Throwable t) { + public void debug(String message, Throwable t) { myLogger.debug(message, t); } - @Override - public void error(String message, @Nullable Throwable t, @NotNull String... details) { - LoggedErrorProcessor.getInstance().processError(message, t, details, myLogger); - } - @Override public void info(String message) { myLogger.info(message); @@ -64,8 +58,13 @@ public class TestLogger extends com.intellij.openapi.diagnostic.Logger { } @Override - public void warn(@NonNls String message, Throwable t) { - myLogger.warn(message, t); + public void warn(String message, @Nullable Throwable t) { + LoggedErrorProcessor.getInstance().processWarn(message, t, myLogger); + } + + @Override + public void error(String message, @Nullable Throwable t, @NotNull String... details) { + LoggedErrorProcessor.getInstance().processError(message, t, details, myLogger); } public Level getLevel() {