Example #1
0
    def emit(self, record):
        """
        Handle a log record
        
        :param record: Log record as a string
        :return: True/False = Success/Fail
        """
        if len(self._records) >= self._max_len:
            self._records.pop(0)

        self._records.append(record)

        if record.exc_info:
            print termcolors.colorize('%s: %s Exception: ' %
                                      (record.name, record.msg),
                                      record.exc_info[0:-1],
                                      fg='red')
        else:
            console_text = self.format(record)
            if record.levelno == logging.DEBUG:
                print termcolors.colorize(console_text, fg='white')
            elif (record.levelno > logging.DEBUG) and (record.levelno <
                                                       logging.WARNING):
                print termcolors.colorize(console_text, fg='green')
            elif (record.levelno >= logging.WARNING) and (record.levelno <
                                                          logging.ERROR):
                print termcolors.colorize(console_text, fg='yellow')
            elif record.levelno >= logging.ERROR:
                print termcolors.colorize(console_text, fg='red')
            else:
                print '%s: %s' % (record.name, record.msg)
Example #2
0
    def emit(self, record):
        """
        Handle a log record
        :param record: Log record as a string
        :return: True/False - Success/Fail
        """
        if len(self._records) >= self._max_len:
            self._records.pop(0)

        self._records.append(record)

        if record.exc_info:
            print termcolors.colorize('%s: %s Exception: ' % (record.name, record.msg), record.exc_info[0:-1],
                                      fg='red')
        else:
            console_text = self.format(record)
            if record.levelno == logging.DEBUG:
                print termcolors.colorize(console_text, fg='white')
            elif (record.levelno > logging.DEBUG) and (record.levelno < logging.WARNING):
                print termcolors.colorize(console_text, fg='green')
            elif (record.levelno >= logging.WARNING) and (record.levelno < logging.ERROR):
                print termcolors.colorize(console_text, fg='yellow')
            elif record.levelno >= logging.ERROR:
                print termcolors.colorize(console_text, fg='red')
            else:
                print '%s: %s' % (record.name, record.msg)
Example #3
0
 def format(self, record):
     if record.levelname in COLORS:
         colors = COLORS[record.levelname]
         kwargs = {'fg': colors[0]}
         if colors[1]:
             kwargs['bg'] = colors[1]
         record.msg = colorize(record.msg, **kwargs)
     if record.levelname == 'INFO':
         if 'success' in record.msg or 'OK' in record.msg:
             record.msg = colorize(record.msg, fg='green')
     return super(ColorFormatter, self).format(record)
Example #4
0
    def print_messages(self, num_to_print=1):
        """
        Print log messages stored in FIFO
        :param num_to_print:
        """

        for ctr, record in enumerate(self._records):
            if ctr == num_to_print:
                break
            if record.exc_info:
                print termcolors.colorize('%s: %s Exception: ' %
                                          (record.name, record.msg),
                                          record.exc_info[0:-1],
                                          fg='red')
            else:
                if record.levelno < logging.WARNING:
                    print termcolors.colorize('%s: %s' %
                                              (record.name, record.msg),
                                              fg='green')
                elif (record.levelno >= logging.WARNING) and (record.levelno <
                                                              logging.ERROR):
                    print termcolors.colorize('%s: %s' %
                                              (record.name, record.msg),
                                              fg='yellow')
                elif record.levelno >= logging.ERROR:
                    print termcolors.colorize('%s: %s' %
                                              (record.name, record.msg),
                                              fg='red')
                else:
                    print '%s: %s' % (record.name, record.msg)
Example #5
0
  def __init__(self, init_file = None):
    self.prompt = "%s # " % colorize("torsh", fg="red", opts=("bold",))
    self.completekey = "\t"
    self.cmdqueue = []
    self.stdout = sys.stdout
    TorUtil.logfile = "torsh.log"

    self._socket = None
    self._connection = None
    self._threads = None

    self._path = None
    self._selmgr = PathSupport.SelectionManager(
        pathlen=3,
        order_exits=True,
        percent_fast=80,
        percent_skip=0,
        min_bw=1024,
        use_all_exits=True,
        uniform=True,
        use_exit=None,
        use_guards=True)

    self._do_aliases()

    if init_file:
      try:
        self._execute_file(init_file)
      except Exception as e:
        print(e)
Example #6
0
 def printMessages(self):
     for i in self._records:
         if i.exc_info:
             print termcolors.colorize('%s: %s Exception: '%(i.name,i.msg),i.exc_info[0:-1],fg='red')
         else:
             if i.levelno < logging.WARNING:
                 print termcolors.colorize('%s: %s'%(i.name,i.msg),fg='green')
             elif (i.levelno >= logging.WARNING) and (i.levelno < logging.ERROR):
                 print termcolors.colorize('%s: %s'%(i.name,i.msg),fg='yellow')
             elif i.levelno >= logging.ERROR:
                 print termcolors.colorize('%s: %s'%(i.name,i.msg),fg='red')
             else:
                 print '%s: %s'%(i.name,i.msg)
Example #7
0
    def print_messages(self, num_to_print=1):
        """
        Print log messages stored in FIFO
        :param num_to_print:
        :return:
        """

        for ctr, record in enumerate(self._records):
            if ctr == num_to_print:
                break
            if record.exc_info:
                print termcolors.colorize('%s: %s Exception: ' % (record.name, record.msg), record.exc_info[0:-1],
                                          fg='red')
            else:
                if record.levelno < logging.WARNING:
                    print termcolors.colorize('%s: %s' % (record.name, record.msg), fg='green')
                elif (record.levelno >= logging.WARNING) and (record.levelno < logging.ERROR):
                    print termcolors.colorize('%s: %s' % (record.name, record.msg), fg='yellow')
                elif record.levelno >= logging.ERROR:
                    print termcolors.colorize('%s: %s' % (record.name, record.msg), fg='red')
                else:
                    print '%s: %s' % (record.name, record.msg)
Example #8
0
  def do_connect(self, data):
    """ 
    Connects to a Tor instance by ControlSocket or ControlPort.

    Usage:
      connect --method=port --host=<host> --port=<port number> --pass=<passphrase>
      or
      connect --method=socket --path=<socket_path>
    """

    controlport = True
    host = "localhost"
    port = 9051
    passphrase = None
    path = None

    try:
      opts, args = getopt.getopt(data.split(" "), "m:", \
          ["method=", "host=", "port=", "pass="******"path="])

      for opt, arg in opts:
        if opt == "--method":
          if arg == "port":
            controlport = True
          elif arg == "socket":
            controlport = False
          else:
            raise Exception("Unkown method for connection. \
                Use sock for ControlSocket and port for ControlPort")
        elif opt == "--host":
          host = arg
        elif opt == "--port":
          port = int(arg)
        elif opt == "--pass":
          passphrase = arg
        elif opt == "--path":
          path = arg

      if not controlport:
        raise Exception("ControlSocket connection isn't supported yet, \
            use ControlPort instead.")

      self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      self._socket.connect((host,port))
      self._connection = PathSupport.Connection(self._socket)
      self._connection.debug(file("control.log", "w", buffering=0))
      self._threads = self._connection.launch_thread()
      auth_type, auth_value = self._connection.get_auth_type(), ""
      
      if auth_type == TorCtl.AUTH_TYPE.PASSWORD:
        if passphrase: auth_value = passphrase
        else:
          try: auth_value = getpass.getpass()
          except KeyboardInterrupt: return None
      elif auth_type == TorCtl.AUTH_TYPE.COOKIE:
        auth_value = self._connection.get_auth_cookie_path()

      self._connection.authenticate(auth_value)

      self._path = PathSupport.PathBuilder(self._connection, self._selmgr)
      self._connection.set_event_handler(self._path)
      self._connection.set_events([TorCtl.EVENT_TYPE.STREAM,
        TorCtl.EVENT_TYPE.BW,
        TorCtl.EVENT_TYPE.NEWCONSENSUS,
        TorCtl.EVENT_TYPE.NEWDESC,
        TorCtl.EVENT_TYPE.CIRC,
        TorCtl.EVENT_TYPE.STREAM_BW], True)

      self.prompt = "%s@%s:%s # " % (colorize("torsh", fg="red", opts=("bold",)),\
          colorize(host, fg="blue"),\
          colorize(str(port), fg="blue"))

    except getopt.GetoptError:
      # TODO: implement a generic usage function
      # self.usage() 
      print("ERROR at the arguments")
      return
    except Exception as e:
      print("ERROR:", e[0])