Compare commits

...

1 Commits

Author SHA1 Message Date
Arinerron
39ac87b6ad
Improve logging, mostly 2019-07-06 12:54:52 -07:00
8 changed files with 54 additions and 45 deletions

View File

@ -32,6 +32,7 @@ class colors:
strikethrough='\033[09m'
invisible='\033[08m'
italics='\033[3m'
class fg:
black='\033[30m'
red='\033[31m'
@ -64,9 +65,7 @@ class colors:
'''
Use the bitwise operators to merge these settings
'''
USE_ANSI = 0b100
if os.name == 'nt':
USE_ANSI = 0b000
USE_ANSI = 0b000 if os.name == 'nt' else 0b100
OUTPUT_TO_CONSOLE = 0b010
OUTPUT_TO_FILE = 0b001
@ -150,6 +149,7 @@ def log(prefix, data, color = '', timestamp=True, fd = sys.stdout, prompt = True
data : The actual data to output
color : The color to output before the data
'''
curTime = ''
if timestamp:
curTime = time.strftime("%m-%d %H:%M:%S") + ' '
@ -210,33 +210,33 @@ def confirm(default = 'y', message = 'Are you sure %s? '):
return default == 'y'
# debug: when there is info that could be useful for debugging purposes only
def debug(data, error = None, timestamp = True, prompt = True, terminal = False, level = LEVEL_DEBUG):
def debug(data, error = None, timestamp = True, prompt = True, terminal = True, level = LEVEL_DEBUG):
if get_level() <= level:
log('/', data, timestamp = timestamp, prompt = prompt, terminal = terminal)
if not error is None:
debug('Error: ' + str(error) + parse_error())
# info: when there is something to notify the user of, such as the success of a process
def info(data, timestamp = False, prompt = True, terminal = False, level = LEVEL_INFO):
def info(data, timestamp = False, prompt = True, terminal = True, level = LEVEL_INFO):
if get_level() <= level:
log('+', data, colors.fg.green, timestamp = timestamp, prompt = prompt, terminal = terminal)
# warn: when there is a potential for something bad to happen
def warn(data, error = None, timestamp = True, prompt = True, terminal = False, level = LEVEL_WARN):
def warn(data, error = None, timestamp = True, prompt = True, terminal = True, level = LEVEL_WARN):
if not error is None:
debug('Error: ' + str(error) + parse_error())
if get_level() <= level:
log('!', data, colors.fg.orange, timestamp = timestamp, prompt = prompt, terminal = terminal)
# error: when only one function, module, or process of the program encountered a problem and must stop
def error(data, error = None, timestamp = True, prompt = True, terminal = False, level = LEVEL_ERROR):
def error(data, error = None, timestamp = True, prompt = True, terminal = True, level = LEVEL_ERROR):
if get_level() <= level:
log('-', data, colors.fg.red, timestamp = timestamp, fd = sys.stderr, prompt = prompt, terminal = terminal)
if not error is None:
debug('Error: ' + str(error) + parse_error())
# fatal: when the something so bad has happened that the program must stop
def fatal(data, error = None, timestamp=True, prompt = True, terminal = False, level = LEVEL_FATAL):
def fatal(data, error = None, timestamp = True, prompt = True, terminal = True, level = LEVEL_FATAL):
if not error is None:
debug('Error: ' + str(error) + parse_error(), terminal = terminal)
if get_level() <= level:
@ -248,6 +248,9 @@ def parse_error():
output = ''
for line in details:
output += '\n ... module %s in %s:%i' % (line[2], line[0], line[1])
output += ('\n ... ' + colors.bold + '%s' + colors.reset + ' in %s:' + colors.bold + '%i' + colors.reset) % (line[2], line[0], line[1])
if not (get_settings() & USE_ANSI):
output = colors.filter(output)
return output

View File

@ -134,13 +134,8 @@ class Onionr:
# initialize plugins
events.event('init', onionr = self, threaded = False)
command = ''
try:
command = sys.argv[1].lower()
except IndexError:
command = ''
finally:
self.execute(command)
command = sys.argv[1].lower() if len(sys.argv) >= 2 else ''
self.execute(command)
return
@ -285,8 +280,7 @@ class Onionr:
# define commands
commands = self.getCommands()
command = commands.get(argument, self.notFound)
command()
return commands.get(argument, self.notFound)()
def version(self, verbosity = 5, function = logger.info):
'''

View File

@ -151,12 +151,16 @@ class Block:
Outputs:
- (bool): indicates whether or not the operation was successful
'''
try:
# import from string
blockdata = data
# import from file
if blockdata is None:
if self.getHash() is None:
return None
try:
blockdata = onionrstorage.getData(self.core, self.getHash()).decode()
except AttributeError:
@ -190,7 +194,7 @@ class Block:
if len(self.getRaw()) <= config.get('allocations.blockCache', 500000):
self.cache()
if self.autoDecrypt:
self.decrypt()
@ -677,7 +681,7 @@ class Block:
'''
if isinstance(bHash, Block):
bHash = bHash.getHash()
ret = isinstance(onionrstorage.getData(onionrcore.Core(), bHash), type(None))
return not ret

View File

@ -164,4 +164,4 @@ cmd_help = {
'change-id': 'Change active ID',
'open-home': 'Open your node\'s home/info screen',
'reset-tor': 'Delete the Tor data directory. Only do this if Tor never starts.'
}
}

View File

@ -24,8 +24,8 @@ from onionrutils import bytesconverter
config.reload()
def getDifficultyModifier(coreOrUtilsInst=None):
'''Accepts a core or utils instance returns
the difficulty modifier for block storage based
'''Accepts a core or utils instance returns
the difficulty modifier for block storage based
on a variety of factors, currently only disk use.
'''
classInst = coreOrUtilsInst
@ -110,14 +110,14 @@ class DataPOW:
self.data = self.data.encode()
except AttributeError:
pass
self.data = nacl.hash.blake2b(self.data)
logger.info('Computing POW (difficulty: %s)...' % self.difficulty)
logger.debug('Computing POW (difficulty: %s)...' % self.difficulty)
self.mainHash = '0' * 70
self.puzzle = self.mainHash[0:min(self.difficulty, len(self.mainHash))]
myCore = core.Core()
for i in range(max(1, threadCount)):
t = threading.Thread(name = 'thread%s' % i, target = self.pow, args = (True,myCore))
@ -131,7 +131,7 @@ class DataPOW:
answer = ''
heartbeat = 200000
hbCount = 0
while self.hashing:
rand = nacl.utils.random()
token = nacl.hash.blake2b(rand + self.data).decode()
@ -141,7 +141,7 @@ class DataPOW:
self.hashing = False
iFound = True
break
if iFound:
endTime = math.floor(time.time())
if self.reporting:
@ -160,12 +160,12 @@ class DataPOW:
'''
Returns the result then sets to false, useful to automatically clear the result
'''
try:
retVal = self.result
except AttributeError:
retVal = False
self.result = False
return retVal
@ -207,15 +207,15 @@ class POW:
self.data = self.data.encode()
except AttributeError:
pass
if forceDifficulty > 0:
self.difficulty = forceDifficulty
else:
# Calculate difficulty. Dumb for now, may use good algorithm in the future.
self.difficulty = getDifficultyForNewBlock(bytes(json_metadata + b'\n' + self.data), coreInst=myCore)
logger.info('Computing POW (difficulty: %s)...' % self.difficulty)
logger.debug('Computing POW (difficulty: %s)...' % self.difficulty)
self.mainHash = '0' * 64
self.puzzle = self.mainHash[0:min(self.difficulty, len(self.mainHash))]
@ -251,7 +251,7 @@ class POW:
self.result = payload
break
nonce += 1
if iFound:
endTime = math.floor(time.time())
if self.reporting:
@ -268,12 +268,12 @@ class POW:
'''
Returns the result then sets to false, useful to automatically clear the result
'''
try:
retVal = self.result
except AttributeError:
retVal = False
self.result = False
return retVal
@ -292,4 +292,4 @@ class POW:
except KeyboardInterrupt:
self.shutdown()
logger.warn('Got keyboard interrupt while waiting for POW result, stopping')
return result
return result

View File

@ -45,4 +45,4 @@ def import_new_blocks(core_inst=None, scanDir=''):
else:
logger.warn('Failed to verify hash for %s' % block, terminal=True)
if not exist:
logger.info('No blocks found to import', terminal=True)
logger.debug('No blocks found to import', terminal=True)

View File

@ -226,7 +226,15 @@ def pluginToBlock(plugin, import_block = True):
except:
pass
metadata = {'author' : author, 'date' : str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')), 'name' : plugin, 'info' : info, 'compiled-by' : plugin_name, 'content' : data.decode('utf-8'), 'description' : description}
metadata = {
'author' : author,
'date' : str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')),
'name' : plugin,
'info' : info,
'compiled-by' : plugin_name,
'content' : data.decode('utf-8'),
'description' : description
}
block = Block(core = pluginapi.get_core())
@ -250,6 +258,7 @@ def pluginToBlock(plugin, import_block = True):
def installBlock(block):
try:
block = Block(block, core = pluginapi.get_core())
print(block.getContent())
blockContent = json.loads(block.getContent())
name = sanitize(blockContent['name'])

View File

@ -29,7 +29,7 @@ class SubprocessPOW:
def __init__(self, data, metadata, core_inst=None, subproc_count=None):
'''
Onionr proof of work using multiple processes
Accepts block data, block metadata
Accepts block data, block metadata
and optionally an onionr core library instance.
if subproc_count is not set, os.cpu_count() is used to determine the number of processes
@ -55,8 +55,8 @@ class SubprocessPOW:
self.data = bytesconverter.str_to_bytes(data)
# Calculate difficulty. Dumb for now, may use good algorithm in the future.
self.difficulty = onionrproofs.getDifficultyForNewBlock(bytes(json_metadata + b'\n' + self.data), coreInst=self.core_inst)
logger.info('Computing POW (difficulty: %s)...' % self.difficulty)
logger.debug('Computing POW (difficulty: %s)...' % self.difficulty)
self.mainHash = '0' * 64
self.puzzle = self.mainHash[0:min(self.difficulty, len(self.mainHash))]
@ -74,7 +74,7 @@ class SubprocessPOW:
else:
self.shutdown = True
return self.payload
def _spawn_proc(self):
# Create a child proof of work process, wait for data and send shutdown signal when its found
parent_conn, child_conn = Pipe()
@ -117,4 +117,3 @@ class SubprocessPOW:
pipe.send(payload)
break
nonce += 1