X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.logging%2Fsrc%2Forg%2Fsimantics%2Flogging%2FLogCollector.java;h=ab6985dff5bd7c3d7ac203abb80155ca72172b2b;hp=4d98e27670cd071578a8194e32b2c0324f5edc95;hb=f77aae8906bcd780c0202720257d8c443a261342;hpb=1251d640abb698a4018ef1ea212f8cdf78befaaf 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); + } + } + }