Skip to content

Commit

Permalink
feat: 新增'查看错误日志'选项
Browse files Browse the repository at this point in the history
  • Loading branch information
maotoumao committed Mar 24, 2023
1 parent b6622f0 commit fc2b6cc
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/components/dialogs/components/simpleDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import {Button, Dialog, Paragraph} from 'react-native-paper';
import useColors from '@/hooks/useColors';
import useDialog from '../useDialog';
import {vh} from '@/utils/rpx';

interface ISimpleDialogProps {
title: string;
Expand All @@ -18,7 +19,10 @@ export default function SimpleDialog(props: ISimpleDialogProps) {
onDismiss={hideDialog}
style={{backgroundColor: colors.primary}}>
<Dialog.Title>{title}</Dialog.Title>
<Dialog.Content>
<Dialog.Content
style={{
maxHeight: vh(65),
}}>
{typeof content === 'string' ? (
<Paragraph>{content}</Paragraph>
) : (
Expand Down
4 changes: 0 additions & 4 deletions src/entry/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {SafeAreaProvider} from 'react-native-safe-area-context';
import toastConfig from '@/components/base/toast';
import useBootstrap from './useBootstrap';
import Debug from '@/components/debug';
import useOrientation from '@/hooks/useOrientation';

/**
* 字体颜色
Expand All @@ -41,9 +40,6 @@ export default function Pages() {

useBootstrap();

const orientation = useOrientation();
console.log(orientation);

return (
<GestureHandlerRootView style={{flex: 1}}>
<PaperProvider theme={mergedTheme}>
Expand Down
20 changes: 19 additions & 1 deletion src/pages/setting/settingTypes/basicSetting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import pathConst from '@/constants/pathConst';
import {ROUTE_PATH, useNavigate} from '@/entry/router';
import {readdir} from 'react-native-fs';
import {qualityKeys, qualityText} from '@/utils/qualities';
import {clearLog} from '@/utils/log';
import {clearLog, getErrorLogContent} from '@/utils/log';
import {ScrollView} from 'react-native-gesture-handler';
import {Paragraph} from 'react-native-paper';

const ITEM_HEIGHT = rpx(96);

Expand Down Expand Up @@ -335,6 +337,22 @@ export default function BasicSetting() {
'setting.basic.debug.devLog',
basicSetting?.debug?.devLog ?? false,
),
{
title: '查看错误日志',
right: undefined,
async onPress() {
// 获取日志文件夹
const errorLogContent = await getErrorLogContent();
showDialog('SimpleDialog', {
title: '错误日志',
content: (
<ScrollView>
<Paragraph>{errorLogContent}</Paragraph>
</ScrollView>
),
});
},
},
{
title: '清空日志',
right: undefined,
Expand Down
41 changes: 40 additions & 1 deletion src/utils/log.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {logger, fileAsyncTransport} from 'react-native-logs';
import RNFS from 'react-native-fs';
import RNFS, {readDir, readFile} from 'react-native-fs';
import pathConst from '@/constants/pathConst';
import Config from '../core/config';
import {addLog} from '@/lib/react-native-vdebug/src/log';
Expand Down Expand Up @@ -57,6 +57,45 @@ export async function clearLog() {
);
}

export async function getErrorLogContent() {
try {
const files = await readDir(pathConst.logPath);
console.log(files);
const today = new Date();
// 两天的错误日志
const yesterday = new Date();
yesterday.setDate(today.getDate() - 1);
const todayLog = files.find(
_ =>
_.isFile() &&
_.path.endsWith(
`error-log-${today.getDate()}-${
today.getMonth() + 1
}-${today.getFullYear()}.log`,
),
);
const yesterdayLog = files.find(
_ =>
_.isFile() &&
_.path.endsWith(
`error-log-${yesterday.getDate()}-${
yesterday.getMonth() + 1
}-${yesterday.getFullYear()}.log`,
),
);
let logContent = '';
if (todayLog) {
logContent += await readFile(todayLog.path, 'utf8');
}
if (yesterdayLog) {
logContent += await readFile(yesterdayLog.path, 'utf8');
}
return logContent;
} catch {
return '';
}
}

export function errorLog(desc: string, message: any) {
if (Config.get('setting.basic.debug.errorLog')) {
log.error({
Expand Down

0 comments on commit fc2b6cc

Please sign in to comment.