Skip to content
This repository was archived by the owner on Feb 23, 2021. It is now read-only.
Merged
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package host.exp.exponent;

import android.content.res.AssetManager;
import android.os.FileObserver;
import android.util.Base64;
import android.util.Log;

Expand All @@ -12,10 +13,14 @@
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand All @@ -37,11 +42,14 @@ public class LndNativeModule extends ReactContextBaseJavaModule {
private static final String respEventTypeKey = "event";
private static final String respEventTypeData = "data";
private static final String respEventTypeError = "error";
private static final String logEventName = "logs";

private Map<String, SendStream> activeStreams = new HashMap<>();
private Map<String, Method> syncMethods = new HashMap<>();
private Map<String, Method> streamMethods = new HashMap<>();

private FileObserver logObserver;

private static boolean isReceiveStream(Method m) {
return m.toString().contains("RecvStream");
}
Expand Down Expand Up @@ -146,6 +154,53 @@ public void start(final Promise promise) {
File appDir = getReactApplicationContext().getFilesDir();
copyConfig(appDir);

final String logDir = appDir + "/logs/bitcoin/testnet";
final String logFile = logDir + "/lnd.log";

FileInputStream stream = null;
while (true) {
try {
stream = new FileInputStream(logFile);
} catch (FileNotFoundException e) {
File dir = new File(logDir);
dir.mkdirs();
File f = new File(logFile);
try {
f.createNewFile();
continue;
} catch (IOException e1) {
e1.printStackTrace();
return;
}
}
break;
}

final InputStreamReader istream = new InputStreamReader(stream);
final BufferedReader buf = new BufferedReader(istream);
try {
readToEnd(buf, false);
} catch (IOException e) {
e.printStackTrace();
return;
}

logObserver = new FileObserver(logFile) {
@Override
public void onEvent(int event, String file) {
if(event != FileObserver.MODIFY) {
return;
}
try {
readToEnd(buf, true);
} catch (IOException e) {
e.printStackTrace();
}
}
};
logObserver.startWatching();
Log.i("LndNativeModule", "Started watching " + logFile);

final String args = "--lnddir=" + appDir;
Log.i("LndNativeModule", "Starting LND with args " + args);

Expand All @@ -158,6 +213,18 @@ public void run() {
new Thread(startLnd).start();
}

private void readToEnd(BufferedReader buf, boolean emit) throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit. Extra new line here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

String s = "";
while ( (s = buf.readLine()) != null ) {
if (!emit) {
continue;
}
getReactApplicationContext()
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(logEventName, s);
}
}

private void copyConfig(File appDir) {
File conf = new File(appDir, "lnd.conf");
AssetManager am = getCurrentActivity().getAssets();
Expand Down