Code review

This commit is contained in:
OLEGSHA 2020-11-02 17:27:17 +03:00
parent 384b2047ac
commit e7e54d0ffd
6 changed files with 34 additions and 39 deletions

View File

@ -34,10 +34,11 @@ public class ProgressiaClientMain {
CrashReportGenerator.registerProvider(new OSContextProvider());
CrashReportGenerator.registerAnalyzer(new OutOfMemoryAnalyzer());
try {
@SuppressWarnings("unused")
long[] ssdss = new long[1 << 30];
} catch (Throwable t)
{
CrashReportGenerator.makeCrashReport(t, "");
CrashReportGenerator.makeCrashReport(t, "u %s stupid", "vry");
}
ProgressiaLauncher.launch(args, new ClientProxy());

View File

@ -1,5 +1,5 @@
package ru.windcorp.progressia.common.util.crash;
public interface Analyzer {
String getPrompt(Throwable throwable, String messageFormat, Object... args);
String analyze(Throwable throwable, String messageFormat, Object... args);
}

View File

@ -3,6 +3,5 @@ package ru.windcorp.progressia.common.util.crash;
import java.util.Map;
public interface ContextProvider {
Map<String, String> provideContext();
}

View File

@ -13,50 +13,47 @@ import java.util.Map;
public class CrashReportGenerator {
private CrashReportGenerator() {
}
private CrashReportGenerator() {}
final static File latestLogFile = new File("crash-reports/latest.log");
private static final File LATEST_LOG_FILE = new File("crash-reports/latest.log");
private static Collection<ContextProvider> providers = new ArrayList<ContextProvider>();
private static Collection<Map<String, String>> providerResponse = new ArrayList<Map<String, String>>();
private static final Collection<ContextProvider> PROVIDERS = new ArrayList<>();
private static final Collection<Map<String, String>> PROVIDER_RESPONSES = new ArrayList<>();
private static Collection<Analyzer> analyzers = new ArrayList<Analyzer>();
private static Collection<String> analyzerResponse = new ArrayList<String>();
private static final Collection<Analyzer> ANALYZER = new ArrayList<>();
private static final Collection<String> ANALYZER_RESPONSES = new ArrayList<>();
private static final Logger logger = LogManager.getLogger("crash");
private static final Logger LOGGER = LogManager.getLogger("crash");
static public void makeCrashReport(Throwable throwable, String messageFormat, Object... args) {
public static void makeCrashReport(Throwable throwable, String messageFormat, Object... args) {
StringBuilder output = new StringBuilder();
for (ContextProvider currentProvider : providers) {
if (currentProvider != null) {
providerResponse.add(currentProvider.provideContext());
for (ContextProvider provider : PROVIDERS) {
if (provider != null) {
PROVIDER_RESPONSES.add(provider.provideContext());
}
}
if (throwable != null) {
for (Analyzer currentAnalyzer : analyzers) {
if (currentAnalyzer != null) {
analyzerResponse.add(currentAnalyzer.getPrompt(throwable, messageFormat, args));
}
for (Analyzer analyzer : ANALYZER) {
if (analyzer != null) {
ANALYZER_RESPONSES.add(analyzer.analyze(throwable, messageFormat, args));
}
}
for (Map<String, String> currentProviderResponse : providerResponse) {
if (currentProviderResponse != null && !currentProviderResponse.isEmpty()) {
for (Map<String, String> response : PROVIDER_RESPONSES) {
if (response != null && !response.isEmpty()) {
addSeparator(output);
for (Map.Entry<String, String> entry : currentProviderResponse.entrySet()) {
for (Map.Entry<String, String> entry : response.entrySet()) {
output.append(entry.getKey()).append(": ").append(entry.getValue()).append("\n");
}
}
}
for (String currentPrompt : analyzerResponse) {
if (currentPrompt != null && !currentPrompt.isEmpty()) {
for (String response : ANALYZER_RESPONSES) {
if (response != null && !response.isEmpty()) {
addSeparator(output);
output.append(currentPrompt).append("\n");
output.append(response).append("\n");
}
}
@ -72,8 +69,8 @@ public class CrashReportGenerator {
sink.append("Null");
}
logger.info("\n" + output.toString());
logger.fatal("Stacktrace: \n" + sink.toString());
LOGGER.info(output.toString());
LOGGER.fatal("Stacktrace: \n" + sink.toString());
addSeparator(output);
@ -86,11 +83,9 @@ public class CrashReportGenerator {
// PLAK
}
createFileForCrashReport(output);
createFileForLatestCrashReport(output);
System.exit(0);
}
@ -111,7 +106,7 @@ public class CrashReportGenerator {
}
public static void createFileForLatestCrashReport(StringBuilder sb) {
try (FileOutputStream fos = new FileOutputStream(latestLogFile)) {
try (FileOutputStream fos = new FileOutputStream(LATEST_LOG_FILE)) {
byte[] buffer = sb.toString().getBytes();
fos.write(buffer, 0, buffer.length);
@ -121,11 +116,11 @@ public class CrashReportGenerator {
}
public static void registerProvider(ContextProvider provider) {
providers.add(provider);
PROVIDERS.add(provider);
}
public static void registerAnalyzer(Analyzer analyzer) {
analyzers.add(analyzer);
ANALYZER.add(analyzer);
}
private static void addSeparator(StringBuilder sb) {

View File

@ -4,7 +4,7 @@ import ru.windcorp.progressia.common.util.crash.Analyzer;
public class OutOfMemoryAnalyzer implements Analyzer {
@Override
public String getPrompt(Throwable throwable, String messageFormat, Object... args) {
public String analyze(Throwable throwable, String messageFormat, Object... args) {
if (throwable instanceof OutOfMemoryError)
return "Try add memory for the JVM";
return null;

View File

@ -9,10 +9,10 @@ public class OSContextProvider implements ContextProvider {
@Override
public Map<String, String> provideContext() {
Map<String, String> theThings = new HashMap<>();
theThings.put("Name OS", System.getProperty("os.name"));
theThings.put("Version OS", System.getProperty("os.version"));
theThings.put("Architecture OS", System.getProperty("os.arch"));
return theThings;
Map<String, String> result = new HashMap<>();
result.put("Name OS", System.getProperty("os.name"));
result.put("Version OS", System.getProperty("os.version"));
result.put("Architecture OS", System.getProperty("os.arch"));
return result;
}
}