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

Last change on this file since 10696 was 10696, checked in by simon04, 8 years ago

Status report: distinguish +active vs. -inactive style/preset/rule entries

  • Property svn:eol-style set to native
File size: 12.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.stream.Collectors;
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.map.MapPaintPreference;
31import org.openstreetmap.josm.gui.preferences.map.TaggingPresetPreference;
32import org.openstreetmap.josm.gui.preferences.validator.ValidatorTagCheckerRulesPreference;
33import org.openstreetmap.josm.io.OsmApi;
34import org.openstreetmap.josm.plugins.PluginHandler;
35import org.openstreetmap.josm.tools.PlatformHookUnixoid;
36import org.openstreetmap.josm.tools.Shortcut;
37import org.openstreetmap.josm.tools.Utils;
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 boolean isRunningJavaWebStart() {
66 try {
67 // See http://stackoverflow.com/a/16200769/2257172
68 return Class.forName("javax.jnlp.ServiceManager") != null;
69 } catch (ClassNotFoundException e) {
70 return false;
71 }
72 }
73
74 /**
75 * Replies the report header (software and system info)
76 * @return The report header (software and system info)
77 */
78 public static String getReportHeader() {
79 StringBuilder text = new StringBuilder(256);
80 String runtimeVersion = System.getProperty("java.runtime.version");
81 text.append(Version.getInstance().getReleaseAttributes())
82 .append("\nIdentification: ").append(Version.getInstance().getAgentString())
83 .append("\nMemory Usage: ")
84 .append(Runtime.getRuntime().totalMemory()/1024/1024)
85 .append(" MB / ")
86 .append(Runtime.getRuntime().maxMemory()/1024/1024)
87 .append(" MB (")
88 .append(Runtime.getRuntime().freeMemory()/1024/1024)
89 .append(" MB allocated, but free)\nJava version: ")
90 .append(runtimeVersion != null ? runtimeVersion : System.getProperty("java.version")).append(", ")
91 .append(System.getProperty("java.vendor")).append(", ")
92 .append(System.getProperty("java.vm.name")).append('\n');
93 if (Main.platform.getClass() == PlatformHookUnixoid.class) {
94 // Add Java package details
95 String packageDetails = ((PlatformHookUnixoid) Main.platform).getJavaPackageDetails();
96 if (packageDetails != null) {
97 text.append("Java package: ")
98 .append(packageDetails)
99 .append('\n');
100 }
101 // Add WebStart package details if run from JNLP
102 if (isRunningJavaWebStart()) {
103 String webStartDetails = ((PlatformHookUnixoid) Main.platform).getWebStartPackageDetails();
104 if (webStartDetails != null) {
105 text.append("WebStart package: ")
106 .append(webStartDetails)
107 .append('\n');
108 }
109 }
110 }
111 try {
112 // Build a new list of VM parameters to modify it below if needed (default implementation returns an UnmodifiableList instance)
113 List<String> vmArguments = new ArrayList<>(ManagementFactory.getRuntimeMXBean().getInputArguments());
114 for (ListIterator<String> it = vmArguments.listIterator(); it.hasNext();) {
115 String value = it.next();
116 if (value.contains("=")) {
117 String[] param = value.split("=");
118 // Hide some parameters for privacy concerns
119 if (param[0].toLowerCase(Locale.ENGLISH).startsWith("-dproxy")) {
120 it.set(param[0]+"=xxx");
121 } else {
122 // Replace some paths for readability and privacy concerns
123 String val = paramCleanup(param[1]);
124 if (!val.equals(param[1])) {
125 it.set(param[0] + '=' + val);
126 }
127 }
128 } else if (value.startsWith("-X")) {
129 // Remove arguments like -Xbootclasspath/a, -Xverify:remote, that can be very long and unhelpful
130 it.remove();
131 }
132 }
133 if (!vmArguments.isEmpty()) {
134 text.append("VM arguments: ").append(vmArguments.toString().replace("\\\\", "\\")).append('\n');
135 }
136 } catch (SecurityException e) {
137 Main.trace(e);
138 }
139 List<String> commandLineArgs = Main.getCommandLineArgs();
140 if (!commandLineArgs.isEmpty()) {
141 text.append("Program arguments: ").append(Arrays.toString(paramCleanup(commandLineArgs).toArray())).append('\n');
142 }
143 if (Main.main != null) {
144 DataSet dataset = Main.getLayerManager().getEditDataSet();
145 if (dataset != null) {
146 String result = DatasetConsistencyTest.runTests(dataset);
147 if (result.isEmpty()) {
148 text.append("Dataset consistency test: No problems found\n");
149 } else {
150 text.append("\nDataset consistency test:\n").append(result).append('\n');
151 }
152 }
153 }
154 text.append("\n");
155 appendCollection(text, "Plugins", Utils.transform(PluginHandler.getBugReportInformation(), i -> "+ " + i));
156 appendCollection(text, "Tagging presets", getCustomUrls(TaggingPresetPreference.PresetPrefHelper.INSTANCE));
157 appendCollection(text, "Map paint styles", getCustomUrls(MapPaintPreference.MapPaintPrefHelper.INSTANCE));
158 appendCollection(text, "Validator rules", getCustomUrls(ValidatorTagCheckerRulesPreference.RulePrefHelper.INSTANCE));
159 appendCollection(text, "Last errors/warnings", Utils.transform(Main.getLastErrorAndWarnings(), i -> "- " + i));
160
161 String osmApi = OsmApi.getOsmApi().getServerUrl();
162 if (!OsmApi.DEFAULT_API_URL.equals(osmApi.trim())) {
163 text.append("OSM API: ").append(osmApi).append("\n\n");
164 }
165
166 return text.toString();
167 }
168
169 private static Collection<String> getCustomUrls(SourceEditor.SourcePrefHelper helper) {
170 final Set<String> defaultUrls = helper.getDefault().stream()
171 .map(i -> i.url)
172 .collect(Collectors.toSet());
173 return helper.get().stream()
174 .filter(i -> !defaultUrls.contains(i.url))
175 .map(i -> (i.active ? "+ " : "- ") + i.url)
176 .collect(Collectors.toList());
177 }
178
179 private static List<String> paramCleanup(Collection<String> params) {
180 List<String> result = new ArrayList<>(params.size());
181 for (String param : params) {
182 result.add(paramCleanup(param));
183 }
184 return result;
185 }
186
187 /**
188 * Shortens and removes private informations from a parameter used for status report.
189 * @param param parameter to cleanup
190 * @return shortened/anonymized parameter
191 */
192 private static String paramCleanup(String param) {
193 final String envJavaHome = System.getenv("JAVA_HOME");
194 final String envJavaHomeAlt = Main.isPlatformWindows() ? "%JAVA_HOME%" : "${JAVA_HOME}";
195 final String propJavaHome = System.getProperty("java.home");
196 final String propJavaHomeAlt = "<java.home>";
197 final String prefDir = Main.pref.getPreferencesDirectory().toString();
198 final String prefDirAlt = "<josm.pref>";
199 final String userDataDir = Main.pref.getUserDataDirectory().toString();
200 final String userDataDirAlt = "<josm.userdata>";
201 final String userCacheDir = Main.pref.getCacheDirectory().toString();
202 final String userCacheDirAlt = "<josm.cache>";
203 final String userHomeDir = System.getProperty("user.home");
204 final String userHomeDirAlt = Main.isPlatformWindows() ? "%UserProfile%" : "${HOME}";
205 final String userName = System.getProperty("user.name");
206 final String userNameAlt = "<user.name>";
207
208 String val = param;
209 val = paramReplace(val, envJavaHome, envJavaHomeAlt);
210 val = paramReplace(val, envJavaHome, envJavaHomeAlt);
211 val = paramReplace(val, propJavaHome, propJavaHomeAlt);
212 val = paramReplace(val, prefDir, prefDirAlt);
213 val = paramReplace(val, userDataDir, userDataDirAlt);
214 val = paramReplace(val, userCacheDir, userCacheDirAlt);
215 val = paramReplace(val, userHomeDir, userHomeDirAlt);
216 val = paramReplace(val, userName, userNameAlt);
217 return val;
218 }
219
220 private static String paramReplace(String str, String target, String replacement) {
221 return target == null ? str : str.replace(target, replacement);
222 }
223
224 private static void appendCollection(StringBuilder text, String label, Collection<String> col) {
225 if (!col.isEmpty()) {
226 text.append(label).append(":\n");
227 for (String o : col) {
228 text.append(paramCleanup(o)).append('\n');
229 }
230 text.append('\n');
231 }
232 }
233
234 @Override
235 public void actionPerformed(ActionEvent e) {
236 StringBuilder text = new StringBuilder();
237 String reportHeader = getReportHeader();
238 text.append(reportHeader);
239 try {
240 Map<String, Setting<?>> settings = Main.pref.getAllSettings();
241 Set<String> keys = new HashSet<>(settings.keySet());
242 for (String key : keys) {
243 // Remove sensitive information from status report
244 if (key.startsWith("marker.show") || key.contains("username") || key.contains("password") || key.contains("access-token")) {
245 settings.remove(key);
246 }
247 }
248 for (Entry<String, Setting<?>> entry : settings.entrySet()) {
249 text.append(paramCleanup(entry.getKey()))
250 .append('=')
251 .append(paramCleanup(entry.getValue().getValue().toString())).append('\n');
252 }
253 } catch (Exception x) {
254 Main.error(x);
255 }
256
257 DebugTextDisplay ta = new DebugTextDisplay(text.toString());
258
259 ExtendedDialog ed = new ExtendedDialog(Main.parent,
260 tr("Status Report"),
261 new String[] {tr("Copy to clipboard and close"), tr("Report bug"), tr("Close") });
262 ed.setButtonIcons(new String[] {"copy", "bug", "cancel" });
263 ed.setContent(ta, false);
264 ed.setMinimumSize(new Dimension(380, 200));
265 ed.setPreferredSize(new Dimension(700, Main.parent.getHeight()-50));
266
267 switch (ed.showDialog().getValue()) {
268 case 1: ta.copyToClippboard(); break;
269 case 2: BugReportSender.reportBug(reportHeader); break;
270 default: // Do nothing
271 }
272 }
273}
Note: See TracBrowser for help on using the repository browser.