Minor ssh debug logging changes
-Refactor the logger implementation to make creation more intuitive -Remove SSH debug logging preference persistence (didn't work properly anyway, and probably not worth trying to fix)
This commit is contained in:
@@ -16,6 +16,12 @@ public class Kp2aJSchLogger implements Logger {
|
||||
void log(String message);
|
||||
}
|
||||
|
||||
private interface EntryToLogFactory {
|
||||
ILogger create(LogEntry e);
|
||||
}
|
||||
|
||||
private static final EntryToLogFactory ANDROID_FACTORY = e -> e.logger;
|
||||
|
||||
private static final class LogEntry {
|
||||
private final String levelTag;
|
||||
private final ILogger logger;
|
||||
@@ -39,10 +45,19 @@ public class Kp2aJSchLogger implements Logger {
|
||||
);
|
||||
|
||||
|
||||
private final String logFilename;
|
||||
private final EntryToLogFactory logFactory;
|
||||
|
||||
public Kp2aJSchLogger(String logFilename) {
|
||||
this.logFilename = logFilename;
|
||||
static Kp2aJSchLogger createAndroidLogger() {
|
||||
return new Kp2aJSchLogger(ANDROID_FACTORY);
|
||||
}
|
||||
|
||||
static Kp2aJSchLogger createFileLogger(String logFilename) {
|
||||
final String fName = logFilename;
|
||||
return new Kp2aJSchLogger(e -> createFileLogger(e, fName));
|
||||
}
|
||||
|
||||
private Kp2aJSchLogger(EntryToLogFactory logFactory) {
|
||||
this.logFactory = logFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -61,19 +76,12 @@ public class Kp2aJSchLogger implements Logger {
|
||||
if (entry == null)
|
||||
entry = DEFAULT_ENTRY;
|
||||
|
||||
ILogger logger;
|
||||
if (logFilename != null) {
|
||||
logger = createFileLogger(entry);
|
||||
} else {
|
||||
logger = entry.logger;
|
||||
}
|
||||
|
||||
return logger;
|
||||
return logFactory.create(entry);
|
||||
}
|
||||
|
||||
private ILogger createFileLogger(LogEntry entry) {
|
||||
private static ILogger createFileLogger(LogEntry entry, String fName) {
|
||||
try {
|
||||
final PrintWriter p = new PrintWriter(new FileWriter(logFilename, true));
|
||||
final PrintWriter p = new PrintWriter(new FileWriter(fName, true));
|
||||
return msg -> {
|
||||
try {
|
||||
String fullMsg = String.join(" ", entry.levelTag, PREFIX, msg);
|
||||
|
||||
@@ -19,6 +19,7 @@ import com.jcraft.jsch.ChannelSftp;
|
||||
import com.jcraft.jsch.ChannelSftp.LsEntry;
|
||||
import com.jcraft.jsch.JSch;
|
||||
import com.jcraft.jsch.JSchException;
|
||||
import com.jcraft.jsch.Logger;
|
||||
import com.jcraft.jsch.Session;
|
||||
import com.jcraft.jsch.SftpATTRS;
|
||||
import com.jcraft.jsch.SftpException;
|
||||
@@ -515,11 +516,15 @@ public class SftpStorage extends JavaFileStorageBase {
|
||||
|
||||
@SuppressWarnings("unused") // Exposed by JavaFileStorageBindings
|
||||
public void setJschLogging(boolean enabled, String logFilename) {
|
||||
Logger impl = null;
|
||||
if (enabled) {
|
||||
JSch.setLogger(new Kp2aJSchLogger(logFilename));
|
||||
} else {
|
||||
JSch.setLogger(null);
|
||||
if (logFilename != null) {
|
||||
impl = Kp2aJSchLogger.createFileLogger(logFilename);
|
||||
} else {
|
||||
impl = Kp2aJSchLogger.createAndroidLogger();
|
||||
}
|
||||
}
|
||||
JSch.setLogger(impl);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -711,10 +716,6 @@ public class SftpStorage extends JavaFileStorageBase {
|
||||
.addOption(SSH_CFG_SERVER_HOST_KEY, shkAlgorithms, nonBlankStringResolver)
|
||||
.build());
|
||||
|
||||
// FIXME: Remove this!
|
||||
Log.d(TAG, "buildFullPath returns uri: " + uri);
|
||||
// FIXME <end>
|
||||
|
||||
return uri.toString();
|
||||
}
|
||||
|
||||
|
||||
@@ -677,7 +677,7 @@
|
||||
android:key="@string/DebugLog_send_key" />
|
||||
<CheckBoxPreference
|
||||
android:enabled="true"
|
||||
android:persistent="true"
|
||||
android:persistent="false"
|
||||
android:defaultValue="false"
|
||||
android:title="@string/JSchDebug_title"
|
||||
android:key="@string/JSchDebug_key" />
|
||||
|
||||
Reference in New Issue
Block a user