Skip to content

Instantly share code, notes, and snippets.

@spikeekips
Last active August 27, 2019 05:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spikeekips/eac61a00f5e855165e377744f6a7e7ef to your computer and use it in GitHub Desktop.
Save spikeekips/eac61a00f5e855165e377744f6a7e7ef to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import os
import time
import functools
import json
from colors import green, blue, red
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
F = None
L = 0
def tail(ignore_emptyline=None):
global F
global L
buf = []
reached_end = False
while True:
F.seek(L)
a = F.read()
if len(a) < 1:
reached_end = True
continue
L += len(a)
if '\n' not in a:
buf.append(a)
continue
buf.append(a)
d = []
for i in ''.join(buf):
if i == '\n':
o = ''.join(d)
if ignore_emptyline == True and len(o.strip()) < 1:
pass
else:
yield (o, reached_end)
d = []
continue
d.append(i)
buf = d
#printed = []
def print_records(nodes, height, l):
for n in nodes:
orphan = n not in l
o = l.get(n, dict(node=n, block=dict(height=height)))
print(
'%s:%2s - %s - %-33s - %s' % (
green('%3d' % o.get('block', dict()).get('height')),
o.get('block', dict()).get('round', ''),
red('%-6s' % o['node']) if orphan else blue('%-6s' % o['node']),
o.get('t', ''),
o.get('block', dict()).get('hash', ''),
))
return
def update_file():
global F
global L
f = 'logs/contest/latest/all.log'
F = open(os.path.abspath(os.path.expanduser(f)))
L = 0
print('='*100)
class LoggingEventHandler(FileSystemEventHandler):
def on_modified(self, event):
super(LoggingEventHandler, self).on_modified(event)
update_file()
def on_created(self, event):
super(LoggingEventHandler, self).on_created(event)
update_file()
def sort_nodes(a, b):
return int(a[1:]) - int(b[1:])
if __name__ == '__main__':
update_file()
observer = Observer()
observer.schedule(LoggingEventHandler(), 'logs/contest/latest/')
observer.start()
# get all nodes
last_height = 0
nodes = []
ol = dict()
update_nodes = False
updated_at = None
for i, reached_end in tail(ignore_emptyline=True):
try:
o = json.loads(i)
except:
continue
if 'node' not in o or 'm' not in o:
continue
if 'node created' in o['m']:
nodes.append(o['node'])
update_nodes = True
if 'new block' not in o['m']:
continue
if last_height > o['block']['height']:
continue
if last_height < o['block']['height']:
last_height = o['block']['height']
ol = dict()
ol[o['node']] = o
if update_nodes:
nodes = sorted(nodes, key=functools.cmp_to_key(sort_nodes))
if reached_end:
print(chr(27) + "[2J")
print('-'*100)
print_records(nodes, last_height, ol)
print('-'*100)
if reached_end:
time.sleep(0.01)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment