forked from Memmy-App/memmy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogHelper.ts
52 lines (39 loc) · 1.39 KB
/
LogHelper.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import * as FileSystem from "react-native-fs";
import * as MailComposer from "expo-mail-composer";
import { getReadableVersion } from "react-native-device-info";
import moment from "moment";
const logFile = `${FileSystem.LibraryDirectoryPath}/lemmy-debug.log`;
const createIfDontExist = async () => {
console.log("Checking...");
if (!(await FileSystem.exists(logFile))) {
console.log("Doesn't exist...");
await FileSystem.touch(logFile)
.then()
.catch((e) => console.log(e.toString()));
}
};
const writeToLog = (text: string) => {
console.log(text);
const time = moment().utc(true);
FileSystem.appendFile(logFile, `\n[${time}] ${text}`).then();
};
const readLog = async (): Promise<string> => FileSystem.readFile(logFile);
const deleteLog = (): void => {
FileSystem.exists(logFile).then((r) => {
if (!r) return;
FileSystem.unlink(logFile).then();
});
};
const sendLog = async () => {
const exists = await FileSystem.exists(logFile);
if (!exists) throw Error("no_file");
const version = getReadableVersion();
const options = {
subject: `Memmy App ${version} Debug File`,
recipients: ["[email protected]"],
body: "Please enter any other information that you would like to send with your debug file:",
attachments: [logFile],
};
MailComposer.composeAsync(options).then();
};
export { createIfDontExist, writeToLog, readLog, deleteLog, sendLog };