1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-15 09:04:04 +02:00
joplin/ReactNativeClient/lib/components/screens/log.js

130 lines
3.7 KiB
JavaScript
Raw Normal View History

const React = require('react'); const Component = React.Component;
const { ListView, 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.js');
const { BaseScreenComponent } = require('lib/components/base-screen.js');
const { _ } = require('lib/locale.js');
2017-07-07 19:19:24 +02:00
2017-07-14 20:49:14 +02:00
class LogScreenComponent extends BaseScreenComponent {
2017-07-07 19:19:24 +02:00
static navigationOptions(options) {
return { header: null };
}
constructor() {
super();
const ds = new ListView.DataSource({
rowHasChanged: (r1, r2) => { return r1 !== r2; }
2017-07-07 19:19:24 +02:00
});
this.state = {
dataSource: ds,
showErrorsOnly: false,
2017-07-07 19:19:24 +02:00
};
2017-08-01 19:59:01 +02:00
this.styles_ = {};
}
styles() {
const theme = themeStyle(this.props.theme);
if (this.styles_[this.props.theme]) return this.styles_[this.props.theme];
this.styles_ = {};
let styles = {
row: {
flexDirection: 'row',
2017-08-01 19:59:01 +02:00
paddingLeft: 1,
paddingRight: 1,
paddingTop:0,
paddingBottom:0,
2017-08-01 19:59:01 +02:00
},
rowText: {
fontSize: 10,
color: theme.color,
2017-08-01 19:59:01 +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;
this.styles_[this.props.theme] = StyleSheet.create(styles);
return this.styles_[this.props.theme];
2017-07-07 19:19:24 +02:00
}
componentWillMount() {
2017-07-08 01:25:10 +02:00
this.resfreshLogEntries();
}
resfreshLogEntries(showErrorsOnly = null) {
if (showErrorsOnly === null) showErrorsOnly = this.state.showErrorsOnly;
let levels = [Logger.LEVEL_DEBUG, Logger.LEVEL_INFO, Logger.LEVEL_WARN, Logger.LEVEL_ERROR];
if (showErrorsOnly) levels = [Logger.LEVEL_WARN, Logger.LEVEL_ERROR]
reg.logger().lastEntries(1000, { levels: levels }).then((entries) => {
const newDataSource = this.state.dataSource.cloneWithRows(entries);
this.setState({ dataSource: newDataSource });
});
2017-07-07 19:19:24 +02:00
}
toggleErrorsOnly() {
const showErrorsOnly = !this.state.showErrorsOnly;
this.setState({ showErrorsOnly: showErrorsOnly });
this.resfreshLogEntries(showErrorsOnly);
}
2017-07-07 19:19:24 +02:00
render() {
let 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;
2017-07-07 19:19:24 +02:00
return (
2017-08-01 19:59:01 +02:00
<View style={this.styles().row}>
<Text style={textStyle}>{time.formatMsToLocal(item.timestamp, 'MM-DDTHH:mm:ss') + ': ' + item.message}</Text>
2017-07-07 19:19:24 +02:00
</View>
);
}
2017-07-07 19:19:24 +02:00
// `enableEmptySections` is to fix this warning: https://github.com/FaridSafi/react-native-gifted-listview/issues/39
return (
2017-08-01 19:59:01 +02:00
<View style={this.rootStyle(this.props.theme).root}>
<ScreenHeader title={_('Log')}/>
<ListView
dataSource={this.state.dataSource}
renderRow={renderRow}
enableEmptySections={true}
/>
<View style={{flexDirection: 'row'}}>
<View style={{flex:1, marginRight: 5 }}>
<Button title={_("Refresh")} onPress={() => { this.resfreshLogEntries(); }}/>
</View>
<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>
);
}
2017-07-07 19:19:24 +02:00
}
const LogScreen = connect(
(state) => {
return {
theme: state.settings.theme,
};
}
)(LogScreenComponent)
2017-07-07 19:19:24 +02:00
module.exports = { LogScreen };