Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use logger instead of writing to stderr (fixes #907) #916

Merged
merged 1 commit into from
Dec 21, 2023
Merged
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
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;