Skip to content

Commit

Permalink
Merge pull request #9909 from JabRef/fixLogger
Browse files Browse the repository at this point in the history
Make logger instances vars in linux
  • Loading branch information
Siedlerchr authored May 16, 2023
2 parents db31df3 + 09eb36d commit 0247913
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 18 deletions.
11 changes: 8 additions & 3 deletions src/main/java/org/jabref/gui/desktop/os/DefaultDesktop.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@
import java.nio.file.Path;

import org.jabref.architecture.AllowedToUseAwt;
import org.jabref.cli.Launcher;
import org.jabref.gui.DialogService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This class contains some default implementations (if OS is neither linux, windows or osx) file directories and file/application open handling methods <br>
* We cannot use a static logger instance here in this class as the Logger first needs to be configured in the {@link Launcher#addLogToDisk}
* The configuration of tinylog will become immutable as soon as the first log entry is issued.
* https://tinylog.org/v2/configuration/
**/
@AllowedToUseAwt("Requires AWT to open a file")
public class DefaultDesktop implements NativeDesktop {
private static final Logger LOGGER = LoggerFactory.getLogger(NativeDesktop.class);

@Override
public void openFile(String filePath, String fileType) throws IOException {
Expand All @@ -33,7 +38,7 @@ public void openFolderAndSelectFile(Path filePath) throws IOException {

@Override
public void openConsole(String absolutePath, DialogService dialogService) throws IOException {
LOGGER.error("This feature is not supported by your Operating System.");
LoggerFactory.getLogger(DefaultDesktop.class).error("This feature is not supported by your Operating System.");
}

@Override
Expand Down
34 changes: 19 additions & 15 deletions src/main/java/org/jabref/gui/desktop/os/Linux.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.Optional;

import org.jabref.architecture.AllowedToUseAwt;
import org.jabref.cli.Launcher;
import org.jabref.gui.DialogService;
import org.jabref.gui.Globals;
import org.jabref.gui.JabRefExecutorService;
Expand All @@ -20,30 +21,33 @@
import org.jabref.gui.util.StreamGobbler;
import org.jabref.logic.l10n.Localization;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This class contains Linux specific implementations for file directories and file/application open handling methods <br>
* We cannot use a static logger instance here in this class as the Logger first needs to be configured in the {@link Launcher#addLogToDisk}
* The configuration of tinylog will become immutable as soon as the first log entry is issued.
* https://tinylog.org/v2/configuration/
**/
@AllowedToUseAwt("Requires AWT to open a file with the native method")
public class Linux implements NativeDesktop {

private static final Logger LOGGER = LoggerFactory.getLogger(Linux.class);

private void nativeOpenFile(String filePath) {
JabRefExecutorService.INSTANCE.execute(() -> {
try {
File file = new File(filePath);
Desktop.getDesktop().open(file);
LOGGER.debug("Open file in default application with Desktop integration");
LoggerFactory.getLogger(Linux.class).debug("Open file in default application with Desktop integration");
} catch (IllegalArgumentException e) {
LOGGER.debug("Fail back to xdg-open");
LoggerFactory.getLogger(Linux.class).debug("Fail back to xdg-open");
try {
String[] cmd = {"xdg-open", filePath};
Runtime.getRuntime().exec(cmd);
} catch (Exception e2) {
LOGGER.warn("Open operation not successful: " + e2);
LoggerFactory.getLogger(Linux.class).warn("Open operation not successful: ", e2);
}
} catch (IOException e) {
LOGGER.warn("Native open operation not successful: " + e);
LoggerFactory.getLogger(Linux.class).warn("Native open operation not successful: ", e);
}
});
}
Expand All @@ -57,8 +61,8 @@ public void openFile(String filePath, String fileType) throws IOException {
viewer = type.get().getOpenWithApplication();
ProcessBuilder processBuilder = new ProcessBuilder(viewer, filePath);
Process process = processBuilder.start();
StreamGobbler streamGobblerInput = new StreamGobbler(process.getInputStream(), LOGGER::debug);
StreamGobbler streamGobblerError = new StreamGobbler(process.getErrorStream(), LOGGER::debug);
StreamGobbler streamGobblerInput = new StreamGobbler(process.getInputStream(), LoggerFactory.getLogger(Linux.class)::debug);
StreamGobbler streamGobblerError = new StreamGobbler(process.getErrorStream(), LoggerFactory.getLogger(Linux.class)::debug);

JabRefExecutorService.INSTANCE.execute(streamGobblerInput);
JabRefExecutorService.INSTANCE.execute(streamGobblerError);
Expand All @@ -80,8 +84,8 @@ public void openFileWithApplication(String filePath, String application) throws
ProcessBuilder processBuilder = new ProcessBuilder(cmdArray);
Process process = processBuilder.start();

StreamGobbler streamGobblerInput = new StreamGobbler(process.getInputStream(), LOGGER::debug);
StreamGobbler streamGobblerError = new StreamGobbler(process.getErrorStream(), LOGGER::debug);
StreamGobbler streamGobblerInput = new StreamGobbler(process.getInputStream(), LoggerFactory.getLogger(Linux.class)::debug);
StreamGobbler streamGobblerError = new StreamGobbler(process.getErrorStream(), LoggerFactory.getLogger(Linux.class)::debug);

JabRefExecutorService.INSTANCE.execute(streamGobblerInput);
JabRefExecutorService.INSTANCE.execute(streamGobblerError);
Expand Down Expand Up @@ -112,8 +116,8 @@ public void openFolderAndSelectFile(Path filePath) throws IOException {
ProcessBuilder processBuilder = new ProcessBuilder(cmd);
Process process = processBuilder.start();

StreamGobbler streamGobblerInput = new StreamGobbler(process.getInputStream(), LOGGER::debug);
StreamGobbler streamGobblerError = new StreamGobbler(process.getErrorStream(), LOGGER::debug);
StreamGobbler streamGobblerInput = new StreamGobbler(process.getInputStream(), LoggerFactory.getLogger(Linux.class)::debug);
StreamGobbler streamGobblerError = new StreamGobbler(process.getErrorStream(), LoggerFactory.getLogger(Linux.class)::debug);

JabRefExecutorService.INSTANCE.execute(streamGobblerInput);
JabRefExecutorService.INSTANCE.execute(streamGobblerError);
Expand Down Expand Up @@ -150,8 +154,8 @@ public void openConsole(String absolutePath, DialogService dialogService) throws
builder.directory(new File(absolutePath));
Process processTerminal = builder.start();

StreamGobbler streamGobblerInput = new StreamGobbler(processTerminal.getInputStream(), LOGGER::debug);
StreamGobbler streamGobblerError = new StreamGobbler(processTerminal.getErrorStream(), LOGGER::debug);
StreamGobbler streamGobblerInput = new StreamGobbler(processTerminal.getInputStream(), LoggerFactory.getLogger(Linux.class)::debug);
StreamGobbler streamGobblerError = new StreamGobbler(processTerminal.getErrorStream(), LoggerFactory.getLogger(Linux.class)::debug);

JabRefExecutorService.INSTANCE.execute(streamGobblerInput);
JabRefExecutorService.INSTANCE.execute(streamGobblerError);
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/jabref/gui/desktop/os/OSX.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@
import java.util.Optional;

import org.jabref.architecture.AllowedToUseAwt;
import org.jabref.cli.Launcher;
import org.jabref.gui.DialogService;
import org.jabref.gui.Globals;
import org.jabref.gui.externalfiletype.ExternalFileType;
import org.jabref.gui.externalfiletype.ExternalFileTypes;

/**
* This class contains macOS (OSX) specific implementations for file directories and file/application open handling methods <br>
* We cannot use a static logger instance here in this class as the Logger first needs to be configured in the {@link Launcher#addLogToDisk}
* The configuration of tinylog will become immutable as soon as the first log entry is issued.
* https://tinylog.org/v2/configuration/
**/
@AllowedToUseAwt("Requires AWT to open a file")
public class OSX implements NativeDesktop {

Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/jabref/gui/desktop/os/Windows.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.nio.file.Path;
import java.util.Optional;

import org.jabref.cli.Launcher;
import org.jabref.gui.DialogService;
import org.jabref.gui.Globals;
import org.jabref.gui.externalfiletype.ExternalFileType;
Expand All @@ -17,6 +18,12 @@
import com.sun.jna.platform.win32.Win32Exception;
import org.slf4j.LoggerFactory;

/**
* This class contains Windows specific implementations for file directories and file/application open handling methods <br>
* We cannot use a static logger instance here in this class as the Logger first needs to be configured in the {@link Launcher#addLogToDisk}
* The configuration of tinylog will become immutable as soon as the first log entry is issued.
* https://tinylog.org/v2/configuration/
**/
public class Windows implements NativeDesktop {

private static final String DEFAULT_EXECUTABLE_EXTENSION = ".exe";
Expand Down

0 comments on commit 0247913

Please sign in to comment.