source: josm/trunk/src/org/openstreetmap/josm/actions/ShowStatusReportAction.java@ 10124

Last change on this file since 10124 was 10063, checked in by Don-vip, 8 years ago

fix #12691 - Include custom presets, map paint styles and validator rules in status report

  • Property svn:eol-style set to native
File size: 10.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.Dimension;
8import java.awt.event.ActionEvent;
9import java.awt.event.KeyEvent;
10import java.lang.management.ManagementFactory;
11import java.util.ArrayList;
12import java.util.Arrays;
13import java.util.Collection;
14import java.util.HashSet;
15import java.util.List;
16import java.util.ListIterator;
17import java.util.Locale;
18import java.util.Map;
19import java.util.Map.Entry;
20import java.util.Set;
21import java.util.TreeSet;
22
23import org.openstreetmap.josm.Main;
24import org.openstreetmap.josm.data.Version;
25import org.openstreetmap.josm.data.osm.DataSet;
26import org.openstreetmap.josm.data.osm.DatasetConsistencyTest;
27import org.openstreetmap.josm.data.preferences.Setting;
28import org.openstreetmap.josm.gui.ExtendedDialog;
29import org.openstreetmap.josm.gui.preferences.SourceEditor;
30import org.openstreetmap.josm.gui.preferences.SourceEditor.ExtendedSourceEntry;
31import org.openstreetmap.josm.gui.preferences.SourceEntry;
32import org.openstreetmap.josm.gui.preferences.map.MapPaintPreference;
33import org.openstreetmap.josm.gui.preferences.map.TaggingPresetPreference;
34import org.openstreetmap.josm.gui.preferences.validator.ValidatorTagCheckerRulesPreference;
35import org.openstreetmap.josm.plugins.PluginHandler;
36import org.openstreetmap.josm.tools.PlatformHookUnixoid;
37import org.openstreetmap.josm.tools.Shortcut;
38import org.openstreetmap.josm.tools.bugreport.BugReportSender;
39import org.openstreetmap.josm.tools.bugreport.DebugTextDisplay;
40
41/**
42 * @author xeen
43 *
44 * Opens a dialog with useful status information like version numbers for Java, JOSM and plugins
45 * Also includes preferences with stripped username and password
46 */
47public final class ShowStatusReportAction extends JosmAction {
48
49 /**
50 * Constructs a new {@code ShowStatusReportAction}
51 */
52 public ShowStatusReportAction() {
53 super(
54 tr("Show Status Report"),
55 "clock",
56 tr("Show status report with useful information that can be attached to bugs"),
57 Shortcut.registerShortcut("help:showstatusreport", tr("Help: {0}",
58 tr("Show Status Report")), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE), false);
59
60 putValue("help", ht("/Action/ShowStatusReport"));
61 putValue("toolbar", "help/showstatusreport");
62 Main.toolbar.register(this);
63 }
64
65 private static void shortenParam(ListIterator<String> it, String[] param, String source, String target) {
66 if (source != null && target.length() < source.length() && param[1].startsWith(source)) {
67 it.set(param[0] + '=' + param[1].replace(source, target));
68 }
69 }
70
71 private static boolean isRunningJavaWebStart() {
72 try {
73 // See http://stackoverflow.com/a/16200769/2257172
74 return Class.forName("javax.jnlp.ServiceManager") != null;
75 } catch (ClassNotFoundException e) {
76 return false;
77 }
78 }
79
80 /**
81 * Replies the report header (software and system info)
82 * @return The report header (software and system info)
83 */
84 public static String getReportHeader() {
85 StringBuilder text = new StringBuilder();
86 String runtimeVersion = System.getProperty("java.runtime.version");
87 text.append(Version.getInstance().getReleaseAttributes())
88 .append("\nIdentification: ").append(Version.getInstance().getAgentString())
89 .append("\nMemory Usage: ")
90 .append(Runtime.getRuntime().totalMemory()/1024/1024)
91 .append(" MB / ")
92 .append(Runtime.getRuntime().maxMemory()/1024/1024)
93 .append(" MB (")
94 .append(Runtime.getRuntime().freeMemory()/1024/1024)
95 .append(" MB allocated, but free)\nJava version: ")
96 .append(runtimeVersion != null ? runtimeVersion : System.getProperty("java.version")).append(", ")
97 .append(System.getProperty("java.vendor")).append(", ")
98 .append(System.getProperty("java.vm.name")).append('\n');
99 if (Main.platform.getClass() == PlatformHookUnixoid.class) {
100 // Add Java package details
101 String packageDetails = ((PlatformHookUnixoid) Main.platform).getJavaPackageDetails();
102 if (packageDetails != null) {
103 text.append("Java package: ")
104 .append(packageDetails)
105 .append('\n');
106 }
107 // Add WebStart package details if run from JNLP
108 if (isRunningJavaWebStart()) {
109 String webStartDetails = ((PlatformHookUnixoid) Main.platform).getWebStartPackageDetails();
110 if (webStartDetails != null) {
111 text.append("WebStart package: ")
112 .append(webStartDetails)
113 .append('\n');
114 }
115 }
116 }
117 try {
118 final String envJavaHome = System.getenv("JAVA_HOME");
119 final String envJavaHomeAlt = Main.isPlatformWindows() ? "%JAVA_HOME%" : "${JAVA_HOME}";
120 final String propJavaHome = System.getProperty("java.home");
121 final String propJavaHomeAlt = "<java.home>";
122 // Build a new list of VM parameters to modify it below if needed (default implementation returns an UnmodifiableList instance)
123 List<String> vmArguments = new ArrayList<>(ManagementFactory.getRuntimeMXBean().getInputArguments());
124 for (ListIterator<String> it = vmArguments.listIterator(); it.hasNext();) {
125 String value = it.next();
126 if (value.contains("=")) {
127 String[] param = value.split("=");
128 // Hide some parameters for privacy concerns
129 if (param[0].toLowerCase(Locale.ENGLISH).startsWith("-dproxy")) {
130 it.set(param[0]+"=xxx");
131 } else {
132 // Shorten some parameters for readability concerns
133 shortenParam(it, param, envJavaHome, envJavaHomeAlt);
134 shortenParam(it, param, propJavaHome, propJavaHomeAlt);
135 }
136 } else if (value.startsWith("-X")) {
137 // Remove arguments like -Xbootclasspath/a, -Xverify:remote, that can be very long and unhelpful
138 it.remove();
139 }
140 }
141 if (!vmArguments.isEmpty()) {
142 text.append("VM arguments: ").append(vmArguments.toString().replace("\\\\", "\\")).append('\n');
143 }
144 } catch (SecurityException e) {
145 // Ignore exception
146 if (Main.isTraceEnabled()) {
147 Main.trace(e.getMessage());
148 }
149 }
150 List<String> commandLineArgs = Main.getCommandLineArgs();
151 if (!commandLineArgs.isEmpty()) {
152 text.append("Program arguments: ").append(Arrays.toString(commandLineArgs.toArray())).append('\n');
153 }
154 if (Main.main != null) {
155 DataSet dataset = Main.main.getCurrentDataSet();
156 if (dataset != null) {
157 String result = DatasetConsistencyTest.runTests(dataset);
158 if (result.isEmpty()) {
159 text.append("Dataset consistency test: No problems found\n");
160 } else {
161 text.append("\nDataset consistency test:\n").append(result).append('\n');
162 }
163 }
164 }
165 text.append('\n').append(PluginHandler.getBugReportText()).append('\n');
166
167 appendCollection(text, "Tagging presets", getCustomUrls(TaggingPresetPreference.PresetPrefHelper.INSTANCE));
168 appendCollection(text, "Map paint styles", getCustomUrls(MapPaintPreference.MapPaintPrefHelper.INSTANCE));
169 appendCollection(text, "Validator rules", getCustomUrls(ValidatorTagCheckerRulesPreference.RulePrefHelper.INSTANCE));
170 appendCollection(text, "Last errors/warnings", Main.getLastErrorAndWarnings());
171
172 return text.toString();
173 }
174
175 protected static Collection<String> getCustomUrls(SourceEditor.SourcePrefHelper helper) {
176 Set<String> set = new TreeSet<>();
177 for (SourceEntry entry : helper.get()) {
178 set.add(entry.url);
179 }
180 for (ExtendedSourceEntry def : helper.getDefault()) {
181 set.remove(def.url);
182 }
183 return set;
184 }
185
186 protected static <T> void appendCollection(StringBuilder text, String label, Collection<T> col) {
187 if (!col.isEmpty()) {
188 text.append(label+":\n");
189 for (T o : col) {
190 text.append("- ").append(o.toString()).append('\n');
191 }
192 text.append('\n');
193 }
194 }
195
196 @Override
197 public void actionPerformed(ActionEvent e) {
198 StringBuilder text = new StringBuilder();
199 String reportHeader = getReportHeader();
200 text.append(reportHeader);
201 try {
202 Map<String, Setting<?>> settings = Main.pref.getAllSettings();
203 Set<String> keys = new HashSet<>(settings.keySet());
204 for (String key : keys) {
205 // Remove sensitive information from status report
206 if (key.startsWith("marker.show") || key.contains("username") || key.contains("password") || key.contains("access-token")) {
207 settings.remove(key);
208 }
209 }
210 for (Entry<String, Setting<?>> entry : settings.entrySet()) {
211 text.append(entry.getKey()).append('=').append(entry.getValue().getValue()).append('\n');
212 }
213 } catch (Exception x) {
214 Main.error(x);
215 }
216
217 DebugTextDisplay ta = new DebugTextDisplay(text.toString());
218
219 ExtendedDialog ed = new ExtendedDialog(Main.parent,
220 tr("Status Report"),
221 new String[] {tr("Copy to clipboard and close"), tr("Report bug"), tr("Close") });
222 ed.setButtonIcons(new String[] {"copy", "bug", "cancel" });
223 ed.setContent(ta, false);
224 ed.setMinimumSize(new Dimension(380, 200));
225 ed.setPreferredSize(new Dimension(700, Main.parent.getHeight()-50));
226
227 switch (ed.showDialog().getValue()) {
228 case 1: ta.copyToClippboard(); break;
229 case 2: BugReportSender.reportBug(reportHeader); break;
230 }
231 }
232}
Note: See TracBrowser for help on using the repository browser.