001package apps.util.issuereporter;
002
003import java.io.File;
004import java.io.IOException;
005import java.nio.file.*;
006import java.util.*;
007
008import jmri.profile.Profile;
009import jmri.profile.ProfileManager;
010import jmri.util.prefs.InitializationException;
011
012import org.jdom2.JDOMException;
013
014/**
015 *
016 * @author rhwood
017 */
018public class BugReport extends IssueReport {
019
020    private boolean includeProfile;
021    private final boolean includeSysInfo;
022    private final boolean includeLogs;
023    private final List<File> files = new ArrayList<>();
024
025    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(BugReport.class);
026
027    public BugReport(String title, String body, boolean includeProfile, boolean includeSysInfo, boolean includeLogs) {
028        super(title, body);
029        this.includeProfile = includeProfile;
030        this.includeSysInfo = includeSysInfo;
031        this.includeLogs = includeLogs;
032    }
033
034    @Override
035    public void prepare() {
036        title = Bundle.getMessage(Locale.US, "bug.title", title);
037        if (!tooLong) {
038            try {
039                Path dir = Files.createTempDirectory("jmri-issue-report-" + new Date().getTime());
040                if (includeLogs) {
041                    Files.copy(new File(System.getProperty("jmri.log.path"), "session.log").toPath(), dir.resolve("session.log"), StandardCopyOption.REPLACE_EXISTING);
042                }
043                if (includeProfile) {
044                    Profile profile = ProfileManager.getDefault().getActiveProfile();
045                    if (profile != null) {
046                        File archive = new File(dir.toFile(), "profile.zip");
047                        ProfileManager.getDefault().export(profile, archive, false, false);
048                    }
049                }
050                if (includeSysInfo) {
051                    getSysInfoFile(dir);
052                }
053                try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
054                    stream.forEach(p -> files.add(p.toFile()));
055                }
056            } catch (IOException | JDOMException | InitializationException | NullPointerException ex) {
057                log.error("Unable to include profile in report.", ex);
058                includeProfile = false;
059            }
060            body = Bundle.getMessage(Locale.US,
061                    "bug.body",
062                    !files.isEmpty() ? Bundle.getMessage("instructions.paste.files") : "",
063                    body,
064                    getSimpleContext(),
065                    getIssueFooter());
066        } else {
067            body = Bundle.getMessage("instructions.paste.414");
068        }
069    }
070
071    private void getSysInfoFile(Path path) {
072        Path file = path.resolve("systemInfo.txt");
073        try {
074            Files.createFile(file);
075            Files.write(file, new SystemInfo(false).asList(), StandardOpenOption.TRUNCATE_EXISTING);
076        } catch (IOException ex) {
077            log.error("Exception writing system info", ex);
078        }
079    }
080
081    @Override
082    public List<File> getAttachments() {
083        return files;
084    }
085}