Skip to content

Commit 8d11513

Browse files
faster way to do log tailing
1 parent 004849c commit 8d11513

File tree

2 files changed

+32
-42
lines changed

2 files changed

+32
-42
lines changed

src/windows/Settings/LndLog.tsx

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useLayoutEffect, useRef } from "react";
1+
import React, { useEffect, useLayoutEffect, useRef, useState } from "react";
22
import { EmitterSubscription, NativeModules } from "react-native";
33
import { StackNavigationProp } from "@react-navigation/stack";
44
import { Icon } from "native-base";
@@ -20,31 +20,24 @@ export interface ILndLogProps {
2020
}
2121
export default function LndLog({ navigation }: ILndLogProps) {
2222
const t = useTranslation(namespaces.settings.lndLog).t;
23+
const [logs, setLogs] = useState("");
2324

2425
let log = useRef("");
2526
const forceUpdate = useForceUpdate();
2627

27-
useEffect(() => {
28-
let listener: EmitterSubscription;
29-
(async () => {
28+
const fetchLogs = async () => {
29+
try {
3030
const tailLog = await NativeModules.LndMobileTools.tailLog(100);
31-
log.current = tailLog
32-
.split("\n")
33-
.map((row) => row.slice(11))
34-
.join("\n");
35-
36-
listener = LndMobileToolsEventEmitter.addListener("lndlog", function (data: string) {
37-
log.current = log.current + "\n" + data.slice(11);
38-
forceUpdate();
39-
});
40-
41-
NativeModules.LndMobileTools.observeLndLogFile();
42-
forceUpdate();
43-
})();
31+
setLogs(tailLog);
32+
} catch (error) {
33+
console.error("Error fetching logs:", error);
34+
}
35+
};
4436

45-
return () => {
46-
listener.remove();
47-
};
37+
useEffect(() => {
38+
fetchLogs();
39+
const logUpdateTimer = setInterval(fetchLogs, 1000);
40+
return () => clearInterval(logUpdateTimer);
4841
}, []);
4942

5043
useLayoutEffect(() => {
@@ -53,7 +46,7 @@ export default function LndLog({ navigation }: ILndLogProps) {
5346
headerShown: true,
5447
headerRight: () => {
5548
return (
56-
<NavigationButton onPress={() => onPressCopy(log.current)}>
49+
<NavigationButton onPress={() => onPressCopy(logs)}>
5750
<Icon type="MaterialCommunityIcons" name="content-copy" style={{ fontSize: 22 }} />
5851
</NavigationButton>
5952
);
@@ -68,7 +61,7 @@ export default function LndLog({ navigation }: ILndLogProps) {
6861

6962
return (
7063
<Container>
71-
<LogBox text={log.current} scrollLock={true} />
64+
<LogBox text={logs} scrollLock={true} />
7265
</Container>
7366
);
7467
}

src/windows/SyncInfo.tsx

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@ export default function SyncInfo({}: ISyncInfoProps) {
4949
const recoverInfo = useStoreState((store) => store.lightning.recoverInfo);
5050
const initialKnownBlockheight = useStoreState((store) => store.lightning.initialKnownBlockheight);
5151
let bestBlockheight = useStoreState((store) => store.lightning.bestBlockheight);
52-
const log = useRef("");
53-
const forceUpdate = useForceUpdate();
5452
const [showLndLog, setShowLndLog] = useState(false);
53+
const [logs, setLogs] = useState("");
5554
const listener = useRef<EmitterSubscription>();
5655

5756
useEffect(() => {
@@ -62,23 +61,21 @@ export default function SyncInfo({}: ISyncInfoProps) {
6261
};
6362
}, []);
6463

65-
const onPressShowLndLog = async () => {
66-
const tailLog = await NativeModules.LndMobileTools.tailLog(100);
67-
log.current = tailLog
68-
.split("\n")
69-
.map((row) => row.slice(11))
70-
.join("\n");
71-
72-
listener.current = LndMobileToolsEventEmitter.addListener("lndlog", function (data: string) {
73-
log.current = log.current + "\n" + data.slice(11);
74-
forceUpdate();
75-
});
76-
77-
NativeModules.LndMobileTools.observeLndLogFile();
78-
forceUpdate();
79-
setShowLndLog(true);
64+
const fetchLogs = async () => {
65+
try {
66+
const tailLog = await NativeModules.LndMobileTools.tailLog(100);
67+
setLogs(tailLog);
68+
} catch (error) {
69+
console.error("Error fetching logs:", error);
70+
}
8071
};
8172

73+
useEffect(() => {
74+
fetchLogs();
75+
const logUpdateTimer = setInterval(fetchLogs, 1000);
76+
return () => clearInterval(logUpdateTimer);
77+
}, []);
78+
8279
const onPressCopy = (l: string) => {
8380
Clipboard.setString(l);
8481
toast(t("msg.clipboardCopy", { ns: namespaces.common }), undefined, "warning");
@@ -172,16 +169,16 @@ export default function SyncInfo({}: ISyncInfoProps) {
172169
)}
173170
{!showLndLog && (
174171
<View style={{ marginTop: 10, flexDirection: "row" }}>
175-
<Button small onPress={onPressShowLndLog}>
172+
<Button small onPress={() => setShowLndLog(true)}>
176173
<Text>{t("lndLog.show")}</Text>
177174
</Button>
178175
</View>
179176
)}
180177
{showLndLog && (
181178
<View style={{ marginTop: 10 }}>
182-
<LogBox text={log.current} style={{ maxHeight: 170 }} />
179+
<LogBox text={logs} style={{ maxHeight: 170 }} />
183180
<View style={{ marginTop: 10, flexDirection: "row" }}>
184-
<Button small onPress={() => onPressCopy(log.current)}>
181+
<Button small onPress={() => onPressCopy(logs)}>
185182
<Text>{t("lndLog.copy")}</Text>
186183
</Button>
187184
</View>

0 commit comments

Comments
 (0)