示例#1
0
    def __init__(self, miner, verbose=False, statusfile=None, blkfound=None):
        self.verbose = verbose
        self.miner = miner
        self.lastUpdate = time() - 1
        self.rate = 0
        self.accepted = 0
        self.invalid = 0
        self.lineLength = 0
        self.connectionType = None
        self.idle = False

        self.statushandler = None
        if statusfile:
            from statusfile import StatusFile
            self.statushandler = StatusFile(path=statusfile)

        self.blkfound = blkfound
示例#2
0
 def __init__(self, miner, verbose=False, statusfile=None, blkfound=None):
     self.verbose = verbose
     self.miner = miner
     self.lastUpdate = time() - 1
     self.rate = 0
     self.accepted = 0
     self.invalid = 0
     self.lineLength = 0
     self.connectionType = None
     self.idle = False
     
     self.statushandler = None
     if statusfile:
         from statusfile import StatusFile
         self.statushandler = StatusFile(path=statusfile)
     
     self.blkfound = blkfound
示例#3
0
class ConsoleLogger(object):
    """This class will handle printing messages to the console."""

    TIME_FORMAT = '[%d/%m/%Y %H:%M:%S]'

    UPDATE_TIME = 1.0

    def __init__(self, miner, verbose=False, statusfile=None, blkfound=None):
        self.verbose = verbose
        self.miner = miner
        self.lastUpdate = time() - 1
        self.rate = 0
        self.accepted = 0
        self.invalid = 0
        self.lineLength = 0
        self.connectionType = None
        self.idle = False

        self.statushandler = None
        if statusfile:
            from statusfile import StatusFile
            self.statushandler = StatusFile(path=statusfile)

        self.blkfound = blkfound

    def reportRate(self, rate, update=True):
        """Used to tell the logger the current Khash/sec."""
        self.rate = rate
        if self.statushandler:
            self.statushandler.update('HashRate', rate if not self.idle else 0)
        if update:
            self.updateStatus()

    def reportType(self, type):
        self.connectionType = type

    def reportBlock(self, block):
        self.log('Currently on block: ' + str(block))

    def reportFound(self, hash, accepted, diff=0.0):
        if accepted:
            self.accepted += 1
        else:
            self.invalid += 1

        hexHash = hash[::-1]
        hexHash = hexHash[:8].encode('hex')

        if self.statushandler:
            self.statushandler.update('LastBlockFoundTime', int(time()))
            self.statushandler.update('LastBlockFoundDiff', diff)
            self.statushandler.increment('NumBlocksFound')

        self.blkfound and subprocess.Popen(
            ('%s %s %s diff %.2f' %
             (self.blkfound, hexHash, accepted and 'accepted'
              or 'rejected', diff)).split(' '))

        if self.verbose:
            self.log('Result ...%s %s diff %.2f' %
                     (hexHash, 'accepted' if accepted else 'rejected', diff))
        else:
            self.log(
                'Result: %s %s diff %.2f' %
                (hexHash[8:], 'accepted' if accepted else 'rejected', diff))

    def reportMsg(self, message):
        self.log(('MSG: ' + message), True, True)

    def reportConnected(self, connected):
        if connected:
            self.log('Connected to server')
        else:
            self.log('Disconnected from server')

    def reportConnectionFailed(self):
        self.log('Failed to connect, retrying...')

    def reportDebug(self, message):
        if self.verbose:
            self.log(message)

    def updateStatus(self, force=False):
        #only update if last update was more than a second ago
        dt = time() - self.lastUpdate
        if force or dt > self.UPDATE_TIME:
            rate = self.rate if (not self.miner.idle) else 0
            type = " [" + str(
                self.connectionType
            ) + "]" if self.connectionType is not None else ''
            status = ("[" + formatNumber(rate) + "hash/sec] "
                      "[" + str(self.accepted) + " Accepted] "
                      "[" + str(self.invalid) + " Rejected]" + type)
            self.say(status)
            if self.statushandler:
                self.statushandler.update('HashRate', rate)
                self.statushandler.update('Accepted', self.accepted)
                self.statushandler.update('Rejected', self.invalid)
            self.lastUpdate = time()

    def say(self, message, newLine=False, hideTimestamp=False):
        #add new line if requested
        if newLine:
            message += '\n'
            if hideTimestamp:
                timestamp = ''
            else:
                timestamp = datetime.now().strftime(self.TIME_FORMAT) + ' '

            message = timestamp + message

        #erase the previous line
        if self.lineLength > 0:
            sys.stdout.write('\b \b' * self.lineLength)
            sys.stdout.write(' ' * self.lineLength)
            sys.stdout.write('\b \b' * self.lineLength)

        #print the line
        sys.stdout.write(message)
        sys.stdout.flush()

        #cache the current line length
        if newLine:
            self.lineLength = 0
        else:
            self.lineLength = len(message)

    def log(self, message, update=True, hideTimestamp=False):
        self.say(message, True, hideTimestamp)
        if update:
            self.updateStatus(True)
示例#4
0
class ConsoleLogger(object):
    """This class will handle printing messages to the console."""
    
    TIME_FORMAT = '[%d/%m/%Y %H:%M:%S]'
    
    UPDATE_TIME = 1.0
    
    def __init__(self, miner, verbose=False, statusfile=None, blkfound=None):
        self.verbose = verbose
        self.miner = miner
        self.lastUpdate = time() - 1
        self.rate = 0
        self.accepted = 0
        self.invalid = 0
        self.lineLength = 0
        self.connectionType = None
        self.idle = False
        
        self.statushandler = None
        if statusfile:
            from statusfile import StatusFile
            self.statushandler = StatusFile(path=statusfile)
        
        self.blkfound = blkfound
    
    def reportRate(self, rate, update=True):
        """Used to tell the logger the current Khash/sec."""
        self.rate = rate
        if self.statushandler:
            self.statushandler.update('HashRate', rate if not self.idle else 0)
        if update:
            self.updateStatus()
    
    def reportType(self, type):
        self.connectionType = type
    
    def reportBlock(self, block):
        self.log('Currently on block: ' + str(block))
        
    def reportFound(self, hash, accepted, diff=0.0):
        if accepted:
            self.accepted += 1
        else:
            self.invalid += 1
        
        hexHash = hash[::-1]
        hexHash = hexHash[:8].encode('hex')
        
        if self.statushandler:
            self.statushandler.update('LastBlockFoundTime',int(time()))
            self.statushandler.update('LastBlockFoundDiff',diff)
            self.statushandler.increment('NumBlocksFound')
        
        self.blkfound and subprocess.Popen(('%s %s %s diff %.2f' % (self.blkfound,hexHash,accepted and 'accepted' or 'rejected',diff)).split(' '))
        
        if self.verbose:
            self.log('Result ...%s %s diff %.2f' % (hexHash,
                'accepted' if accepted else 'rejected',diff))
        else:
            self.log('Result: %s %s diff %.2f' % (hexHash[8:],
                'accepted' if accepted else 'rejected',diff))
            
    def reportMsg(self, message):
        self.log(('MSG: ' + message), True, True)
    
    def reportConnected(self, connected):
        if connected:
            self.log('Connected to server')
        else:
            self.log('Disconnected from server')
    
    def reportConnectionFailed(self):
        self.log('Failed to connect, retrying...')
    
    def reportDebug(self, message):
        if self.verbose:
            self.log(message)
        
    def updateStatus(self, force=False):
        #only update if last update was more than a second ago
        dt = time() - self.lastUpdate
        if force or dt > self.UPDATE_TIME:
            rate = self.rate if (not self.miner.idle) else 0
            type = " [" + str(self.connectionType) + "]" if self.connectionType is not None else ''
            status = (
                "[" + formatNumber(rate) + "hash/sec] "
                "[" + str(self.accepted) + " Accepted] "
                "[" + str(self.invalid) + " Rejected]" + type)
            self.say(status)
            if self.statushandler:
                self.statushandler.update('HashRate',rate)
                self.statushandler.update('Accepted',self.accepted)
                self.statushandler.update('Rejected',self.invalid)
            self.lastUpdate = time()
        
    def say(self, message, newLine=False, hideTimestamp=False):
        #add new line if requested
        if newLine:
            message += '\n'
            if hideTimestamp:
                timestamp = ''
            else:
                timestamp = datetime.now().strftime(self.TIME_FORMAT) + ' '
                
            message = timestamp + message
        
        #erase the previous line
        if self.lineLength > 0:
            sys.stdout.write('\b \b' * self.lineLength)
            sys.stdout.write(' ' * self.lineLength)
            sys.stdout.write('\b \b' * self.lineLength)

        #print the line
        sys.stdout.write(message)
        sys.stdout.flush()
        
        #cache the current line length
        if newLine:
            self.lineLength = 0
        else:
            self.lineLength = len(message)

    def log(self, message, update=True, hideTimestamp=False):
        self.say(message, True, hideTimestamp)
        if update:
            self.updateStatus(True)