summaryrefslogtreecommitdiffstats
path: root/web/react/components/admin_console/logs.jsx
blob: d7de76a9440651f048200ee072da5a2c45e574b1 (plain)
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
// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.

var AdminStore = require('../../stores/admin_store.jsx');
var LoadingScreen = require('../loading_screen.jsx');
var AsyncClient = require('../../utils/async_client.jsx');

export default class Logs extends React.Component {
    constructor(props) {
        super(props);

        this.onLogListenerChange = this.onLogListenerChange.bind(this);
        this.reload = this.reload.bind(this);

        this.state = {
            logs: AdminStore.getLogs()
        };
    }

    componentDidMount() {
        AdminStore.addLogChangeListener(this.onLogListenerChange);
        AsyncClient.getLogs();
    }
    componentWillUnmount() {
        AdminStore.removeLogChangeListener(this.onLogListenerChange);
    }
    onLogListenerChange() {
        this.setState({
            logs: AdminStore.getLogs()
        });
    }

    reload() {
        AdminStore.saveLogs(null);
        this.setState({
            logs: null
        });

        AsyncClient.getLogs();
    }

    render() {
        var content = null;

        if (this.state.logs === null) {
            content = <LoadingScreen />;
        } else {
            content = [];

            for (var i = 0; i < this.state.logs.length; i++) {
                var style = {
                    whiteSpace: 'nowrap',
                    fontFamily: 'monospace'
                };

                if (this.state.logs[i].indexOf('[EROR]') > 0) {
                    style.color = 'red';
                }

                content.push(<br key={'br_' + i} />);
                content.push(
                    <span
                        key={'log_' + i}
                        style={style}
                    >
                        {this.state.logs[i]}
                    </span>
                );
            }
        }

        return (
            <div className='panel'>
                <h3>{'Server Logs'}</h3>
                <button
                    type='submit'
                    className='btn btn-primary'
                    onClick={this.reload}
                >
                    {'Reload'}
                </button>
                <div className='log__panel'>
                    {content}
                </div>
            </div>
        );
    }
}