summaryrefslogtreecommitdiff
path: root/src/leap/bitmask/cli/logs.py
blob: 0976ebc96736fff03ddb51c7f188dbb14f16f1f3 (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
# -*- coding: utf-8 -*-
# keys
# Copyright (C) 2016 LEAP
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
Bitmask Command Line interface: logs
"""
import argparse
import commands
import os.path
import sys
import time

from colorama import Fore

from twisted.internet import defer
from twisted.python.procutils import which

from leap.bitmask.cli import command
from leap.common.config import get_path_prefix


class Logs(command.Command):
    usage = '''{name} logs <subcommand>

Bitmask Log Handling

SUBCOMMANDS:

   send       Send last bitmaskd log
   watch      Display logs stream
'''.format(name=command.appname)

    def send(self, raw_args):
        _bin = which('pastebinit')
        if not _bin:
            error('pastebinit not found. install it to upload logs.')
            return defer.succeed(None)
        output = commands.getoutput('{0} -b {1} {2}'.format(
            _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


def success(msg):
    print Fore.GREEN + '[+] Bitmaskd logs pasted to ' + msg + Fore.RESET