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;
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);
+ }
+ }
+
}