From 4a471fb8b434d3df07c5de42fc41590b5d9fc5f5 Mon Sep 17 00:00:00 2001 From: "Kali Kaneko (leap communications)" Date: Wed, 5 Oct 2016 12:18:02 -0400 Subject: [feature] logs watch command --- src/leap/bitmask/cli/bitmask_cli.py | 13 ++++++++++++- src/leap/bitmask/cli/logs.py | 28 +++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/leap/bitmask/cli/bitmask_cli.py b/src/leap/bitmask/cli/bitmask_cli.py index 736e7376..ab3c2069 100755 --- a/src/leap/bitmask/cli/bitmask_cli.py +++ b/src/leap/bitmask/cli/bitmask_cli.py @@ -20,6 +20,7 @@ Bitmask Command Line interface: zmq client. """ import json import sys +import signal from colorama import Fore from twisted.internet import reactor, defer @@ -125,15 +126,25 @@ def execute(): errb=lambda: cli.start(None)) cli.data = [] yield cli.execute(sys.argv[1:]) - yield reactor.stop() + try: + yield reactor.stop() + except: + pass def _null_printer(*args): pass + def main(): + def signal_handler(signal, frame): + if reactor.running: + reactor.stop() + sys.exit(0) + reactor.callWhenRunning(reactor.callLater, 0, execute) + signal.signal(signal.SIGINT, signal_handler) reactor.run() if __name__ == "__main__": diff --git a/src/leap/bitmask/cli/logs.py b/src/leap/bitmask/cli/logs.py index 59ed64d6..0976ebc9 100644 --- a/src/leap/bitmask/cli/logs.py +++ b/src/leap/bitmask/cli/logs.py @@ -21,6 +21,7 @@ import argparse import commands import os.path import sys +import time from colorama import Fore @@ -39,6 +40,7 @@ Bitmask Log Handling SUBCOMMANDS: send Send last bitmaskd log + watch Display logs stream '''.format(name=command.appname) def send(self, raw_args): @@ -46,14 +48,34 @@ SUBCOMMANDS: if not _bin: error('pastebinit not found. install it to upload logs.') return defer.succeed(None) - log_path = os.path.abspath( - os.path.join(get_path_prefix(), 'leap', 'bitmaskd.log')) output = commands.getoutput('{0} -b {1} {2}'.format( - _bin[0], 'paste.debian.net', log_path)) + _bin[0], 'paste.debian.net', _log_path)) uri = output.replace('debian.net/', 'debian.net/plain/') success(uri) return defer.succeed(None) + def watch(self, raw_args): + def tail(_file): + _file.seek(0,2) # Go to the end of the file + while True: + line = _file.readline() + if not line: + time.sleep(0.1) + continue + yield line + + _file = open(_log_path, 'r') + print (Fore.GREEN + '[bitmask] ' + + Fore.RESET + 'Watching log file %s' % _log_path ) + for line in _file.readlines(): + print line, + for line in tail(_file): + print line, + + +_log_path = os.path.abspath( + os.path.join(get_path_prefix(), 'leap', 'bitmaskd.log')) + def error(msg): print Fore.RED + msg + Fore.RESET -- cgit v1.2.3