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

Last change on this file since 11419 was 11156, checked in by bastiK, 7 years ago

rework/simplify inheritance structure of PlatformHook

PlatformHookUnixoid is no longer base class of PlatformHookWindows
and PlatformHookOsx; move common methods to PlatformHook

  • Property svn:eol-style set to native
File size: 13.5 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.DisplayMode;
9import java.awt.GraphicsEnvironment;
10import java.awt.event.ActionEvent;
11import java.awt.event.KeyEvent;
12import java.lang.management.ManagementFactory;
13import java.util.ArrayList;
14import java.util.Arrays;
15import java.util.Collection;
16import java.util.HashSet;
17import java.util.List;
18import java.util.ListIterator;
19import java.util.Locale;
20import java.util.Map;
21import java.util.Map.Entry;
22import java.util.Set;
23import java.util.stream.Collectors;
24
25import org.openstreetmap.josm.Main;
26import org.openstreetmap.josm.data.Version;
27import org.openstreetmap.josm.data.osm.DataSet;
28import org.openstreetmap.josm.data.osm.DatasetConsistencyTest;
29import org.openstreetmap.josm.data.preferences.Setting;
30import org.openstreetmap.josm.gui.ExtendedDialog;
31import org.openstreetmap.josm.gui.preferences.SourceEditor;
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.gui.util.GuiHelper;
36import org.openstreetmap.josm.io.OsmApi;
37import org.openstreetmap.josm.plugins.PluginHandler;
38import org.openstreetmap.josm.tools.PlatformHookUnixoid;
39import org.openstreetmap.josm.tools.Shortcut;
40import org.openstreetmap.josm.tools.Utils;
41import org.openstreetmap.josm.tools.bugreport.BugReportSender;
42import org.openstreetmap.josm.tools.bugreport.DebugTextDisplay;
43
44/**
45 * @author xeen
46 *
47 * Opens a dialog with useful status information like version numbers for Java, JOSM and plugins
48 * Also includes preferences with stripped username and password
49 */
50public final class ShowStatusReportAction extends JosmAction {
51
52 /**
53 * Constructs a new {@code ShowStatusReportAction}
54 */
55 public ShowStatusReportAction() {
56 super(
57 tr("Show Status Report"),
58 "clock",
59 tr("Show status report with useful information that can be attached to bugs"),
60 Shortcut.registerShortcut("help:showstatusreport", tr("Help: {0}",
61 tr("Show Status Report")), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE), false);
62
63 putValue("help", ht("/Action/ShowStatusReport"));
64 putValue("toolbar", "help/showstatusreport");
65 Main.toolbar.register(this);
66 }
67
68 private static boolean isRunningJavaWebStart() {
69 try {
70 // See http://stackoverflow.com/a/16200769/2257172
71 return Class.forName("javax.jnlp.ServiceManager") != null;
72 } catch (ClassNotFoundException e) {
73 return false;
74 }
75 }
76
77 /**
78 * Replies the report header (software and system info)
79 * @return The report header (software and system info)
80 */
81 public static String getReportHeader() {
82 StringBuilder text = new StringBuilder(256);
83 String runtimeVersion = System.getProperty("java.runtime.version");
84 text.append(Version.getInstance().getReleaseAttributes())
85 .append("\nIdentification: ").append(Version.getInstance().getAgentString())
86 .append("\nMemory Usage: ")
87 .append(Runtime.getRuntime().totalMemory()/1024/1024)
88 .append(" MB / ")
89 .append(Runtime.getRuntime().maxMemory()/1024/1024)
90 .append(" MB (")
91 .append(Runtime.getRuntime().freeMemory()/1024/1024)
92 .append(" MB allocated, but free)\nJava version: ")
93 .append(runtimeVersion != null ? runtimeVersion : System.getProperty("java.version")).append(", ")
94 .append(System.getProperty("java.vendor")).append(", ")
95 .append(System.getProperty("java.vm.name"))
96 .append("\nScreen: ");
97 if (!GraphicsEnvironment.isHeadless()) {
98 text.append(Arrays.stream(GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()).map(gd -> {
99 StringBuilder b = new StringBuilder(gd.getIDstring());
100 DisplayMode dm = gd.getDisplayMode();
101 if (dm != null) {
102 b.append(' ').append(dm.getWidth()).append('x').append(dm.getHeight());
103 }
104 return b.toString();
105 }).collect(Collectors.joining(", ")));
106 }
107 Dimension maxScreenSize = GuiHelper.getMaximumScreenSize();
108 text.append("\nMaximum Screen Size: ")
109 .append((int) maxScreenSize.getWidth()).append('x')
110 .append((int) maxScreenSize.getHeight()).append('\n');
111
112 if (Main.platform instanceof PlatformHookUnixoid) {
113 // Add Java package details
114 String packageDetails = ((PlatformHookUnixoid) Main.platform).getJavaPackageDetails();
115 if (packageDetails != null) {
116 text.append("Java package: ")
117 .append(packageDetails)
118 .append('\n');
119 }
120 // Add WebStart package details if run from JNLP
121 if (isRunningJavaWebStart()) {
122 String webStartDetails = ((PlatformHookUnixoid) Main.platform).getWebStartPackageDetails();
123 if (webStartDetails != null) {
124 text.append("WebStart package: ")
125 .append(webStartDetails)
126 .append('\n');
127 }
128 }
129 // Add Gnome Atk wrapper details if found
130 String atkWrapperDetails = ((PlatformHookUnixoid) Main.platform).getAtkWrapperPackageDetails();
131 if (atkWrapperDetails != null) {
132 text.append("Java ATK Wrapper package: ")
133 .append(atkWrapperDetails)
134 .append('\n');
135 }
136 }
137 try {
138 // Build a new list of VM parameters to modify it below if needed (default implementation returns an UnmodifiableList instance)
139 List<String> vmArguments = new ArrayList<>(ManagementFactory.getRuntimeMXBean().getInputArguments());
140 for (ListIterator<String> it = vmArguments.listIterator(); it.hasNext();) {
141 String value = it.next();
142 if (value.contains("=")) {
143 String[] param = value.split("=");
144 // Hide some parameters for privacy concerns
145 if (param[0].toLowerCase(Locale.ENGLISH).startsWith("-dproxy")) {
146 it.set(param[0]+"=xxx");
147 } else {
148 // Replace some paths for readability and privacy concerns
149 String val = paramCleanup(param[1]);
150 if (!val.equals(param[1])) {
151 it.set(param[0] + '=' + val);
152 }
153 }
154 } else if (value.startsWith("-X")) {
155 // Remove arguments like -Xbootclasspath/a, -Xverify:remote, that can be very long and unhelpful
156 it.remove();
157 }
158 }
159 if (!vmArguments.isEmpty()) {
160 text.append("VM arguments: ").append(vmArguments.toString().replace("\\\\", "\\")).append('\n');
161 }
162 } catch (SecurityException e) {
163 Main.trace(e);
164 }
165 List<String> commandLineArgs = Main.getCommandLineArgs();
166 if (!commandLineArgs.isEmpty()) {
167 text.append("Program arguments: ").append(Arrays.toString(paramCleanup(commandLineArgs).toArray())).append('\n');
168 }
169 if (Main.main != null) {
170 DataSet dataset = Main.getLayerManager().getEditDataSet();
171 if (dataset != null) {
172 String result = DatasetConsistencyTest.runTests(dataset);
173 if (result.isEmpty()) {
174 text.append("Dataset consistency test: No problems found\n");
175 } else {
176 text.append("\nDataset consistency test:\n").append(result).append('\n');
177 }
178 }
179 }
180 text.append('\n');
181 appendCollection(text, "Plugins", Utils.transform(PluginHandler.getBugReportInformation(), i -> "+ " + i));
182 appendCollection(text, "Tagging presets", getCustomUrls(TaggingPresetPreference.PresetPrefHelper.INSTANCE));
183 appendCollection(text, "Map paint styles", getCustomUrls(MapPaintPreference.MapPaintPrefHelper.INSTANCE));
184 appendCollection(text, "Validator rules", getCustomUrls(ValidatorTagCheckerRulesPreference.RulePrefHelper.INSTANCE));
185 appendCollection(text, "Last errors/warnings", Utils.transform(Main.getLastErrorAndWarnings(), i -> "- " + i));
186
187 String osmApi = OsmApi.getOsmApi().getServerUrl();
188 if (!OsmApi.DEFAULT_API_URL.equals(osmApi.trim())) {
189 text.append("OSM API: ").append(osmApi).append("\n\n");
190 }
191
192 return text.toString();
193 }
194
195 private static Collection<String> getCustomUrls(SourceEditor.SourcePrefHelper helper) {
196 final Set<String> defaultUrls = helper.getDefault().stream()
197 .map(i -> i.url)
198 .collect(Collectors.toSet());
199 return helper.get().stream()
200 .filter(i -> !defaultUrls.contains(i.url))
201 .map(i -> (i.active ? "+ " : "- ") + i.url)
202 .collect(Collectors.toList());
203 }
204
205 private static List<String> paramCleanup(Collection<String> params) {
206 List<String> result = new ArrayList<>(params.size());
207 for (String param : params) {
208 result.add(paramCleanup(param));
209 }
210 return result;
211 }
212
213 /**
214 * Shortens and removes private informations from a parameter used for status report.
215 * @param param parameter to cleanup
216 * @return shortened/anonymized parameter
217 */
218 private static String paramCleanup(String param) {
219 final String envJavaHome = System.getenv("JAVA_HOME");
220 final String envJavaHomeAlt = Main.isPlatformWindows() ? "%JAVA_HOME%" : "${JAVA_HOME}";
221 final String propJavaHome = System.getProperty("java.home");
222 final String propJavaHomeAlt = "<java.home>";
223 final String prefDir = Main.pref.getPreferencesDirectory().toString();
224 final String prefDirAlt = "<josm.pref>";
225 final String userDataDir = Main.pref.getUserDataDirectory().toString();
226 final String userDataDirAlt = "<josm.userdata>";
227 final String userCacheDir = Main.pref.getCacheDirectory().toString();
228 final String userCacheDirAlt = "<josm.cache>";
229 final String userHomeDir = System.getProperty("user.home");
230 final String userHomeDirAlt = Main.isPlatformWindows() ? "%UserProfile%" : "${HOME}";
231 final String userName = System.getProperty("user.name");
232 final String userNameAlt = "<user.name>";
233
234 String val = param;
235 val = paramReplace(val, envJavaHome, envJavaHomeAlt);
236 val = paramReplace(val, envJavaHome, envJavaHomeAlt);
237 val = paramReplace(val, propJavaHome, propJavaHomeAlt);
238 val = paramReplace(val, prefDir, prefDirAlt);
239 val = paramReplace(val, userDataDir, userDataDirAlt);
240 val = paramReplace(val, userCacheDir, userCacheDirAlt);
241 val = paramReplace(val, userHomeDir, userHomeDirAlt);
242 if (userName.length() >= 3) {
243 val = paramReplace(val, userName, userNameAlt);
244 }
245 return val;
246 }
247
248 private static String paramReplace(String str, String target, String replacement) {
249 return target == null ? str : str.replace(target, replacement);
250 }
251
252 private static void appendCollection(StringBuilder text, String label, Collection<String> col) {
253 if (!col.isEmpty()) {
254 text.append(label).append(":\n");
255 for (String o : col) {
256 text.append(paramCleanup(o)).append('\n');
257 }
258 text.append('\n');
259 }
260 }
261
262 @Override
263 public void actionPerformed(ActionEvent e) {
264 StringBuilder text = new StringBuilder();
265 String reportHeader = getReportHeader();
266 text.append(reportHeader);
267 try {
268 Map<String, Setting<?>> settings = Main.pref.getAllSettings();
269 Set<String> keys = new HashSet<>(settings.keySet());
270 for (String key : keys) {
271 // Remove sensitive information from status report
272 if (key.startsWith("marker.show") || key.contains("username") || key.contains("password") || key.contains("access-token")) {
273 settings.remove(key);
274 }
275 }
276 for (Entry<String, Setting<?>> entry : settings.entrySet()) {
277 text.append(paramCleanup(entry.getKey()))
278 .append('=')
279 .append(paramCleanup(entry.getValue().getValue().toString())).append('\n');
280 }
281 } catch (Exception x) {
282 Main.error(x);
283 }
284
285 DebugTextDisplay ta = new DebugTextDisplay(text.toString());
286
287 ExtendedDialog ed = new ExtendedDialog(Main.parent,
288 tr("Status Report"),
289 new String[] {tr("Copy to clipboard and close"), tr("Report bug"), tr("Close") });
290 ed.setButtonIcons(new String[] {"copy", "bug", "cancel" });
291 ed.setContent(ta, false);
292 ed.setMinimumSize(new Dimension(380, 200));
293 ed.setPreferredSize(new Dimension(700, Main.parent.getHeight()-50));
294
295 switch (ed.showDialog().getValue()) {
296 case 1: ta.copyToClipboard(); break;
297 case 2: BugReportSender.reportBug(reportHeader); break;
298 default: // Do nothing
299 }
300 }
301}
Note: See TracBrowser for help on using the repository browser.