Example #1
0
class LogEcho(threading.Thread):
    def __init__(self, user, server, filename, target_lines=None, filters=[]):
        self.logfile = filename
        threading.Thread.__init__(self)
        self.remote = RemoteConnection(user, server)
        self.count = 0
        self.target_lines = target_lines
        self.show_progress_bar = self.target_lines is not None
        self.process = None
        self.finished = False
        self.filters = filters

    def stop(self):
        print
        try:
            self.process.kill()
        except:
            print padded_log('An error occurred when stopping logger')

    def run(self):
        def tail_log(line, stdin, process):
            # At first line reception, store echo logger process
            # and instantiate progress bar if needed
            if self.process is None:
                self.process = process
                if self.show_progress_bar:
                    self.pacman = Pacman(text='    Progress')

            # Determine if we want to count/print the line based on filters setting
            matched_filter = re.search(r'({})'.format('|'.join(self.filters)), line)
            this_line_is_good = matched_filter or self.filters == []

            # If line is good and we're not reached the 100%
            if this_line_is_good and not self.finished:
                # Update progress bar if in progressbar mode
                if self.show_progress_bar:
                    self.count += 1
                    percent = (100 * self.count) / self.target_lines
                    percent = percent if percent <= 100 else 100
                    self.finished = percent == 100
                    self.pacman.progress(percent)
                # or print line
                else:
                    print '    ' + line.rstrip()

        try:
            code, stdout = self.remote.execute(
                'tail -F -n0 {}'.format(self.logfile),
                _out=tail_log
            )
        except:
            pass
Example #2
0
        def tail_log(line, stdin, process):
            # At first line reception, store echo logger process
            # and instantiate progress bar if needed
            if self.process is None:
                self.process = process
                if self.show_progress_bar:
                    self.pacman = Pacman(text='    Progress')

            # Determine if we want to count/print the line based on filters setting
            matched_filter = re.search(r'({})'.format('|'.join(self.filters)), line)
            this_line_is_good = matched_filter or self.filters == []

            # If line is good and we're not reached the 100%
            if this_line_is_good and not self.finished:
                # Update progress bar if in progressbar mode
                if self.show_progress_bar:
                    self.count += 1
                    percent = (100 * self.count) / self.target_lines
                    percent = percent if percent <= 100 else 100
                    self.finished = percent == 100
                    self.pacman.progress(percent)
                # or print line
                else:
                    print '    ' + line.rstrip()