Added LoggingUtils/archiveLogs SCL function. 33/1533/1
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Wed, 7 Mar 2018 12:59:39 +0000 (14:59 +0200)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Wed, 7 Mar 2018 12:59:39 +0000 (14:59 +0200)
Also moved all archival related headless code to org.simantics.logging
from org.simantics.logging.ui to keep both UI & headless codebases
cleaner.

DBAndMetadataLogProvider was fixed to support workspace paths with
whitespace in their name.

LogCollector.archiveLogs now also removes the temporary directory
structure it creates for collecting and archiving the log files.

refs #7795

Change-Id: Icbffa7ffc5d6a5f2b859b0220fbebcf59b5ac590

bundles/org.simantics.logging.ui/META-INF/MANIFEST.MF
bundles/org.simantics.logging.ui/src/org/simantics/logging/ui/handlers/SaveLogFilesHandler.java
bundles/org.simantics.logging/META-INF/MANIFEST.MF
bundles/org.simantics.logging/scl/LoggingUtils.scl
bundles/org.simantics.logging/src/org/simantics/logging/DBAndMetadataLogProvider.java
bundles/org.simantics.logging/src/org/simantics/logging/LogCollector.java

index 3f910c8e4ab29d081b6c239d1888ce6501417107..6002850180f3ad3ceec138bdafe1033302d968cc 100644 (file)
@@ -10,10 +10,8 @@ Require-Bundle: javax.inject,
  org.eclipse.e4.ui.services,
  org.eclipse.e4.core.di.annotations,
  org.eclipse.core.runtime,
- org.eclipse.ui.ide,
  org.slf4j.api,
  org.simantics.logging,
- org.simantics.utils,
  org.simantics.utils.ui
 Bundle-RequiredExecutionEnvironment: JavaSE-1.8
 Import-Package: javax.annotation;version="1.2.0"
index 7d7983f594a9a551dcac4bf88089bb340eb9fea5..dd62805009376c1554c95e4a908828b820a7f793 100644 (file)
@@ -1,25 +1,16 @@
 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;
@@ -28,13 +19,12 @@ 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_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);
@@ -43,38 +33,15 @@ public class SaveLogFilesHandler {
                 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);
+        String actualFileName = LogCollector.archiveFileName();
         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);
+                LogCollector.archiveLogs(destination);
             } catch (Throwable t) {
                 LOGGER.error("Could not save log files to ZIP", t);
                 ExceptionUtils.logAndShowError("Could not save log files to ZIP", t);
@@ -85,9 +52,5 @@ public class SaveLogFilesHandler {
             }
         }
     }
-    
-    private static String currentLocalDateTimeStamp() {
-        return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd_HHmm"));
-    }
 
 }
index 115019a65b5fafe0415ffde5c50c914f8dfc6f99..22e51353d06926af8b21b8f5a4c5bcf33538593c 100644 (file)
@@ -8,7 +8,8 @@ Bundle-Vendor: Semantum Oy
 Require-Bundle: org.eclipse.core.runtime,
  org.slf4j.api,
  ch.qos.logback.classic,
- ch.qos.logback.core
+ ch.qos.logback.core,
+ org.simantics.utils
 Bundle-RequiredExecutionEnvironment: JavaSE-1.8
 Bundle-ActivationPolicy: lazy
 Service-Component: logbackLogProvider.xml,
index 500ba1b836aad5d3290df9420a1613752307c917..0ffa970be0dd14b647b112f524507503f8825976 100644 (file)
@@ -6,4 +6,5 @@ importJava "org.simantics.logging.LogConfigurator" where
     setLoggingLevelForLogger :: String -> String -> <Proc> ()
 
 importJava "org.simantics.logging.LogCollector" where
-    allLogs :: <Proc> Map.T String [Path]
\ No newline at end of file
+    allLogs :: <Proc> Map.T String [Path]
+    archiveLogs :: String -> <Proc, Exception> ()
index 79e10dbef85b675783e8f68c86077e020772f267..e24a66fc452faaf17a88f8d52bff22bc941f1aa8 100644 (file)
@@ -1,5 +1,6 @@
 package org.simantics.logging;
 
+import java.io.File;
 import java.lang.reflect.Field;
 import java.net.URL;
 import java.nio.file.Path;
@@ -45,8 +46,8 @@ public class DBAndMetadataLogProvider implements LogProvider {
         if (prop != null) {
             try {
                 URL url = new URL(prop);
-                if ("file".equals(url .getProtocol())) {
-                    Path path = Paths.get(url.toURI());
+                if ("file".equals(url.getProtocol())) {
+                    Path path = Paths.get(new File(url.getFile()).getAbsolutePath());
                     return path.resolve(".metadata").resolve(".log");
                 } else {
                     LOGGER.warn("Unsupported protocol {}", url);
index 4d98e27670cd071578a8194e32b2c0324f5edc95..ab6985dff5bd7c3d7ac203abb80155ca72172b2b 100644 (file)
@@ -1,16 +1,24 @@
 package org.simantics.logging;
 
+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.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
 
+import org.eclipse.core.runtime.Platform;
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceReference;
 import org.simantics.logging.internal.Activator;
+import org.simantics.utils.FileUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -62,4 +70,46 @@ public final class LogCollector {
             LOGGER.debug("Found {} log providers", logProviders);
         return logProviders;
     }
+
+    private static String currentLocalDateTimeStamp() {
+        return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd_HHmm"));
+    }
+
+    public static String archiveFileName() {
+        StringBuilder fileName = new StringBuilder();
+        String productName = Platform.getProduct().getName();
+        if (productName != null)
+            fileName.append(productName.replaceAll(" ", "_")).append("-");
+        fileName.append("logs-").append(currentLocalDateTimeStamp());
+        String result = fileName.toString();
+        if (LOGGER.isDebugEnabled())
+            LOGGER.debug("Resolved log files name {}", result);
+        return result;
+    }
+
+    public static void archiveLogs(String destination) throws IOException {
+        archiveLogs(Paths.get(destination));
+    }
+
+    private static void archiveLogs(Path destination) throws IOException {
+        Path tempDir = Files.createTempDirectory(destination.getFileName().toString());
+        try {
+            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.toAbsolutePath().toString());
+        } finally {
+            FileUtils.delete(tempDir);
+        }
+    }
+
 }