]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.logging.ui/src/org/simantics/logging/ui/handlers/SaveLogFilesHandler.java b/bundles/org.simantics.logging.ui/src/org/simantics/logging/ui/handlers/SaveLogFilesHandler.java
new file mode 100644 (file)
index 0000000..7d7983f
--- /dev/null
@@ -0,0 +1,93 @@
+package org.simantics.logging.ui.handlers;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import javax.inject.Named;
+
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.e4.core.di.annotations.Execute;
+import org.eclipse.e4.ui.services.IServiceConstants;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.FileDialog;
+import org.eclipse.swt.widgets.Shell;
+import org.simantics.logging.LogCollector;
+import org.simantics.utils.FileUtils;
+import org.simantics.utils.ui.ExceptionUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class SaveLogFilesHandler {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(SaveLogFilesHandler.class);
+
+    private static final String[] FILTER_NAMES = { "ZIP-archive", "AllFiles (*:*)" };
+    private static final String[] FILTER_EXTENSIONS = { "*.zip", "*.*" };
+    private static final String USER_HOME = System.getProperty("user.home");
+    
+    @Execute
+    public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell) {
+        
+        FileDialog dialog = new FileDialog(shell, SWT.SAVE);
+        dialog.setFilterNames(FILTER_NAMES);
+        dialog.setFilterExtensions(FILTER_EXTENSIONS);
+        if (USER_HOME != null) {
+            if (Files.exists(Paths.get(USER_HOME))) {
+                dialog.setFilterPath(USER_HOME);
+            }
+        }
+        StringBuilder fileName = new StringBuilder();
+        String productName = Platform.getProduct().getName();
+        if (productName != null)
+            fileName.append(productName.replaceAll(" ", "_")).append("-");
+        
+        fileName.append("logs-").append(currentLocalDateTimeStamp());
+        String actualFileName = fileName.toString();
+        if (LOGGER.isDebugEnabled())
+            LOGGER.debug("Resolved log files name {}", actualFileName);
+        dialog.setFileName(actualFileName);
+        
+        String destination = dialog.open();
+        if (destination != null) {
+            if (LOGGER.isDebugEnabled())
+                LOGGER.debug("Destination for saving log files is {}", destination);
+            
+            try {
+                Path tempDir = Files.createTempDirectory(actualFileName);
+                Map<String, List<Path>> allLogs = LogCollector.allLogs();
+                for (Entry<String, List<Path>> logEntry : allLogs.entrySet()) {
+                    Path subFolder = tempDir.resolve(logEntry.getKey());
+                    Files.createDirectory(subFolder);
+                    for (Path p : logEntry.getValue()) {
+                        try {
+                            Files.copy(p, subFolder.resolve(p.getFileName()));
+                        } catch (IOException e) {
+                            LOGGER.error("Could not copy {}", p.toAbsolutePath(), e);
+                        }
+                    }
+                }
+                FileUtils.compressZip(tempDir.toAbsolutePath().toString(), destination);
+                FileUtils.delete(tempDir);
+            } catch (Throwable t) {
+                LOGGER.error("Could not save log files to ZIP", t);
+                ExceptionUtils.logAndShowError("Could not save log files to ZIP", t);
+            }
+        } else {
+            if (LOGGER.isDebugEnabled()) {
+                LOGGER.debug("No destination selected for saving logs");
+            }
+        }
+    }
+    
+    private static String currentLocalDateTimeStamp() {
+        return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd_HHmm"));
+    }
+
+}