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"
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;
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);
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<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);
- 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);
}
}
}
-
- private static String currentLocalDateTimeStamp() {
- return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd_HHmm"));
- }
}
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,
setLoggingLevelForLogger :: String -> String -> <Proc> ()
importJava "org.simantics.logging.LogCollector" where
- allLogs :: <Proc> Map.T String [Path]
\ No newline at end of file
+ allLogs :: <Proc> Map.T String [Path]
+ archiveLogs :: String -> <Proc, Exception> ()
package org.simantics.logging;
+import java.io.File;
import java.lang.reflect.Field;
import java.net.URL;
import java.nio.file.Path;
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);
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);
+ }
+ }
+
}