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.registerProvider(new OSContextProvider());
CrashReportGenerator.registerAnalyzer(new OutOfMemoryAnalyzer()); CrashReportGenerator.registerAnalyzer(new OutOfMemoryAnalyzer());
try { try {
@SuppressWarnings("unused")
long[] ssdss = new long[1 << 30]; long[] ssdss = new long[1 << 30];
} catch (Throwable t) } catch (Throwable t)
{ {
CrashReportGenerator.makeCrashReport(t, ""); CrashReportGenerator.makeCrashReport(t, "u %s stupid", "vry");
} }
ProgressiaLauncher.launch(args, new ClientProxy()); ProgressiaLauncher.launch(args, new ClientProxy());

View File

@ -1,5 +1,5 @@
package ru.windcorp.progressia.common.util.crash; package ru.windcorp.progressia.common.util.crash;
public interface Analyzer { 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; import java.util.Map;
public interface ContextProvider { public interface ContextProvider {
Map<String, String> provideContext(); Map<String, String> provideContext();
} }

View File

@ -13,50 +13,47 @@ import java.util.Map;
public class CrashReportGenerator { 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 final Collection<ContextProvider> PROVIDERS = new ArrayList<>();
private static Collection<Map<String, String>> providerResponse = new ArrayList<Map<String, String>>(); private static final Collection<Map<String, String>> PROVIDER_RESPONSES = new ArrayList<>();
private static Collection<Analyzer> analyzers = new ArrayList<Analyzer>(); private static final Collection<Analyzer> ANALYZER = new ArrayList<>();
private static Collection<String> analyzerResponse = new ArrayList<String>(); 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(); StringBuilder output = new StringBuilder();
for (ContextProvider currentProvider : providers) { for (ContextProvider provider : PROVIDERS) {
if (currentProvider != null) { if (provider != null) {
providerResponse.add(currentProvider.provideContext()); PROVIDER_RESPONSES.add(provider.provideContext());
} }
} }
if (throwable != null) { for (Analyzer analyzer : ANALYZER) {
for (Analyzer currentAnalyzer : analyzers) { if (analyzer != null) {
if (currentAnalyzer != null) { ANALYZER_RESPONSES.add(analyzer.analyze(throwable, messageFormat, args));
analyzerResponse.add(currentAnalyzer.getPrompt(throwable, messageFormat, args));
}
} }
} }
for (Map<String, String> currentProviderResponse : providerResponse) { for (Map<String, String> response : PROVIDER_RESPONSES) {
if (currentProviderResponse != null && !currentProviderResponse.isEmpty()) { if (response != null && !response.isEmpty()) {
addSeparator(output); 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"); output.append(entry.getKey()).append(": ").append(entry.getValue()).append("\n");
} }
} }
} }
for (String currentPrompt : analyzerResponse) { for (String response : ANALYZER_RESPONSES) {
if (currentPrompt != null && !currentPrompt.isEmpty()) { if (response != null && !response.isEmpty()) {
addSeparator(output); addSeparator(output);
output.append(currentPrompt).append("\n"); output.append(response).append("\n");
} }
} }
@ -72,8 +69,8 @@ public class CrashReportGenerator {
sink.append("Null"); sink.append("Null");
} }
logger.info("\n" + output.toString()); LOGGER.info(output.toString());
logger.fatal("Stacktrace: \n" + sink.toString()); LOGGER.fatal("Stacktrace: \n" + sink.toString());
addSeparator(output); addSeparator(output);
@ -86,11 +83,9 @@ public class CrashReportGenerator {
// PLAK // PLAK
} }
createFileForCrashReport(output); createFileForCrashReport(output);
createFileForLatestCrashReport(output); createFileForLatestCrashReport(output);
System.exit(0); System.exit(0);
} }
@ -111,7 +106,7 @@ public class CrashReportGenerator {
} }
public static void createFileForLatestCrashReport(StringBuilder sb) { 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(); byte[] buffer = sb.toString().getBytes();
fos.write(buffer, 0, buffer.length); fos.write(buffer, 0, buffer.length);
@ -121,11 +116,11 @@ public class CrashReportGenerator {
} }
public static void registerProvider(ContextProvider provider) { public static void registerProvider(ContextProvider provider) {
providers.add(provider); PROVIDERS.add(provider);
} }
public static void registerAnalyzer(Analyzer analyzer) { public static void registerAnalyzer(Analyzer analyzer) {
analyzers.add(analyzer); ANALYZER.add(analyzer);
} }
private static void addSeparator(StringBuilder sb) { 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 { public class OutOfMemoryAnalyzer implements Analyzer {
@Override @Override
public String getPrompt(Throwable throwable, String messageFormat, Object... args) { public String analyze(Throwable throwable, String messageFormat, Object... args) {
if (throwable instanceof OutOfMemoryError) if (throwable instanceof OutOfMemoryError)
return "Try add memory for the JVM"; return "Try add memory for the JVM";
return null; return null;

View File

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