1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-30 10:36:35 +02:00
joplin/ReactNativeClient/lib/components/screens/log.js

136 lines
3.5 KiB
JavaScript
Raw Normal View History

2019-07-29 15:43:53 +02:00
const React = require('react');
2019-07-29 15:58:33 +02:00
2020-02-09 16:51:12 +02:00
const { FlatList, View, Text, Button, StyleSheet, Platform } = require('react-native');
const { connect } = require('react-redux');
const { reg } = require('lib/registry.js');
const { ScreenHeader } = require('lib/components/screen-header.js');
const { time } = require('lib/time-utils');
const { themeStyle } = require('lib/components/global-style.js');
const Logger = require('lib/Logger').default;
const { BaseScreenComponent } = require('lib/components/base-screen.js');
const { _ } = require('lib/locale');
2017-07-07 19:19:24 +02:00
2017-07-14 20:49:14 +02:00
class LogScreenComponent extends BaseScreenComponent {
static navigationOptions() {
2017-07-07 19:19:24 +02:00
return { header: null };
}
constructor() {
super();
2020-02-09 16:51:12 +02:00
2017-07-07 19:19:24 +02:00
this.state = {
2020-02-09 16:51:12 +02:00
logEntries: [],
showErrorsOnly: false,
2017-07-07 19:19:24 +02:00
};
2017-08-01 19:59:01 +02:00
this.styles_ = {};
}
styles() {
2020-09-15 15:01:07 +02:00
const theme = themeStyle(this.props.themeId);
2017-08-01 19:59:01 +02:00
2020-09-15 15:01:07 +02:00
if (this.styles_[this.props.themeId]) return this.styles_[this.props.themeId];
2017-08-01 19:59:01 +02:00
this.styles_ = {};
const styles = {
2017-08-01 19:59:01 +02:00
row: {
flexDirection: 'row',
2017-08-01 19:59:01 +02:00
paddingLeft: 1,
paddingRight: 1,
2019-07-29 15:43:53 +02:00
paddingTop: 0,
paddingBottom: 0,
2017-08-01 19:59:01 +02:00
},
rowText: {
fontSize: 10,
2019-07-29 15:43:53 +02:00
color: theme.color,
2017-08-01 19:59:01 +02:00
},
};
2019-07-29 15:43:53 +02:00
if (Platform.OS !== 'ios') {
// Crashes on iOS with error "Unrecognized font family 'monospace'"
styles.rowText.fontFamily = 'monospace';
}
2017-08-01 19:59:01 +02:00
styles.rowTextError = Object.assign({}, styles.rowText);
styles.rowTextError.color = theme.colorError;
2017-08-21 20:32:43 +02:00
styles.rowTextWarn = Object.assign({}, styles.rowText);
2017-08-01 19:59:01 +02:00
styles.rowTextWarn.color = theme.colorWarn;
2020-09-15 15:01:07 +02:00
this.styles_[this.props.themeId] = StyleSheet.create(styles);
return this.styles_[this.props.themeId];
2017-07-07 19:19:24 +02:00
}
UNSAFE_componentWillMount() {
2017-07-08 01:25:10 +02:00
this.resfreshLogEntries();
}
2020-02-09 16:51:12 +02:00
async resfreshLogEntries(showErrorsOnly = null) {
if (showErrorsOnly === null) showErrorsOnly = this.state.showErrorsOnly;
let levels = [Logger.LEVEL_DEBUG, Logger.LEVEL_INFO, Logger.LEVEL_WARN, Logger.LEVEL_ERROR];
2019-07-29 15:43:53 +02:00
if (showErrorsOnly) levels = [Logger.LEVEL_WARN, Logger.LEVEL_ERROR];
2020-02-09 16:51:12 +02:00
this.setState({
logEntries: await reg.logger().lastEntries(1000, { levels: levels }),
showErrorsOnly: showErrorsOnly,
});
2017-07-07 19:19:24 +02:00
}
toggleErrorsOnly() {
2020-02-09 16:51:12 +02:00
this.resfreshLogEntries(!this.state.showErrorsOnly);
}
2017-07-07 19:19:24 +02:00
render() {
const renderRow = ({ item }) => {
2017-08-01 19:59:01 +02:00
let textStyle = this.styles().rowText;
if (item.level == Logger.LEVEL_WARN) textStyle = this.styles().rowTextWarn;
if (item.level == Logger.LEVEL_ERROR) textStyle = this.styles().rowTextError;
2019-07-29 15:43:53 +02:00
2017-07-07 19:19:24 +02:00
return (
2017-08-01 19:59:01 +02:00
<View style={this.styles().row}>
2019-09-19 23:51:18 +02:00
<Text style={textStyle}>{`${time.formatMsToLocal(item.timestamp, 'MM-DDTHH:mm:ss')}: ${item.message}`}</Text>
2017-07-07 19:19:24 +02:00
</View>
);
2019-07-29 15:43:53 +02:00
};
2017-07-07 19:19:24 +02:00
// `enableEmptySections` is to fix this warning: https://github.com/FaridSafi/react-native-gifted-listview/issues/39
2020-02-09 16:51:12 +02:00
2017-07-07 19:19:24 +02:00
return (
2020-09-15 15:01:07 +02:00
<View style={this.rootStyle(this.props.themeId).root}>
2019-07-29 15:43:53 +02:00
<ScreenHeader title={_('Log')} />
2020-02-09 16:51:12 +02:00
<FlatList
data={this.state.logEntries}
renderItem={renderRow}
keyExtractor={item => { return `${item.id}`; }}
2020-02-09 16:51:12 +02:00
/>
2019-07-29 15:43:53 +02:00
<View style={{ flexDirection: 'row' }}>
<View style={{ flex: 1, marginRight: 5 }}>
<Button
title={_('Refresh')}
onPress={() => {
this.resfreshLogEntries();
}}
/>
</View>
2019-07-29 15:43:53 +02:00
<View style={{ flex: 1 }}>
<Button
title={this.state.showErrorsOnly ? _('Show all') : _('Errors only')}
onPress={() => {
this.toggleErrorsOnly();
}}
/>
</View>
</View>
2017-07-07 19:19:24 +02:00
</View>
);
}
}
const LogScreen = connect(state => {
2019-07-29 15:43:53 +02:00
return {
2020-09-15 15:01:07 +02:00
themeId: state.settings.theme,
2019-07-29 15:43:53 +02:00
};
})(LogScreenComponent);
2017-07-07 19:19:24 +02:00
2019-07-29 15:43:53 +02:00
module.exports = { LogScreen };