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

Last change on this file since 14397 was 14397, checked in by Don-vip, 5 years ago

fix #16935 - simplify/cleanup help topics of ToggleDialog/ToggleDialogAction

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