File: json_log_viewer.py

package info (click to toggle)
sheepdog 0.8.3-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,364 kB
  • ctags: 3,951
  • sloc: ansic: 30,552; sh: 3,573; perl: 2,924; asm: 453; makefile: 391; python: 192
file content (254 lines) | stat: -rwxr-xr-x 6,159 bytes parent folder | download | duplicates (3)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#! /usr/bin/env python

import sys, os, errno
import json, curses
import atexit

begin_sec, begin_usec = -1, -1

class LogRecord(object):
    def __init__(self, json_line, proc):
        json_obj = json.loads(json_line)

        user_info = json_obj['user_info']
        self.progname = user_info['program_name']
        self.port = user_info['port']

        body = json_obj['body']
        self.timestamp = { 'sec': body['second'], 'usec': body['usecond']}
        self.worker_name = body['worker_name']
        self.worker_idx = body['worker_idx']
        self.func = body['func']
        self.line = body['line']
        self.msg = body['msg']

        self.proc = proc
        self.color = None

    def is_sheep(self):
        return self.progname == 'sheep'

    def get_color(self):
        return self.proc.color

    def pop(self):
        ret = self.proc.__pop_next_record__()
        assert ret == self
        return ret

    def __lt__(self, other):
        if self.timestamp['sec'] < other.timestamp['sec']:
            return True
        elif other.timestamp['sec'] < self.timestamp['sec']:
            return False

        if self.timestamp['usec'] < other.timestamp['usec']:
            return True

        return False

    def format_line(self, max_x):
        sec = self.timestamp['sec']
        usec = self.timestamp['usec']
        udelta = usec - begin_usec
        if udelta < 0:
            udelta += 1000000
            sec -= 1
        t = '%d.%06d' % (sec - begin_sec, udelta)

        ret = '%s+%s: ' % (' ' * (10 - len(t[:10])), t[:10])
        if self.progname == 'sheep':
            hdr = 'sheep %d,%s(%d) ' % \
                (self.port, self.func, self.line)
            ret += hdr[:40] + ' ' * (40 - len(hdr[:40]) + 1)
            ret += self.msg

        return ret[:max_x - 1]

        return self.msg

class Process(object):
    def __init__(self, log_file_path):
        self.log_file = open(log_file_path)

        self.next_record = None
        self.color = None

    def set_color(self, color):
        self.color = color

    def peek_next_record(self):
        if self.next_record == None:
            next_line = self.log_file.readline()
            if next_line == '':
                # end of the log
                return None

            self.next_record = LogRecord(next_line, self)
        return self.next_record

    # __pop_next_record__() must be called by LogRecord
    def __pop_next_record__(self):
        assert self.next_record != None
        ret = self.next_record
        self.next_record = None
        return ret

dying_msg = ''

w = None
curses_colors = [
    curses.COLOR_RED,
    curses.COLOR_GREEN,
    curses.COLOR_YELLOW,
    curses.COLOR_BLUE,
    curses.COLOR_MAGENTA,
    curses.COLOR_CYAN,
    ]
nr_curses_colors = len(curses_colors)

def init_curses():
    global w

    w = curses.initscr()
    curses.nonl()
    curses.cbreak()
    curses.noecho()

    curses.start_color()
    for i in range(1, nr_curses_colors + 1):
        curses.init_pair(i, curses_colors[i - 1], curses.COLOR_BLACK)

def assign_color(procs):
    sheeps = []
    for proc in procs:
        if proc.peek_next_record().is_sheep():
            sheeps.append(proc)
    nr_sheeps = len(sheeps)

    if nr_curses_colors < nr_sheeps:
        # we don't have enough colors to assign...
        return

    for i in range(0, nr_sheeps):
        sheeps[i].set_color(i + 1)

current_y = 0
max_y, max_x = 0, 0
records = []
records_len = 0

def unify_records(procs):
    first_rec = procs[0].peek_next_record()
    for proc in procs[1:]:
        rec = proc.peek_next_record()
        if rec < first_rec:
            first_rec = rec

    records.append(first_rec.pop())

    global begin_sec, begin_usec
    begin_sec = first_rec.timestamp['sec']
    begin_usec = first_rec.timestamp['usec']

    nr_procs = len(procs)
    is_empty = [False] * nr_procs
    nr_empteis = 0
    while nr_empteis != nr_procs:
        next_rec = None

        for i in range(0, nr_procs):
            if is_empty[i]:
                continue

            proc = procs[i]
            rec = proc.peek_next_record()

            if rec == None:
                is_empty[i] = True
                nr_empteis += 1
                continue

            if next_rec == None:
                next_rec = rec
                continue

            if rec < next_rec:
                next_rec = rec
                continue

        if next_rec == None:
            assert nr_empteis == nr_procs
            break

        records.append(next_rec.pop())

def update_terminal():
    w.clear()

    for i in range(0, max_y):
        w.move(i, 0)
        if not current_y + i < records_len:
            break

        record = records[current_y + i]

        color = record.get_color()
        if color:
            w.attrset(curses.color_pair(color))

        w.addstr(record.format_line(max_x))

        if color:
            w.attroff(curses.color_pair(color))

    w.refresh()

if __name__ == '__main__':
    @atexit.register
    def exit_handler():
        curses.endwin()
        if dying_msg != '':
            print dying_msg + '\n'

    init_curses()

    procs = map(lambda x: Process(x), sys.argv[1:])
    assign_color(procs)
    unify_records(procs)
    records_len = len(records)

    tty_file = open('/dev/tty', 'rb')

    max_y, max_x = w.getmaxyx()
    update_terminal()
    running = True

    while running:
        try:
            key = tty_file.read(1)
        except IOError, (enr, msg):
            if enr == errno.EINTR:
                continue

            dying_msg = 'fatal error: %s' % \
                (os.strerror(enr))
            break

        if key == 'q':
            break
        elif key == 'j':
            if current_y + 1 < records_len:
                current_y += 1
        elif key == 'k':
            if current_y:
                current_y -= 1
        elif key == ' ':
            if current_y + max_y < records_len:
                current_y += max_y
        elif key == 'g':
            current_y = 0
        elif key == 'G':
            current_y = records_len - max_y

        update_terminal()