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

Last change on this file since 14693 was 14693, checked in by GerdP, 6 years ago

fix #17192 Add tab with installation details in "About" popup

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