From f77aae8906bcd780c0202720257d8c443a261342 Mon Sep 17 00:00:00 2001 From: Tuukka Lehtonen Date: Wed, 7 Mar 2018 14:59:39 +0200 Subject: [PATCH] Added LoggingUtils/archiveLogs SCL function. 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 --- .../META-INF/MANIFEST.MF | 2 - .../ui/handlers/SaveLogFilesHandler.java | 47 ++--------------- .../META-INF/MANIFEST.MF | 3 +- .../scl/LoggingUtils.scl | 3 +- .../logging/DBAndMetadataLogProvider.java | 5 +- .../org/simantics/logging/LogCollector.java | 50 +++++++++++++++++++ 6 files changed, 62 insertions(+), 48 deletions(-) diff --git a/bundles/org.simantics.logging.ui/META-INF/MANIFEST.MF b/bundles/org.simantics.logging.ui/META-INF/MANIFEST.MF index 3f910c8e4..600285018 100644 --- a/bundles/org.simantics.logging.ui/META-INF/MANIFEST.MF +++ b/bundles/org.simantics.logging.ui/META-INF/MANIFEST.MF @@ -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" 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 index 7d7983f59..dd6280500 100644 --- 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 @@ -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> allLogs = LogCollector.allLogs(); - for (Entry> 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")); - } } diff --git a/bundles/org.simantics.logging/META-INF/MANIFEST.MF b/bundles/org.simantics.logging/META-INF/MANIFEST.MF index 115019a65..22e51353d 100644 --- a/bundles/org.simantics.logging/META-INF/MANIFEST.MF +++ b/bundles/org.simantics.logging/META-INF/MANIFEST.MF @@ -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, diff --git a/bundles/org.simantics.logging/scl/LoggingUtils.scl b/bundles/org.simantics.logging/scl/LoggingUtils.scl index 500ba1b83..0ffa970be 100644 --- a/bundles/org.simantics.logging/scl/LoggingUtils.scl +++ b/bundles/org.simantics.logging/scl/LoggingUtils.scl @@ -6,4 +6,5 @@ importJava "org.simantics.logging.LogConfigurator" where setLoggingLevelForLogger :: String -> String -> () importJava "org.simantics.logging.LogCollector" where - allLogs :: Map.T String [Path] \ No newline at end of file + allLogs :: Map.T String [Path] + archiveLogs :: String -> () diff --git a/bundles/org.simantics.logging/src/org/simantics/logging/DBAndMetadataLogProvider.java b/bundles/org.simantics.logging/src/org/simantics/logging/DBAndMetadataLogProvider.java index 79e10dbef..e24a66fc4 100644 --- a/bundles/org.simantics.logging/src/org/simantics/logging/DBAndMetadataLogProvider.java +++ b/bundles/org.simantics.logging/src/org/simantics/logging/DBAndMetadataLogProvider.java @@ -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); diff --git a/bundles/org.simantics.logging/src/org/simantics/logging/LogCollector.java b/bundles/org.simantics.logging/src/org/simantics/logging/LogCollector.java index 4d98e2767..ab6985dff 100644 --- a/bundles/org.simantics.logging/src/org/simantics/logging/LogCollector.java +++ b/bundles/org.simantics.logging/src/org/simantics/logging/LogCollector.java @@ -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> allLogs = LogCollector.allLogs(); + for (Entry> 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); + } + } + } -- 2.43.2