]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.logging/src/org/simantics/logging/LogCollector.java
Added LoggingUtils/archiveLogs SCL function.
[simantics/platform.git] / bundles / org.simantics.logging / src / org / simantics / logging / LogCollector.java
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);
+        }
+    }
+
 }