diff --git a/native/src/main/java/org/jline/nativ/JLineNativeLoader.java b/native/src/main/java/org/jline/nativ/JLineNativeLoader.java index 639199344..47a053dfe 100644 --- a/native/src/main/java/org/jline/nativ/JLineNativeLoader.java +++ b/native/src/main/java/org/jline/nativ/JLineNativeLoader.java @@ -40,6 +40,8 @@ import java.util.List; import java.util.Properties; import java.util.Random; +import java.util.logging.Level; +import java.util.logging.Logger; /** * Set the system properties, library.jline.path, library.jline.name, @@ -53,6 +55,7 @@ */ public class JLineNativeLoader { + private static final Logger logger = Logger.getLogger("org.jline"); private static boolean loaded = false; private static String nativeLibraryPath; private static String nativeLibrarySourceUrl; @@ -113,7 +116,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); } } } @@ -221,7 +224,7 @@ private static boolean extractAndLoadLibraryFile( return true; } } catch (IOException e) { - System.err.println(e.getMessage()); + log(Level.WARNING, "Unable to load JLine's native library", e); } return false; } @@ -253,9 +256,11 @@ private static boolean loadNativeLibrary(File libPath) { nativeLibraryPath = path; return true; } catch (UnsatisfiedLinkError e) { - System.err.println("Failed to load native library:" + libPath.getName() + ". osinfo: " - + OSInfo.getNativeLibFolderPathForCurrentOS()); - System.err.println(e); + log( + Level.WARNING, + "Failed to load native library:" + libPath.getName() + ". osinfo: " + + OSInfo.getNativeLibFolderPathForCurrentOS(), + e); return false; } @@ -364,7 +369,6 @@ public static int getMinorVersion() { * @return The version of the jline library. */ public static String getVersion() { - URL versionFile = JLineNativeLoader.class.getResource("/META-INF/maven/org.jline/jline-native/pom.properties"); String version = "unknown"; @@ -376,7 +380,7 @@ public static String getVersion() { version = version.trim().replaceAll("[^0-9.]", ""); } } catch (IOException e) { - System.err.println(e); + log(Level.WARNING, "Unable to load jline-native version", e); } return version; } @@ -392,4 +396,14 @@ private static String join(List list, String separator) { } return sb.toString(); } + + 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)"); + } + } + } } diff --git a/native/src/main/java/org/jline/nativ/OSInfo.java b/native/src/main/java/org/jline/nativ/OSInfo.java index 9df657e4f..a4a3859b6 100644 --- a/native/src/main/java/org/jline/nativ/OSInfo.java +++ b/native/src/main/java/org/jline/nativ/OSInfo.java @@ -32,6 +32,8 @@ import java.io.InputStream; import java.util.HashMap; import java.util.Locale; +import java.util.logging.Level; +import java.util.logging.Logger; /** * Provides OS name and architecture name. @@ -48,6 +50,7 @@ public class OSInfo { public static final String PPC64 = "ppc64"; public static final String ARM64 = "arm64"; + private static final Logger logger = Logger.getLogger("org.jline"); private static final HashMap archMapping = new HashMap<>(); static { @@ -140,7 +143,7 @@ static String getHardwareName() { return readFully(in); } } catch (Throwable e) { - System.err.println("Error while running uname -m: " + e.getMessage()); + log(Level.WARNING, "Error while running uname -m", e); return "unknown"; } } @@ -219,4 +222,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)"); + } + } + } }