]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.logging.ui/src/org/simantics/logging/ui/handlers/SaveLogFilesHandler.java
Logging configuration via SCL and UI & saving to ZIP-archive
[simantics/platform.git] / bundles / org.simantics.logging.ui / src / org / simantics / logging / ui / handlers / SaveLogFilesHandler.java
1 package org.simantics.logging.ui.handlers;
2
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;
9 import java.util.List;
10 import java.util.Map;
11 import java.util.Map.Entry;
12
13 import javax.inject.Named;
14
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;
26
27 public class SaveLogFilesHandler {
28
29     private static final Logger LOGGER = LoggerFactory.getLogger(SaveLogFilesHandler.class);
30
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");
34     
35     @Execute
36     public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell) {
37         
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);
44             }
45         }
46         StringBuilder fileName = new StringBuilder();
47         String productName = Platform.getProduct().getName();
48         if (productName != null)
49             fileName.append(productName.replaceAll(" ", "_")).append("-");
50         
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);
56         
57         String destination = dialog.open();
58         if (destination != null) {
59             if (LOGGER.isDebugEnabled())
60                 LOGGER.debug("Destination for saving log files is {}", destination);
61             
62             try {
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()) {
69                         try {
70                             Files.copy(p, subFolder.resolve(p.getFileName()));
71                         } catch (IOException e) {
72                             LOGGER.error("Could not copy {}", p.toAbsolutePath(), e);
73                         }
74                     }
75                 }
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);
81             }
82         } else {
83             if (LOGGER.isDebugEnabled()) {
84                 LOGGER.debug("No destination selected for saving logs");
85             }
86         }
87     }
88     
89     private static String currentLocalDateTimeStamp() {
90         return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd_HHmm"));
91     }
92
93 }