11package host .exp .exponent ;
22
33import android .content .res .AssetManager ;
4+ import android .os .FileObserver ;
45import android .util .Base64 ;
56import android .util .Log ;
67
1213import com .facebook .react .bridge .WritableMap ;
1314import com .facebook .react .modules .core .DeviceEventManagerModule ;
1415
16+ import java .io .BufferedReader ;
1517import java .io .File ;
18+ import java .io .FileInputStream ;
19+ import java .io .FileNotFoundException ;
1620import java .io .FileOutputStream ;
1721import java .io .IOException ;
1822import java .io .InputStream ;
23+ import java .io .InputStreamReader ;
1924import java .io .OutputStream ;
2025import java .lang .reflect .InvocationTargetException ;
2126import java .lang .reflect .Method ;
@@ -37,11 +42,14 @@ public class LndNativeModule extends ReactContextBaseJavaModule {
3742 private static final String respEventTypeKey = "event" ;
3843 private static final String respEventTypeData = "data" ;
3944 private static final String respEventTypeError = "error" ;
45+ private static final String logEventName = "logs" ;
4046
4147 private Map <String , SendStream > activeStreams = new HashMap <>();
4248 private Map <String , Method > syncMethods = new HashMap <>();
4349 private Map <String , Method > streamMethods = new HashMap <>();
4450
51+ private FileObserver logObserver ;
52+
4553 private static boolean isReceiveStream (Method m ) {
4654 return m .toString ().contains ("RecvStream" );
4755 }
@@ -146,6 +154,53 @@ public void start(final Promise promise) {
146154 File appDir = getReactApplicationContext ().getFilesDir ();
147155 copyConfig (appDir );
148156
157+ final String logDir = appDir + "/logs/bitcoin/testnet" ;
158+ final String logFile = logDir + "/lnd.log" ;
159+
160+ FileInputStream stream = null ;
161+ while (true ) {
162+ try {
163+ stream = new FileInputStream (logFile );
164+ } catch (FileNotFoundException e ) {
165+ File dir = new File (logDir );
166+ dir .mkdirs ();
167+ File f = new File (logFile );
168+ try {
169+ f .createNewFile ();
170+ continue ;
171+ } catch (IOException e1 ) {
172+ e1 .printStackTrace ();
173+ return ;
174+ }
175+ }
176+ break ;
177+ }
178+
179+ final InputStreamReader istream = new InputStreamReader (stream );
180+ final BufferedReader buf = new BufferedReader (istream );
181+ try {
182+ readToEnd (buf , false );
183+ } catch (IOException e ) {
184+ e .printStackTrace ();
185+ return ;
186+ }
187+
188+ logObserver = new FileObserver (logFile ) {
189+ @ Override
190+ public void onEvent (int event , String file ) {
191+ if (event != FileObserver .MODIFY ) {
192+ return ;
193+ }
194+ try {
195+ readToEnd (buf , true );
196+ } catch (IOException e ) {
197+ e .printStackTrace ();
198+ }
199+ }
200+ };
201+ logObserver .startWatching ();
202+ Log .i ("LndNativeModule" , "Started watching " + logFile );
203+
149204 final String args = "--lnddir=" + appDir ;
150205 Log .i ("LndNativeModule" , "Starting LND with args " + args );
151206
@@ -158,6 +213,18 @@ public void run() {
158213 new Thread (startLnd ).start ();
159214 }
160215
216+ private void readToEnd (BufferedReader buf , boolean emit ) throws IOException {
217+ String s = "" ;
218+ while ( (s = buf .readLine ()) != null ) {
219+ if (!emit ) {
220+ continue ;
221+ }
222+ getReactApplicationContext ()
223+ .getJSModule (DeviceEventManagerModule .RCTDeviceEventEmitter .class )
224+ .emit (logEventName , s );
225+ }
226+ }
227+
161228 private void copyConfig (File appDir ) {
162229 File conf = new File (appDir , "lnd.conf" );
163230 AssetManager am = getCurrentActivity ().getAssets ();
0 commit comments