-
Notifications
You must be signed in to change notification settings - Fork 45
/
Logs.js
157 lines (143 loc) · 3.9 KB
/
Logs.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import React, { PureComponent } from 'react';
import { InteractionManager, FlatList } from 'react-native';
import {
Container,
Header,
Right,
Left,
Content,
Body,
Title,
List,
ListItem,
Text,
Button,
Icon,
Spinner
} from 'native-base';
import BackgroundGeolocation from '@mauron85/react-native-background-geolocation';
const STYLES = Object();
STYLES['ERROR'] = { backgroundColor:'white',color:'red' };
STYLES['WARN'] = { backgroundColor:'black',color:'yellow' };
STYLES['INFO'] = { backgroundColor:'white',color:'blue' };
STYLES['TRACE'] = { backgroundColor:'white',color:'black' };
STYLES['DEBUG'] = { backgroundColor:'white',color:'black' };
function padLeft(nr, n, str) {
return Array(n - String(nr).length + 1).join(str || '0') + nr;
}
function formatLogEntry(logEntry) {
const d = new Date(Number(logEntry.timestamp));
const dateStr = [d.getFullYear(), padLeft(d.getMonth()+1,2), padLeft(d.getDate(),2)].join('/');
const timeStr = [padLeft(d.getHours(),2), padLeft(d.getMinutes(),2), padLeft(d.getSeconds(),2)].join(':');
return {
eventId: logEntry.id || logEntry.timestamp,
style: STYLES[logEntry.level],
text: ['[', dateStr, ' ', timeStr, ']\t', logEntry.message, logEntry.stackTrace ? logEntry.stackTrace : ''].join('')
};
}
class LogsScene extends PureComponent {
static navigationOptions = {
title: 'Logs',
header: null
};
constructor(props) {
super(props);
this.state = {
isRefreshing: false,
logEntries: [],
};
this.rowLimit = 100;
this.lastEventId = 0; // intentionally not set in state as it's asynchronnous
this.refresh = this.refresh.bind(this);
}
componentDidMount() {
InteractionManager.runAfterInteractions(() => {
this.refresh();
});
}
refresh() {
this.fetchLogs(0);
}
fetchLogs(lastEventId) {
if (lastEventId < 0) {
return;
}
if (lastEventId == 0) {
this.setState({ isRefreshing: true, logEntries: [] });
}
BackgroundGeolocation.getLogEntries(this.rowLimit, lastEventId, 'DEBUG', newLogEntries => {
const { logEntries } = this.state
const lastEventId = newLogEntries.length > 0 ? newLogEntries[newLogEntries.length - 1].id : -1;
this.lastEventId = lastEventId;
this.setState({
isRefreshing: false,
logEntries: logEntries.concat(newLogEntries),
});
});
}
_keyExtractor = (item, index) => item.id;
_renderItem = ({ item }) => {
const entry = formatLogEntry(item);
return (
<ListItem
style={{
marginLeft: 2,
backgroundColor: entry.style.backgroundColor
}}
>
<Text
style={{
color: entry.style.color
}}
>
{`${entry.eventId}:${entry.text}`}
</Text>
</ListItem>
);
};
_onEndReached = () => {
this.fetchLogs(this.lastEventId);
};
renderContent(logEntries) {
return (
<FlatList
style={{ flex: 1, backgroundColor: '#fff' }}
data={logEntries}
keyExtractor={this._keyExtractor}
renderItem={this._renderItem}
onEndReached={this._onEndReached}
/>
);
}
render() {
const { logEntries, isRefreshing } = this.state;
return (
<Container>
<Header>
<Left>
<Button transparent onPress={() => this.props.navigation.goBack()}>
<Icon name="arrow-back" />
</Button>
</Left>
<Body>
<Title>Logs</Title>
</Body>
<Right>
<Button transparent onPress={this.refresh}>
<Icon name="refresh" />
</Button>
</Right>
</Header>
<Content>
{(() => {
if (isRefreshing) {
return <Spinner />;
}
return this.renderContent(logEntries);
})()}
</Content>
</Container>
);
}
}
export default LogsScene;