1 package org.simantics.logging.ui.handlers;
3 import java.nio.file.Files;
4 import java.nio.file.Paths;
6 import javax.inject.Named;
8 import org.eclipse.e4.core.di.annotations.Execute;
9 import org.eclipse.e4.ui.services.IServiceConstants;
10 import org.eclipse.swt.SWT;
11 import org.eclipse.swt.widgets.FileDialog;
12 import org.eclipse.swt.widgets.Shell;
13 import org.simantics.logging.LogCollector;
14 import org.simantics.utils.ui.ExceptionUtils;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
18 public class SaveLogFilesHandler {
20 private static final Logger LOGGER = LoggerFactory.getLogger(SaveLogFilesHandler.class);
22 private static final String[] FILTER_NAMES = { Messages.SaveLogFilesHandler_FilterZipArchive, Messages.SaveLogFilesHandler_FilterAllFiles };
23 private static final String[] FILTER_EXTENSIONS = { "*.zip", "*.*" }; //$NON-NLS-1$ //$NON-NLS-2$
24 private static final String USER_HOME = System.getProperty("user.home"); //$NON-NLS-1$
27 public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell) {
28 FileDialog dialog = new FileDialog(shell, SWT.SAVE);
29 dialog.setFilterNames(FILTER_NAMES);
30 dialog.setFilterExtensions(FILTER_EXTENSIONS);
31 if (USER_HOME != null) {
32 if (Files.exists(Paths.get(USER_HOME))) {
33 dialog.setFilterPath(USER_HOME);
36 String actualFileName = LogCollector.archiveFileName();
37 dialog.setFileName(actualFileName);
39 String destination = dialog.open();
40 if (destination != null) {
41 if (LOGGER.isDebugEnabled())
42 LOGGER.debug("Destination for saving log files is {}", destination); //$NON-NLS-1$
44 LogCollector.archiveLogs(destination);
45 } catch (Throwable t) {
46 LOGGER.error("Could not save log files to ZIP", t); //$NON-NLS-1$
47 ExceptionUtils.logAndShowError("Could not save log files to ZIP", t); //$NON-NLS-1$
50 if (LOGGER.isDebugEnabled()) {
51 LOGGER.debug("No destination selected for saving logs"); //$NON-NLS-1$