001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.actions; 003 004import static org.openstreetmap.josm.gui.help.HelpUtil.ht; 005import static org.openstreetmap.josm.tools.I18n.tr; 006import static org.openstreetmap.josm.tools.Utils.getSystemEnv; 007import static org.openstreetmap.josm.tools.Utils.getSystemProperty; 008 009import java.awt.Dimension; 010import java.awt.DisplayMode; 011import java.awt.GraphicsDevice; 012import java.awt.GraphicsEnvironment; 013import java.awt.Toolkit; 014import java.awt.event.ActionEvent; 015import java.awt.event.KeyEvent; 016import java.awt.geom.AffineTransform; 017import java.io.PrintWriter; 018import java.io.StringWriter; 019import java.lang.management.ManagementFactory; 020import java.util.ArrayList; 021import java.util.Arrays; 022import java.util.Collection; 023import java.util.LinkedHashMap; 024import java.util.List; 025import java.util.ListIterator; 026import java.util.Locale; 027import java.util.Map; 028import java.util.Map.Entry; 029import java.util.Optional; 030import java.util.Set; 031import java.util.stream.Collectors; 032 033import javax.swing.UIManager; 034 035import org.openstreetmap.josm.data.Preferences; 036import org.openstreetmap.josm.data.Version; 037import org.openstreetmap.josm.data.osm.DataSet; 038import org.openstreetmap.josm.data.osm.DatasetConsistencyTest; 039import org.openstreetmap.josm.data.preferences.sources.MapPaintPrefHelper; 040import org.openstreetmap.josm.data.preferences.sources.PresetPrefHelper; 041import org.openstreetmap.josm.data.preferences.sources.SourcePrefHelper; 042import org.openstreetmap.josm.data.preferences.sources.ValidatorPrefHelper; 043import org.openstreetmap.josm.gui.ExtendedDialog; 044import org.openstreetmap.josm.gui.MainApplication; 045import org.openstreetmap.josm.gui.bugreport.DebugTextDisplay; 046import org.openstreetmap.josm.gui.util.GuiHelper; 047import org.openstreetmap.josm.io.OsmApi; 048import org.openstreetmap.josm.plugins.PluginHandler; 049import org.openstreetmap.josm.spi.preferences.Config; 050import org.openstreetmap.josm.tools.Logging; 051import org.openstreetmap.josm.tools.PlatformHookUnixoid; 052import org.openstreetmap.josm.tools.PlatformManager; 053import org.openstreetmap.josm.tools.Shortcut; 054import org.openstreetmap.josm.tools.Utils; 055import org.openstreetmap.josm.tools.bugreport.BugReportSender; 056 057/** 058 * Opens a dialog with useful status information like version numbers for Java, JOSM and plugins 059 * Also includes preferences with stripped username and password. 060 * 061 * @author xeen 062 */ 063public final class ShowStatusReportAction extends JosmAction { 064 065 /** 066 * Localized description text for this action 067 */ 068 public static final String ACTION_DESCRIPTION = tr("Show status report with useful information that can be attached to bugs"); 069 070 /** 071 * Constructs a new {@code ShowStatusReportAction} 072 */ 073 public ShowStatusReportAction() { 074 super( 075 tr("Show Status Report"), 076 "misc/statusreport", 077 ACTION_DESCRIPTION, 078 Shortcut.registerShortcut("help:showstatusreport", tr("Help: {0}", 079 tr("Show Status Report")), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE), true, "help/showstatusreport", false); 080 081 setHelpId(ht("/Action/ShowStatusReport")); 082 } 083 084 /** 085 * Replies the report header (software and system info) 086 * @return The report header (software and system info) 087 */ 088 public static String getReportHeader() { 089 StringWriter stringWriter = new StringWriter(256); 090 PrintWriter text = new PrintWriter(stringWriter); 091 String runtimeVersion = getSystemProperty("java.runtime.version"); 092 text.println(Version.getInstance().getReleaseAttributes()); 093 text.format("Identification: %s%n", Version.getInstance().getAgentString()); 094 String buildNumber = PlatformManager.getPlatform().getOSBuildNumber(); 095 if (!buildNumber.isEmpty()) { 096 text.format("OS Build number: %s%n", buildNumber); 097 } 098 text.format(Locale.ROOT, "Memory Usage: %d MB / %d MB (%d MB allocated, but free)%n", 099 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")); 108 if (!GraphicsEnvironment.isHeadless()) { 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 text.format(Locale.ROOT, " %s (scaling %.2f\u00D7%.2f)", 116 dm, transform.getScaleX(), transform.getScaleY()); 117 } 118 } 119 text.println(); 120 } 121 text.format("Maximum Screen Size: %s%n", toString(GuiHelper.getMaximumScreenSize())); 122 if (!GraphicsEnvironment.isHeadless()) { 123 Dimension bestCursorSize16 = Toolkit.getDefaultToolkit().getBestCursorSize(16, 16); 124 Dimension bestCursorSize32 = Toolkit.getDefaultToolkit().getBestCursorSize(32, 32); 125 text.format("Best cursor sizes: %s→%s, %s→%s%n", 126 toString(new Dimension(16, 16)), toString(bestCursorSize16), 127 toString(new Dimension(32, 32)), toString(bestCursorSize32)); 128 } 129 130 for (String name : Arrays.asList("LANG", "LC_ALL")) { 131 String value = getSystemEnv(name); 132 if (value != null) { 133 text.format("Environment variable %s: %s%n", name, value); 134 } 135 } 136 for (String name : Arrays.asList("file.encoding", "sun.jnu.encoding")) { 137 String value = getSystemProperty(name); 138 if (value != null) { 139 text.format("System property %s: %s%n", name, value); 140 } 141 } 142 text.format("Locale info: %s%n", Locale.getDefault().toString()); 143 text.format("Numbers with default locale: %s -> %d%n", Integer.toString(1_234_567_890), 1_234_567_890); 144 145 if (PlatformManager.isPlatformUnixoid()) { 146 PlatformHookUnixoid platform = (PlatformHookUnixoid) PlatformManager.getPlatform(); 147 // Add desktop environment 148 platform.getDesktopEnvironment().ifPresent(desktop -> text.format("Desktop environment: %s%n", desktop)); 149 // Add Java package details 150 String packageDetails = platform.getJavaPackageDetails(); 151 if (packageDetails != null) { 152 text.format("Java package: %s%n", packageDetails); 153 } 154 // Add WebStart package details if run from JNLP 155 if (Utils.isRunningWebStart()) { 156 String webStartDetails = platform.getWebStartPackageDetails(); 157 if (webStartDetails != null) { 158 text.format("WebStart package: %s%n", webStartDetails); 159 } 160 } 161 // Add Gnome ATK wrapper details if found 162 String atkWrapperDetails = platform.getAtkWrapperPackageDetails(); 163 if (atkWrapperDetails != null) { 164 text.format("Java ATK Wrapper package: %s%n", atkWrapperDetails); 165 } 166 // Add dependency details if found 167 for (String p : new String[] { 168 "apache-commons-compress", "libcommons-compress-java", 169 "apache-commons-jcs-core", 170 "apache-commons-logging", "libcommons-logging-java", 171 "fonts-noto", 172 "jsonp", 173 "metadata-extractor2", 174 "signpost-core", "liboauth-signpost-java", 175 "jsvg" 176 }) { 177 String details = PlatformHookUnixoid.getPackageDetails(p); 178 if (details != null) { 179 text.format("%s: %s%n", p, details); 180 } 181 } 182 } 183 try { 184 // Build a new list of VM parameters to modify it below if needed (default implementation returns an UnmodifiableList instance) 185 List<String> vmArguments = new ArrayList<>(ManagementFactory.getRuntimeMXBean().getInputArguments()); 186 for (ListIterator<String> it = vmArguments.listIterator(); it.hasNext();) { 187 String value = it.next(); 188 if (value.contains("=")) { 189 String[] param = value.split("=", 2); 190 // Hide some parameters for privacy concerns 191 if (param[0].toLowerCase(Locale.ENGLISH).startsWith("-dproxy")) { 192 it.set(param[0]+"=xxx"); 193 } else if ("-Djnlpx.vmargs".equals(param[0])) { 194 // Remove jnlpx.vmargs (base64 encoded copy of VM arguments already included in clear) 195 it.remove(); 196 } else { 197 // Replace some paths for readability and privacy concerns 198 String val = paramCleanup(param[1]); 199 if (!val.equals(param[1])) { 200 it.set(param[0] + '=' + val); 201 } 202 } 203 } else if (value.startsWith("-X")) { 204 // Remove arguments like -Xbootclasspath/a, -Xverify:remote, that can be very long and unhelpful 205 it.remove(); 206 } 207 } 208 if (!vmArguments.isEmpty()) { 209 text.format("VM arguments: %s%n", paramCleanup(vmArguments).toString().replace("\\\\", "\\")); 210 } 211 } catch (SecurityException e) { 212 Logging.trace(e); 213 } 214 List<String> commandLineArgs = MainApplication.getCommandLineArgs(); 215 if (!commandLineArgs.isEmpty()) { 216 text.format("Program arguments: %s%n", Arrays.toString(paramCleanup(commandLineArgs).toArray())); 217 } 218 DataSet dataset = MainApplication.getLayerManager().getActiveDataSet(); 219 if (dataset != null) { 220 String result = DatasetConsistencyTest.runTests(dataset); 221 if (result.isEmpty()) { 222 text.println("Dataset consistency test: No problems found"); 223 } else { 224 text.println(); 225 text.println("Dataset consistency test:"); 226 text.println(result); 227 } 228 } 229 text.println(); 230 appendCollection(text, "Plugins", Utils.transform(PluginHandler.getBugReportInformation(), i -> "+ " + i)); 231 appendCollection(text, "Tagging presets", getCustomUrls(PresetPrefHelper.INSTANCE)); 232 appendCollection(text, "Map paint styles", getCustomUrls(MapPaintPrefHelper.INSTANCE)); 233 appendCollection(text, "Validator rules", getCustomUrls(ValidatorPrefHelper.INSTANCE)); 234 appendCollection(text, "Last errors/warnings", Utils.transform(Logging.getLastErrorAndWarnings(), i -> "- " + i)); 235 236 String osmApi = OsmApi.getOsmApi().getServerUrl(); 237 if (!Config.getUrls().getDefaultOsmApiUrl().equals(osmApi.trim())) { 238 text.format("OSM API: %s%n", osmApi); 239 } 240 241 text.println(); 242 return stringWriter.toString(); 243 } 244 245 private static String toString(Dimension dimension) { 246 return dimension.width + "\u00D7" + dimension.height; 247 } 248 249 private static Collection<String> getCustomUrls(SourcePrefHelper helper) { 250 final Set<String> defaultUrls = helper.getDefault().stream() 251 .map(i -> i.url) 252 .collect(Collectors.toSet()); 253 return helper.get().stream() 254 .filter(i -> !defaultUrls.contains(i.url)) 255 .map(i -> (i.active ? "+ " : "- ") + i.url) 256 .collect(Collectors.toList()); 257 } 258 259 private static List<String> paramCleanup(Collection<String> params) { 260 return params.stream() 261 .map(ShowStatusReportAction::paramCleanup) 262 .collect(Collectors.toList()); 263 } 264 265 /** 266 * Fill map with anonymized name to the actual used path. 267 * @return map that maps shortened name to full directory path 268 */ 269 static Map<String, String> getAnonimicDirectorySymbolMap() { 270 /* maps the anonymized name to the actual used path */ 271 Map<String, String> map = new LinkedHashMap<>(); 272 map.put(PlatformManager.isPlatformWindows() ? "%JAVA_HOME%" : "${JAVA_HOME}", getSystemEnv("JAVA_HOME")); 273 map.put("<java.home>", getSystemProperty("java.home")); 274 map.put("<josm.pref>", Config.getDirs().getPreferencesDirectory(false).toString()); 275 map.put("<josm.userdata>", Config.getDirs().getUserDataDirectory(false).toString()); 276 map.put("<josm.cache>", Config.getDirs().getCacheDirectory(false).toString()); 277 map.put(PlatformManager.isPlatformWindows() ? "%UserProfile%" : "${HOME}", getSystemProperty("user.home")); 278 return map; 279 } 280 281 /** 282 * Shortens and removes private information from a parameter used for status report. 283 * @param param parameter to cleanup 284 * @return shortened/anonymized parameter 285 */ 286 static String paramCleanup(String param) { 287 final String userName = getSystemProperty("user.name"); 288 final String userNameAlt = "<user.name>"; 289 290 String val = param; 291 for (Entry<String, String> entry : getAnonimicDirectorySymbolMap().entrySet()) { 292 val = paramReplace(val, entry.getValue(), entry.getKey()); 293 } 294 if (userName != null && userName.length() >= 3) { 295 val = paramReplace(val, userName, userNameAlt); 296 } 297 return val; 298 } 299 300 private static String paramReplace(String str, String target, String replacement) { 301 return target == null ? str : str.replace(target, replacement); 302 } 303 304 private static void appendCollection(PrintWriter text, String label, Collection<String> col) { 305 if (!col.isEmpty()) { 306 text.append(col.stream().map(o -> paramCleanup(o) + '\n') 307 .collect(Collectors.joining("", label + ":\n", "\n"))); 308 } 309 } 310 311 private static String valueCleanup(Object value) { 312 String valueString = value.toString(); 313 if (valueString.length() > 512 && value instanceof Collection<?>) { 314 valueString = ((Collection<?>) value).stream().map(v -> { 315 if (v instanceof Map<?, ?>) { 316 LinkedHashMap<Object, Object> map = new LinkedHashMap<>(((Map<?, ?>) v)); 317 map.computeIfPresent("icon", (k, icon) -> Utils.shortenString(icon.toString(), 32)); // see #19058 318 return map.toString(); 319 } else { 320 return String.valueOf(v); 321 } 322 }).collect(Collectors.joining(",\n ", "[", "\n]")); 323 } 324 return paramCleanup(valueString); 325 } 326 327 @Override 328 public void actionPerformed(ActionEvent e) { 329 StringBuilder text = new StringBuilder(); 330 String reportHeader = getReportHeader(); 331 text.append(reportHeader); 332 333 Preferences.main().getAllSettings().forEach((key, setting) -> { 334 if ("file-open.history".equals(key) 335 || "download.overpass.query".equals(key) 336 || "download.overpass.queries".equals(key) 337 || "iodb.stored.offsets".equals(key) 338 || key.contains("username") 339 || key.contains("password") 340 || key.contains("access-token")) { 341 // Remove sensitive or large information from status report 342 return; 343 } 344 text.append(paramCleanup(key)) 345 .append('=') 346 .append(valueCleanup(setting.getValue())) 347 .append('\n'); 348 }); 349 350 DebugTextDisplay ta = new DebugTextDisplay(text.toString()); 351 352 ExtendedDialog ed = new ExtendedDialog(MainApplication.getMainFrame(), 353 tr("Status Report"), 354 tr("Copy to clipboard and close"), tr("Report bug"), tr("Close")); 355 ed.setButtonIcons("copy", "bug", "cancel"); 356 ed.configureContextsensitiveHelp("/Action/ShowStatusReport", true); 357 ed.setContent(ta, false); 358 ed.setMinimumSize(new Dimension(380, 200)); 359 ed.setPreferredSize(new Dimension(700, MainApplication.getMainFrame().getHeight()-50)); 360 361 switch (ed.showDialog().getValue()) { 362 case 1: ta.copyToClipboard(); break; 363 case 2: BugReportSender.reportBug(reportHeader); break; 364 default: // do nothing 365 } 366 GuiHelper.destroyComponents(ed, false); 367 } 368}