Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 29 additions & 12 deletions src/main/java/org/fusesource/jansi/internal/JansiLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
import java.util.List;
import java.util.Properties;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.fusesource.jansi.AnsiConsole;

Expand All @@ -60,6 +62,8 @@
*/
public class JansiLoader {

private static final Logger logger = Logger.getLogger("org.fusesource.jansi");

private static boolean loaded = false;
private static String nativeLibraryPath;
private static String nativeLibrarySourceUrl;
Expand Down Expand Up @@ -124,7 +128,7 @@ public boolean accept(File dir, String name) {
try {
nativeLibFile.delete();
} catch (SecurityException e) {
System.err.println("Failed to delete old native lib" + e.getMessage());
logger.log(Level.INFO, "Failed to delete old native lib" + e.getMessage(), e);
}
}
}
Expand Down Expand Up @@ -229,7 +233,7 @@ private static boolean extractAndLoadLibraryFile(
return true;
}
} catch (IOException e) {
System.err.println(e.getMessage());
log(Level.WARNING, "Unable to load Jansi's native library", e);
}
return false;
}
Expand All @@ -256,17 +260,20 @@ private static boolean loadNativeLibrary(File libPath) {
// NOTE: this can be tested using something like:
// docker run --rm --tmpfs /tmp -v $PWD:/jansi openjdk:11 java -jar
// /jansi/target/jansi-xxx-SNAPSHOT.jar
System.err.printf(
"Failed to load native library:%s. The native library file at %s is not executable, "
+ "make sure that the directory is mounted on a partition without the noexec flag, or set the "
+ "jansi.tmpdir system property to point to a proper location. osinfo: %s%n",
libPath.getName(), libPath, OSInfo.getNativeLibFolderPathForCurrentOS());
log(
Level.WARNING,
String.format("Failed to load native library:%s. The native library file at %s is not executable, "
+ "make sure that the directory is mounted on a partition without the noexec flag, or set the "
+ "jansi.tmpdir system property to point to a proper location. osinfo: %s",
libPath.getName(), libPath, OSInfo.getNativeLibFolderPathForCurrentOS()),
e);
} else {
System.err.printf(
"Failed to load native library:%s. osinfo: %s%n",
libPath.getName(), OSInfo.getNativeLibFolderPathForCurrentOS());
log(
Level.WARNING,
String.format("Failed to load native library:%s. osinfo: %s",
libPath.getName(), OSInfo.getNativeLibFolderPathForCurrentOS()),
e);
}
System.err.println(e);
return false;
}

Expand Down Expand Up @@ -388,8 +395,18 @@ public static String getVersion() {
version = version.trim().replaceAll("[^0-9.]", "");
}
} catch (IOException e) {
System.err.println(e);
log(Level.WARNING, "Unable to load jansi version", e);
}
return version;
}

private static void log(Level level, String message, Throwable t) {
if (logger.isLoggable(level)) {
if (logger.isLoggable(Level.FINE)) {
logger.log(level, message, t);
} else {
logger.log(level, message + " (caused by: " + t + ", enable debug logging for stacktrace)");
}
}
}
}
15 changes: 14 additions & 1 deletion src/main/java/org/fusesource/jansi/internal/OSInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Provides OS name and architecture name.
Expand All @@ -53,6 +55,7 @@ public class OSInfo {
public static final String PPC64 = "ppc64";
public static final String ARM64 = "arm64";

private static final Logger logger = Logger.getLogger("org.fusesource.jansi");
private static final HashMap<String, String> archMapping = new HashMap<String, String>();

static {
Expand Down Expand Up @@ -147,7 +150,7 @@ static String getHardwareName() {
in.close();
}
} catch (Throwable e) {
System.err.println("Error while running uname -m: " + e.getMessage());
log(Level.WARNING, "Error while running uname -m", e);
return "unknown";
}
}
Expand Down Expand Up @@ -226,4 +229,14 @@ static String translateOSNameToFolderName(String osName) {
static String translateArchNameToFolderName(String archName) {
return archName.replaceAll("\\W", "");
}

private static void log(Level level, String message, Throwable t) {
if (logger.isLoggable(level)) {
if (logger.isLoggable(Level.FINE)) {
logger.log(level, message, t);
} else {
logger.log(level, message + " (caused by: " + t + ", enable debug logging for stacktrace)");
}
}
}
}