forked from dropbox-dashbpard/error-detect-of-log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
114 lines (89 loc) · 3.21 KB
/
server.py
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
# -*- coding: utf-8 -*-
import os
import json
from flask import Flask, jsonify, request
from docopt import docopt
from ed import *
TAGS = {
"system_app_crash": app_crash,
"SYSTEM_TOMBSTONE": system_tombstone,
"system_app_wtf": app_wtf,
"system_app_anr": app_anr,
}
def root(p=None):
if p is not None and os.path.isabs(p):
return p
self_dir = os.path.dirname(os.path.abspath(__file__))
return os.path.join(self_dir, p) if p else self_dir
def make_console_log_handler():
import logging
import sys
h = logging.StreamHandler(sys.stdout)
h.setFormatter(logging.Formatter('[%(asctime)s] %(message)s'))
DOCOPT = """
Feature Detector: Start server
Usage:
server.py -c=<config_path>
server.py (-h | --help)
server.py --version
Options:
-c=<config_path> /path/to/config1
-h --help Show this screen
--version Show version
"""
opts = docopt(DOCOPT, version='v1')
app = Flask(__name__)
app.config.from_pyfile(root(opts['-c']))
app.logger.addHandler(make_console_log_handler())
@app.errorhandler(500)
def error_500_handler():
return jsonify(status=0, msg="server 500 error", features={})
@app.errorhandler(Exception)
def exception_handler():
return jsonify(status=0, msg='server exception', features={})
@app.route('/api/detect/<tag>', methods=['POST'])
def api_detect(tag):
if not tag in TAGS.keys():
return jsonify(status=0, msg='tag not supported', features={})
try:
data = json.loads(request.data).get('logcat')
if data:
features = TAGS[tag](data)
if features:
return jsonify(status=1, msg='feature detected', features=features)
else:
return jsonify(status=0, msg='failed to detect features', features={})
else:
return jsonify(status=0, msg='logcat field required', features={})
except Exception:
return jsonify(status=0, msg='failed to detect features', features={})
def run_app(app_):
host, port = '0.0.0.0', app_.config.get('SERVER_PORT', 9786)
if app_.config.get('DEBUG', False):
app_.logger.info("* run: flask")
app_.run(host=host, port=port)
else:
try:
import gunicorn.app.base
class GunicornApp(gunicorn.app.base.Application):
def __init__(self, app, opts):
self.app, self.opts = app, opts
super(GunicornApp, self).__init__()
def load_config(self):
cfg = gunicorn.app.base.Config()
for k, v in self.opts.items():
cfg.set(k, v)
self.cfg = cfg
def load(self):
return self.app
opts = dict(
bind='%s:%s' % (host, port),
workers=app_.config.get('GUNICORN_WORKERS', 1),
)
app_.logger.info("* run: gunicorn")
GunicornApp(app_, opts).run()
except ImportError:
app_.logger.info("* run: flask")
app_.run(host=host, port=port)
if __name__ == "__main__":
run_app(app)