From da47b858da94b337ff8b8437648a80bade08f861 Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Mon, 1 Jul 2024 20:01:32 +0800 Subject: [PATCH] test --- .../io/foldright/cffu/ExceptionReporter.java | 84 +++++++++++++++++++ .../main/java/io/foldright/cffu/LDemo.java | 15 ++++ 2 files changed, 99 insertions(+) create mode 100644 cffu-core/src/main/java/io/foldright/cffu/ExceptionReporter.java create mode 100644 cffu-core/src/main/java/io/foldright/cffu/LDemo.java diff --git a/cffu-core/src/main/java/io/foldright/cffu/ExceptionReporter.java b/cffu-core/src/main/java/io/foldright/cffu/ExceptionReporter.java new file mode 100644 index 00000000..cf67d80c --- /dev/null +++ b/cffu-core/src/main/java/io/foldright/cffu/ExceptionReporter.java @@ -0,0 +1,84 @@ +package io.foldright.cffu; + +import edu.umd.cs.findbugs.annotations.Nullable; +import io.foldright.cffu.logger.slf4j.Slf4jLogger; +import org.slf4j.LoggerFactory; +import org.slf4j.spi.LocationAwareLogger; + +import java.util.logging.Level; + + +class ExceptionReporter { + private static final LoggerAdapter logger = getLogger(); + + @SuppressWarnings("DataFlowIssue") + public static T reportException(String msg, Throwable ex) { + String report = System.getProperty("cffu.uncaught.exception.report", "short"); + if ("full".equals(report)) { + logger.error(msg, ex); + } else if ("short".equals(report)) { + logger.error(msg + ", cause: " + ex.getMessage(), null); + } + return null; + } + + private static LoggerAdapter getLogger() { + try { + return new Slf4jLoggerAdapter(); + } catch (NoClassDefFoundError e) { + return new JulLoggerAdapter(); + } + } + + private static interface LoggerAdapter { + void error(String msg, @Nullable Throwable thrown); + + void warn(String msg, @Nullable Throwable thrown); + } + + private static class Slf4jLoggerAdapter implements LoggerAdapter { + private static final String FQCN = Slf4jLogger.class.getName(); + + private final org.slf4j.Logger logger = LoggerFactory.getLogger("cffu"); + + @Override + public void error(String msg, @Nullable Throwable thrown) { + if (logger instanceof LocationAwareLogger) { + ((LocationAwareLogger) logger).log(null, FQCN, LocationAwareLogger.ERROR_INT, msg, null, thrown); + } else { + if (thrown != null) { + logger.error(msg, thrown); + } else { + logger.error(msg); + } + } + } + + @Override + public void warn(String msg, @Nullable Throwable thrown) { + if (logger instanceof LocationAwareLogger) { + ((LocationAwareLogger) logger).log(null, FQCN, LocationAwareLogger.WARN_INT, msg, null, thrown); + } else { + if (thrown != null) { + logger.error(msg, thrown); + } else { + logger.error(msg); + } + } + } + } + + private static class JulLoggerAdapter implements LoggerAdapter { + private static final java.util.logging.Logger logger = java.util.logging.Logger.getLogger("cffu"); + + @Override + public void error(String msg, @Nullable Throwable thrown) { + logger.log(Level.SEVERE, msg, thrown); + } + + @Override + public void warn(String msg, @Nullable Throwable thrown) { + logger.log(Level.WARNING, msg, thrown); + } + } +} diff --git a/cffu-core/src/main/java/io/foldright/cffu/LDemo.java b/cffu-core/src/main/java/io/foldright/cffu/LDemo.java new file mode 100644 index 00000000..f7e80f13 --- /dev/null +++ b/cffu-core/src/main/java/io/foldright/cffu/LDemo.java @@ -0,0 +1,15 @@ +package io.foldright.cffu; + + +import java.util.concurrent.ThreadLocalRandom; + + +public class LDemo { + public static void main(String[] args) { + if(ThreadLocalRandom.current().nextBoolean()) { + System.setProperty("cffu.uncaught.exception.report", "full"); + System.out.println("set full to cffu.uncaught.exception.report"); + } + ExceptionReporter.reportException("hello", new RuntimeException("Bang!")); + } +}