1 package org.simantics.logging.ui.handlers;
3 import java.io.IOException;
4 import java.nio.file.Files;
5 import java.nio.file.Path;
6 import java.nio.file.Paths;
7 import java.time.LocalDateTime;
8 import java.time.format.DateTimeFormatter;
11 import java.util.Map.Entry;
13 import javax.inject.Named;
15 import org.eclipse.core.runtime.Platform;
16 import org.eclipse.e4.core.di.annotations.Execute;
17 import org.eclipse.e4.ui.services.IServiceConstants;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.widgets.FileDialog;
20 import org.eclipse.swt.widgets.Shell;
21 import org.simantics.logging.LogCollector;
22 import org.simantics.utils.FileUtils;
23 import org.simantics.utils.ui.ExceptionUtils;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 public class SaveLogFilesHandler {
29 private static final Logger LOGGER = LoggerFactory.getLogger(SaveLogFilesHandler.class);
31 private static final String[] FILTER_NAMES = { "ZIP-archive", "AllFiles (*:*)" };
32 private static final String[] FILTER_EXTENSIONS = { "*.zip", "*.*" };
33 private static final String USER_HOME = System.getProperty("user.home");
36 public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell) {
38 FileDialog dialog = new FileDialog(shell, SWT.SAVE);
39 dialog.setFilterNames(FILTER_NAMES);
40 dialog.setFilterExtensions(FILTER_EXTENSIONS);
41 if (USER_HOME != null) {
42 if (Files.exists(Paths.get(USER_HOME))) {
43 dialog.setFilterPath(USER_HOME);
46 StringBuilder fileName = new StringBuilder();
47 String productName = Platform.getProduct().getName();
48 if (productName != null)
49 fileName.append(productName.replaceAll(" ", "_")).append("-");
51 fileName.append("logs-").append(currentLocalDateTimeStamp());
52 String actualFileName = fileName.toString();
53 if (LOGGER.isDebugEnabled())
54 LOGGER.debug("Resolved log files name {}", actualFileName);
55 dialog.setFileName(actualFileName);
57 String destination = dialog.open();
58 if (destination != null) {
59 if (LOGGER.isDebugEnabled())
60 LOGGER.debug("Destination for saving log files is {}", destination);
63 Path tempDir = Files.createTempDirectory(actualFileName);
64 Map<String, List<Path>> allLogs = LogCollector.allLogs();
65 for (Entry<String, List<Path>> logEntry : allLogs.entrySet()) {
66 Path subFolder = tempDir.resolve(logEntry.getKey());
67 Files.createDirectory(subFolder);
68 for (Path p : logEntry.getValue()) {
70 Files.copy(p, subFolder.resolve(p.getFileName()));
71 } catch (IOException e) {
72 LOGGER.error("Could not copy {}", p.toAbsolutePath(), e);
76 FileUtils.compressZip(tempDir.toAbsolutePath().toString(), destination);
77 FileUtils.delete(tempDir);
78 } catch (Throwable t) {
79 LOGGER.error("Could not save log files to ZIP", t);
80 ExceptionUtils.logAndShowError("Could not save log files to ZIP", t);
83 if (LOGGER.isDebugEnabled()) {
84 LOGGER.debug("No destination selected for saving logs");
89 private static String currentLocalDateTimeStamp() {
90 return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd_HHmm"));