Skip to content

Commit

Permalink
Use logger instead of writing to stderr (#916, fixes #907)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Dec 21, 2023
1 parent ada11ff commit b2749c4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
28 changes: 21 additions & 7 deletions native/src/main/java/org/jline/nativ/JLineNativeLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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";
Expand All @@ -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;
}
Expand All @@ -392,4 +396,14 @@ private static String join(List<String> 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)");
}
}
}
}
15 changes: 14 additions & 1 deletion native/src/main/java/org/jline/nativ/OSInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<String, String> archMapping = new HashMap<>();

static {
Expand Down Expand Up @@ -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";
}
}
Expand Down Expand Up @@ -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)");
}
}
}
}

0 comments on commit b2749c4

Please sign in to comment.