Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Jul 1, 2024
1 parent ddf73fe commit da47b85
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
84 changes: 84 additions & 0 deletions cffu-core/src/main/java/io/foldright/cffu/ExceptionReporter.java
Original file line number Diff line number Diff line change
@@ -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> 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);
}
}
}
15 changes: 15 additions & 0 deletions cffu-core/src/main/java/io/foldright/cffu/LDemo.java
Original file line number Diff line number Diff line change
@@ -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!"));
}
}

0 comments on commit da47b85

Please sign in to comment.