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

Last change on this file since 17637 was 17637, checked in by simon04, 3 years ago

fix #20647 - Add --status-report command line argument

  • Property svn:eol-style set to native
File size: 16.2 KB
RevLine 
[1417]1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
[2500]4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
[1417]5import static org.openstreetmap.josm.tools.I18n.tr;
[13647]6import static org.openstreetmap.josm.tools.Utils.getSystemEnv;
7import static org.openstreetmap.josm.tools.Utils.getSystemProperty;
[1417]8
9import java.awt.Dimension;
[10929]10import java.awt.DisplayMode;
[17337]11import java.awt.GraphicsDevice;
[10929]12import java.awt.GraphicsEnvironment;
[16580]13import java.awt.Toolkit;
[1417]14import java.awt.event.ActionEvent;
15import java.awt.event.KeyEvent;
[16580]16import java.awt.geom.AffineTransform;
[17337]17import java.io.PrintWriter;
18import java.io.StringWriter;
[5831]19import java.lang.management.ManagementFactory;
[5840]20import java.util.ArrayList;
[5831]21import java.util.Arrays;
[7420]22import java.util.Collection;
[14701]23import java.util.LinkedHashMap;
[5831]24import java.util.List;
[5840]25import java.util.ListIterator;
[8404]26import java.util.Locale;
[14701]27import java.util.Map;
28import java.util.Map.Entry;
[16531]29import java.util.Optional;
[4635]30import java.util.Set;
[10696]31import java.util.stream.Collectors;
[1417]32
[16531]33import javax.swing.UIManager;
34
[14149]35import org.openstreetmap.josm.data.Preferences;
[2358]36import org.openstreetmap.josm.data.Version;
[2500]37import org.openstreetmap.josm.data.osm.DataSet;
38import org.openstreetmap.josm.data.osm.DatasetConsistencyTest;
[12649]39import org.openstreetmap.josm.data.preferences.sources.MapPaintPrefHelper;
40import org.openstreetmap.josm.data.preferences.sources.PresetPrefHelper;
[12846]41import org.openstreetmap.josm.data.preferences.sources.SourcePrefHelper;
[12649]42import org.openstreetmap.josm.data.preferences.sources.ValidatorPrefHelper;
[1417]43import org.openstreetmap.josm.gui.ExtendedDialog;
[11650]44import org.openstreetmap.josm.gui.MainApplication;
[12649]45import org.openstreetmap.josm.gui.bugreport.DebugTextDisplay;
[10929]46import org.openstreetmap.josm.gui.util.GuiHelper;
[10226]47import org.openstreetmap.josm.io.OsmApi;
[1417]48import org.openstreetmap.josm.plugins.PluginHandler;
[12855]49import org.openstreetmap.josm.spi.preferences.Config;
[12620]50import org.openstreetmap.josm.tools.Logging;
[6103]51import org.openstreetmap.josm.tools.PlatformHookUnixoid;
[14138]52import org.openstreetmap.josm.tools.PlatformManager;
[1417]53import org.openstreetmap.josm.tools.Shortcut;
[10696]54import org.openstreetmap.josm.tools.Utils;
[10055]55import org.openstreetmap.josm.tools.bugreport.BugReportSender;
[1417]56
57/**
[12581]58 * Opens a dialog with useful status information like version numbers for Java, JOSM and plugins
59 * Also includes preferences with stripped username and password.
60 *
[1417]61 * @author xeen
62 */
63public final class ShowStatusReportAction extends JosmAction {
[6069]64
[5840]65 /**
[17637]66 * Localized description text for this action
67 */
68 public static final String ACTION_DESCRIPTION = tr("Show status report with useful information that can be attached to bugs");
69
70 /**
[5840]71 * Constructs a new {@code ShowStatusReportAction}
72 */
[1417]73 public ShowStatusReportAction() {
74 super(
75 tr("Show Status Report"),
[16827]76 "misc/statusreport",
[17637]77 ACTION_DESCRIPTION,
[1417]78 Shortcut.registerShortcut("help:showstatusreport", tr("Help: {0}",
[16509]79 tr("Show Status Report")), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE), true, "help/showstatusreport", false);
[1417]80
[14397]81 setHelpId(ht("/Action/ShowStatusReport"));
[1417]82 }
83
[5873]84 /**
85 * Replies the report header (software and system info)
86 * @return The report header (software and system info)
87 */
[6850]88 public static String getReportHeader() {
[17337]89 StringWriter stringWriter = new StringWriter(256);
90 PrintWriter text = new PrintWriter(stringWriter);
[13647]91 String runtimeVersion = getSystemProperty("java.runtime.version");
[17337]92 text.println(Version.getInstance().getReleaseAttributes());
93 text.format("Identification: %s%n", Version.getInstance().getAgentString());
[14138]94 String buildNumber = PlatformManager.getPlatform().getOSBuildNumber();
[12217]95 if (!buildNumber.isEmpty()) {
[17337]96 text.format("OS Build number: %s%n", buildNumber);
[12217]97 }
[17337]98 text.format("Memory Usage: %d MB / %d MB (%d MB allocated, but free)%n",
99 Runtime.getRuntime().totalMemory() / 1024 / 1024,
100 Runtime.getRuntime().maxMemory() / 1024 / 1024,
101 Runtime.getRuntime().freeMemory() / 1024 / 1024);
102 text.format("Java version: %s, %s, %s%n",
103 runtimeVersion != null ? runtimeVersion : getSystemProperty("java.version"),
104 getSystemProperty("java.vendor"),
105 getSystemProperty("java.vm.name"));
106 text.format("Look and Feel: %s%n",
107 Optional.ofNullable(UIManager.getLookAndFeel()).map(laf -> laf.getClass().getName()).orElse("null"));
[10932]108 if (!GraphicsEnvironment.isHeadless()) {
[17337]109 text.append("Screen:");
110 for (GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()) {
111 text.append(" ").append(gd.getIDstring());
112 DisplayMode dm = gd.getDisplayMode();
113 if (dm != null) {
114 AffineTransform transform = gd.getDefaultConfiguration().getDefaultTransform();
115 // Java 11: use DisplayMode#toString
116 text.format(" %d\u00D7%d (scaling %.2f\u00D7%.2f)",
117 dm.getWidth(), dm.getHeight(), transform.getScaleX(), transform.getScaleY());
118 }
119 }
120 text.println();
[10929]121 }
[17337]122 text.format("Maximum Screen Size: %s%n", toString(GuiHelper.getMaximumScreenSize()));
[16580]123 if (!GraphicsEnvironment.isHeadless()) {
124 Dimension bestCursorSize16 = Toolkit.getDefaultToolkit().getBestCursorSize(16, 16);
125 Dimension bestCursorSize32 = Toolkit.getDefaultToolkit().getBestCursorSize(32, 32);
[17337]126 text.format("Best cursor sizes: %s→%s, %s→%s%n",
127 toString(new Dimension(16, 16)), toString(bestCursorSize16),
128 toString(new Dimension(32, 32)), toString(bestCursorSize32));
[16580]129 }
[10929]130
[14138]131 if (PlatformManager.isPlatformUnixoid()) {
132 PlatformHookUnixoid platform = (PlatformHookUnixoid) PlatformManager.getPlatform();
[16911]133 // Add desktop environment
[17337]134 platform.getDesktopEnvironment().ifPresent(desktop -> text.format("Desktop environment: %s%n", desktop));
[7318]135 // Add Java package details
[14138]136 String packageDetails = platform.getJavaPackageDetails();
[6103]137 if (packageDetails != null) {
[17337]138 text.format("Java package: %s%n", packageDetails);
[6103]139 }
[7318]140 // Add WebStart package details if run from JNLP
[15740]141 if (Utils.isRunningJavaWebStart()) {
[14138]142 String webStartDetails = platform.getWebStartPackageDetails();
[6850]143 if (webStartDetails != null) {
[17337]144 text.format("WebStart package: %s%n", webStartDetails);
[6850]145 }
146 }
[10734]147 // Add Gnome Atk wrapper details if found
[14138]148 String atkWrapperDetails = platform.getAtkWrapperPackageDetails();
[10734]149 if (atkWrapperDetails != null) {
[17337]150 text.format("Java ATK Wrapper package: %s%n", atkWrapperDetails);
[10734]151 }
[17203]152 String lang = System.getenv("LANG");
[17210]153 if (lang != null) {
[17337]154 text.format("Environment variable LANG: %s%n", lang);
[17203]155 }
[15206]156 // Add dependencies details if found
157 for (String p : new String[] {
158 "apache-commons-compress", "libcommons-compress-java",
159 "apache-commons-jcs-core",
160 "apache-commons-logging", "libcommons-logging-java",
161 "fonts-noto",
162 "jsonp",
163 "metadata-extractor2",
164 "signpost-core", "liboauth-signpost-java",
165 "svgsalamander"
166 }) {
167 String details = PlatformHookUnixoid.getPackageDetails(p);
168 if (details != null) {
[17337]169 text.format("%s: %s%n", p, details);
[15206]170 }
171 }
[6103]172 }
[5831]173 try {
[5840]174 // Build a new list of VM parameters to modify it below if needed (default implementation returns an UnmodifiableList instance)
[7005]175 List<String> vmArguments = new ArrayList<>(ManagementFactory.getRuntimeMXBean().getInputArguments());
[8465]176 for (ListIterator<String> it = vmArguments.listIterator(); it.hasNext();) {
[5840]177 String value = it.next();
[5992]178 if (value.contains("=")) {
[15849]179 String[] param = value.split("=", 2);
[5992]180 // Hide some parameters for privacy concerns
[8404]181 if (param[0].toLowerCase(Locale.ENGLISH).startsWith("-dproxy")) {
[5992]182 it.set(param[0]+"=xxx");
[11498]183 } else if ("-Djnlpx.vmargs".equals(param[0])) {
184 // Remove jnlpx.vmargs (base64 encoded copy of VM arguments already included in clear)
185 it.remove();
[5992]186 } else {
[10138]187 // Replace some paths for readability and privacy concerns
[10158]188 String val = paramCleanup(param[1]);
[10138]189 if (!val.equals(param[1])) {
190 it.set(param[0] + '=' + val);
191 }
[5992]192 }
[7420]193 } else if (value.startsWith("-X")) {
194 // Remove arguments like -Xbootclasspath/a, -Xverify:remote, that can be very long and unhelpful
195 it.remove();
[5840]196 }
197 }
[5831]198 if (!vmArguments.isEmpty()) {
[17337]199 text.format("VM arguments: %s%n", vmArguments.toString().replace("\\\\", "\\"));
[5831]200 }
201 } catch (SecurityException e) {
[12620]202 Logging.trace(e);
[5831]203 }
[11650]204 List<String> commandLineArgs = MainApplication.getCommandLineArgs();
[8356]205 if (!commandLineArgs.isEmpty()) {
[17337]206 text.format("Program arguments: %s%n", Arrays.toString(paramCleanup(commandLineArgs).toArray()));
[5831]207 }
[14143]208 DataSet dataset = MainApplication.getLayerManager().getActiveDataSet();
209 if (dataset != null) {
210 String result = DatasetConsistencyTest.runTests(dataset);
211 if (result.isEmpty()) {
[17337]212 text.println("Dataset consistency test: No problems found");
[14143]213 } else {
[17337]214 text.println();
215 text.println("Dataset consistency test:");
216 text.println(result);
[2500]217 }
218 }
[17337]219 text.println();
[10696]220 appendCollection(text, "Plugins", Utils.transform(PluginHandler.getBugReportInformation(), i -> "+ " + i));
[12649]221 appendCollection(text, "Tagging presets", getCustomUrls(PresetPrefHelper.INSTANCE));
222 appendCollection(text, "Map paint styles", getCustomUrls(MapPaintPrefHelper.INSTANCE));
223 appendCollection(text, "Validator rules", getCustomUrls(ValidatorPrefHelper.INSTANCE));
[12620]224 appendCollection(text, "Last errors/warnings", Utils.transform(Logging.getLastErrorAndWarnings(), i -> "- " + i));
[10063]225
[10226]226 String osmApi = OsmApi.getOsmApi().getServerUrl();
[14119]227 if (!Config.getUrls().getDefaultOsmApiUrl().equals(osmApi.trim())) {
[17337]228 text.format("OSM API: %s%n", osmApi);
[10226]229 }
230
[17337]231 text.println();
232 return stringWriter.toString();
[10063]233 }
234
[17337]235 private static String toString(Dimension dimension) {
236 return dimension.width + "\u00D7" + dimension.height;
237 }
238
[12649]239 private static Collection<String> getCustomUrls(SourcePrefHelper helper) {
[10696]240 final Set<String> defaultUrls = helper.getDefault().stream()
241 .map(i -> i.url)
242 .collect(Collectors.toSet());
243 return helper.get().stream()
244 .filter(i -> !defaultUrls.contains(i.url))
245 .map(i -> (i.active ? "+ " : "- ") + i.url)
246 .collect(Collectors.toList());
[10063]247 }
248
[10158]249 private static List<String> paramCleanup(Collection<String> params) {
[16438]250 return params.stream()
251 .map(ShowStatusReportAction::paramCleanup)
252 .collect(Collectors.toList());
[10158]253 }
254
255 /**
[14701]256 * Fill map with anonymized name to the actual used path.
257 * @return map that maps shortened name to full directory path
258 */
259 static Map<String, String> getAnonimicDirectorySymbolMap() {
260 /** maps the anonymized name to the actual used path */
261 Map<String, String> map = new LinkedHashMap<>();
262 map.put(PlatformManager.isPlatformWindows() ? "%JAVA_HOME%" : "${JAVA_HOME}", getSystemEnv("JAVA_HOME"));
263 map.put("<java.home>", getSystemProperty("java.home"));
264 map.put("<josm.pref>", Config.getDirs().getPreferencesDirectory(false).toString());
265 map.put("<josm.userdata>", Config.getDirs().getUserDataDirectory(false).toString());
266 map.put("<josm.cache>", Config.getDirs().getCacheDirectory(false).toString());
267 map.put(PlatformManager.isPlatformWindows() ? "%UserProfile%" : "${HOME}", getSystemProperty("user.home"));
268 return map;
269 }
270
271 /**
[10158]272 * Shortens and removes private informations from a parameter used for status report.
273 * @param param parameter to cleanup
274 * @return shortened/anonymized parameter
275 */
[14693]276 static String paramCleanup(String param) {
[13647]277 final String userName = getSystemProperty("user.name");
[10163]278 final String userNameAlt = "<user.name>";
[10158]279
280 String val = param;
[14701]281 for (Entry<String, String> entry : getAnonimicDirectorySymbolMap().entrySet()) {
282 val = paramReplace(val, entry.getValue(), entry.getKey());
283 }
[13647]284 if (userName != null && userName.length() >= 3) {
[10796]285 val = paramReplace(val, userName, userNameAlt);
286 }
[10158]287 return val;
288 }
289
[10138]290 private static String paramReplace(String str, String target, String replacement) {
[10158]291 return target == null ? str : str.replace(target, replacement);
[10138]292 }
293
[17337]294 private static void appendCollection(PrintWriter text, String label, Collection<String> col) {
[10063]295 if (!col.isEmpty()) {
[16438]296 text.append(col.stream().map(o -> paramCleanup(o) + '\n')
297 .collect(Collectors.joining("", label + ":\n", "\n")));
[7420]298 }
[1674]299 }
300
[16579]301 private static String valueCleanup(Object value) {
302 String valueString = value.toString();
303 if (valueString.length() > 512 && value instanceof Collection<?>) {
304 valueString = ((Collection<?>) value).stream().map(v -> {
305 if (v instanceof Map<?, ?>) {
306 LinkedHashMap<Object, Object> map = new LinkedHashMap<>(((Map<?, ?>) v));
307 map.computeIfPresent("icon", (k, icon) -> Utils.shortenString(icon.toString(), 32)); // see #19058
308 return map.toString();
309 } else {
310 return String.valueOf(v);
311 }
312 }).collect(Collectors.joining(",\n ", "[", "\n]"));
313 }
314 return paramCleanup(valueString);
315 }
316
[6084]317 @Override
[1674]318 public void actionPerformed(ActionEvent e) {
319 StringBuilder text = new StringBuilder();
[5849]320 String reportHeader = getReportHeader();
321 text.append(reportHeader);
[14671]322
323 Preferences.main().getAllSettings().forEach((key, setting) -> {
[15496]324 if ("file-open.history".equals(key)
[14706]325 || "download.overpass.query".equals(key)
326 || "download.overpass.queries".equals(key)
[14671]327 || key.contains("username")
328 || key.contains("password")
329 || key.contains("access-token")) {
330 // Remove sensitive information from status report
331 return;
[1417]332 }
[14671]333 text.append(paramCleanup(key))
334 .append('=')
[16579]335 .append(valueCleanup(setting.getValue()))
[14671]336 .append('\n');
337 });
[1484]338
[10055]339 DebugTextDisplay ta = new DebugTextDisplay(text.toString());
[1417]340
[14153]341 ExtendedDialog ed = new ExtendedDialog(MainApplication.getMainFrame(),
[2039]342 tr("Status Report"),
[12279]343 tr("Copy to clipboard and close"), tr("Report bug"), tr("Close"));
344 ed.setButtonIcons("copy", "bug", "cancel");
[16883]345 ed.configureContextsensitiveHelp("/Action/ShowStatusReport", true);
[10055]346 ed.setContent(ta, false);
[5833]347 ed.setMinimumSize(new Dimension(380, 200));
[14153]348 ed.setPreferredSize(new Dimension(700, MainApplication.getMainFrame().getHeight()-50));
[1484]349
[5849]350 switch (ed.showDialog().getValue()) {
[11102]351 case 1: ta.copyToClipboard(); break;
[10055]352 case 2: BugReportSender.reportBug(reportHeader); break;
[14701]353 default: // do nothing
[5849]354 }
[14701]355 GuiHelper.destroyComponents(ed, false);
[1417]356 }
357}
Note: See TracBrowser for help on using the repository browser.