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

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

see #15229 - deprecate Main.pref

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