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

Last change on this file since 6049 was 5992, checked in by Don-vip, 11 years ago

Nicer VM arguments in status report

  • Property svn:eol-style set to native
File size: 7.6 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;
6
7import java.awt.Dimension;
8import java.awt.event.ActionEvent;
9import java.awt.event.KeyEvent;
10import java.lang.management.ManagementFactory;
11import java.util.ArrayList;
12import java.util.Arrays;
13import java.util.HashSet;
14import java.util.List;
15import java.util.ListIterator;
16import java.util.Map;
17import java.util.Map.Entry;
18import java.util.Set;
19
20import javax.swing.JScrollPane;
21
22import org.openstreetmap.josm.Main;
23import org.openstreetmap.josm.data.Preferences.Setting;
24import org.openstreetmap.josm.data.Version;
25import org.openstreetmap.josm.data.osm.DataSet;
26import org.openstreetmap.josm.data.osm.DatasetConsistencyTest;
27import org.openstreetmap.josm.gui.ExtendedDialog;
28import org.openstreetmap.josm.gui.widgets.JosmTextArea;
29import org.openstreetmap.josm.plugins.PluginHandler;
30import org.openstreetmap.josm.tools.BugReportExceptionHandler;
31import org.openstreetmap.josm.tools.OpenBrowser;
32import org.openstreetmap.josm.tools.PlatformHookWindows;
33import org.openstreetmap.josm.tools.Shortcut;
34import org.openstreetmap.josm.tools.Utils;
35
36/**
37 * @author xeen
38 *
39 * Opens a dialog with useful status information like version numbers for Java, JOSM and plugins
40 * Also includes preferences with stripped username and password
41 */
42public final class ShowStatusReportAction extends JosmAction {
43
44 /**
45 * Constructs a new {@code ShowStatusReportAction}
46 */
47 public ShowStatusReportAction() {
48 super(
49 tr("Show Status Report"),
50 "clock",
51 tr("Show status report with useful information that can be attached to bugs"),
52 Shortcut.registerShortcut("help:showstatusreport", tr("Help: {0}",
53 tr("Show Status Report")), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE), false);
54
55 putValue("help", ht("/Action/ShowStatusReport"));
56 putValue("toolbar", "help/showstatusreport");
57 Main.toolbar.register(this);
58 }
59
60 private static void shortenParam(ListIterator<String> it, String[] param, String source, String target) {
61 if (source != null && target.length() < source.length() && param[1].startsWith(source)) {
62 it.set(param[0] + "=" + param[1].replace(source, target));
63 }
64 }
65
66 /**
67 * Replies the report header (software and system info)
68 * @return The report header (software and system info)
69 */
70 public static String getReportHeader()
71 {
72 StringBuilder text = new StringBuilder();
73 text.append(Version.getInstance().getReleaseAttributes());
74 text.append("\n");
75 text.append("Identification: " + Version.getInstance().getAgentString());
76 text.append("\n");
77 text.append("Memory Usage: ");
78 text.append(Runtime.getRuntime().totalMemory()/1024/1024);
79 text.append(" MB / ");
80 text.append(Runtime.getRuntime().maxMemory()/1024/1024);
81 text.append(" MB (");
82 text.append(Runtime.getRuntime().freeMemory()/1024/1024);
83 text.append(" MB allocated, but free)");
84 text.append("\n");
85 text.append("Java version: " + System.getProperty("java.version") + ", " + System.getProperty("java.vendor") + ", " + System.getProperty("java.vm.name"));
86 text.append("\n");
87 try {
88 final String env_java_home = System.getenv("JAVA_HOME");
89 final String env_java_home_alt = Main.platform instanceof PlatformHookWindows ? "%JAVA_HOME%" : "${JAVA_HOME}";
90 final String prop_java_home = System.getProperty("java.home");
91 final String prop_java_home_alt = "<java.home>";
92 // Build a new list of VM parameters to modify it below if needed (default implementation returns an UnmodifiableList instance)
93 List<String> vmArguments = new ArrayList<String>(ManagementFactory.getRuntimeMXBean().getInputArguments());
94 for (ListIterator<String> it = vmArguments.listIterator(); it.hasNext(); ) {
95 String value = it.next();
96 if (value.contains("=")) {
97 String[] param = value.split("=");
98 // Hide some parameters for privacy concerns
99 if (param[0].toLowerCase().startsWith("-dproxy")) {
100 it.set(param[0]+"=xxx");
101 // Shorten some parameters for readability concerns
102 } else {
103 shortenParam(it, param, env_java_home, env_java_home_alt);
104 shortenParam(it, param, prop_java_home, prop_java_home_alt);
105 }
106 }
107 }
108 if (!vmArguments.isEmpty()) {
109 text.append("VM arguments: "+ vmArguments.toString().replace("\\\\", "\\"));
110 text.append("\n");
111 }
112 } catch (SecurityException e) {
113 // Ignore exception
114 }
115 if (Main.commandLineArgs.length > 0) {
116 text.append("Program arguments: "+ Arrays.toString(Main.commandLineArgs));
117 text.append("\n");
118 }
119 if (Main.main != null) {
120 DataSet dataset = Main.main.getCurrentDataSet();
121 if (dataset != null) {
122 String result = DatasetConsistencyTest.runTests(dataset);
123 if (result.length() == 0) {
124 text.append("Dataset consistency test: No problems found\n");
125 } else {
126 text.append("\nDataset consistency test:\n"+result+"\n");
127 }
128 }
129 }
130 text.append("\n");
131 text.append(PluginHandler.getBugReportText());
132 text.append("\n");
133
134 return text.toString();
135 }
136
137 public void actionPerformed(ActionEvent e) {
138 StringBuilder text = new StringBuilder();
139 String reportHeader = getReportHeader();
140 text.append(reportHeader);
141 try {
142 Map<String, Setting> settings = Main.pref.getAllSettings();
143 settings.remove("osm-server.username");
144 settings.remove("osm-server.password");
145 settings.remove("oauth.access-token.key");
146 settings.remove("oauth.access-token.secret");
147 Set<String> keys = new HashSet<String>(settings.keySet());
148 for (String key : keys) {
149 if (key.startsWith("marker.show")) {
150 settings.remove(key);
151 }
152 }
153 for (Entry<String, Setting> entry : settings.entrySet()) {
154 text.append(entry.getKey()).append("=").append(entry.getValue().getValue().toString()).append("\n");
155 }
156 } catch (Exception x) {
157 x.printStackTrace();
158 }
159
160 JosmTextArea ta = new JosmTextArea(text.toString());
161 ta.setWrapStyleWord(true);
162 ta.setLineWrap(true);
163 ta.setEditable(false);
164 JScrollPane sp = new JScrollPane(ta);
165
166 ExtendedDialog ed = new ExtendedDialog(Main.parent,
167 tr("Status Report"),
168 new String[] {tr("Copy to clipboard and close"), tr("Report bug"), tr("Close") });
169 ed.setButtonIcons(new String[] {"copy.png", "bug.png", "cancel.png" });
170 ed.setContent(sp, false);
171 ed.setMinimumSize(new Dimension(380, 200));
172 ed.setPreferredSize(new Dimension(700, Main.parent.getHeight()-50));
173
174 switch (ed.showDialog().getValue()) {
175 case 1: Utils.copyToClipboard(text.toString()); break;
176 case 2: OpenBrowser.displayUrl(BugReportExceptionHandler.getBugReportUrl(
177 Utils.strip(reportHeader)).toExternalForm()) ; break;
178 }
179 }
180}
Note: See TracBrowser for help on using the repository browser.