X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.logging.ui%2Fsrc%2Forg%2Fsimantics%2Flogging%2Fui%2Fhandlers%2FSaveLogFilesHandler.java;h=8457ca5a87b2f8936370a2134fefe271f16e5345;hp=7d7983f594a9a551dcac4bf88089bb340eb9fea5;hb=47269fe0acb894f346810417d950a1ab59cdc0ea;hpb=7d5a5691780ed8373e77641b4a08f18cba0b9fab 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..8457ca5a8 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_EXTENSIONS = { "*.zip", "*.*" }; - private static final String USER_HOME = System.getProperty("user.home"); - + private static final String[] FILTER_NAMES = { Messages.SaveLogFilesHandler_FilterZipArchive, Messages.SaveLogFilesHandler_FilterAllFiles }; + private static final String[] FILTER_EXTENSIONS = { "*.zip", "*.*" }; //$NON-NLS-1$ //$NON-NLS-2$ + private static final String USER_HOME = System.getProperty("user.home"); //$NON-NLS-1$ + @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,51 +33,24 @@ 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); - + LOGGER.debug("Destination for saving log files is {}", destination); //$NON-NLS-1$ 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); + LOGGER.error("Could not save log files to ZIP", t); //$NON-NLS-1$ + ExceptionUtils.logAndShowError("Could not save log files to ZIP", t); //$NON-NLS-1$ } } else { if (LOGGER.isDebugEnabled()) { - LOGGER.debug("No destination selected for saving logs"); + LOGGER.debug("No destination selected for saving logs"); //$NON-NLS-1$ } } } - - private static String currentLocalDateTimeStamp() { - return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd_HHmm")); - } }