1
0
Fork 0
mirror of https://github.com/snovvcrash/usbrip.git synced 2024-06-08 15:06:04 +02:00

Update logging logic when redirect output

This commit is contained in:
Sam Free5ide 2019-04-17 03:19:35 +03:00
parent 3ef0ab637d
commit 1546283141
4 changed files with 21 additions and 15 deletions

View File

@ -28,6 +28,7 @@ __brief__ = 'Common items.'
import os
import sys
import time
import random
from string import printable
from calendar import month_name
@ -80,7 +81,7 @@ E,N,S,I = list(map(lambda x: random.choice(x), (E,N,S,I)))
if cfg.ISATTY:
E,N,S,I = list(map(lambda x: colored(x, 'green', 'on_blue') + '\033[1;33m', (E,N,S,I)))
else:
mid_start = 55 + len(VERSION_FORMATTED)
mid_start = 62 + len(VERSION_FORMATTED)
mid_end = mid_start + 97
VERSION = '{v' + VERSION + '}'
BANNER = BANNER[31:55] + VERSION + BANNER[mid_start:mid_end] + SITE
@ -241,14 +242,18 @@ def is_correct(password):
# ----------------------------------------------------------
def _get_time(fmt='%H:%M:%S'):
return time.strftime(fmt, time.localtime())
def print_info(message):
if cfg.QUIET:
return
if cfg.ISATTY:
cprint(f'[INFO] {message}', 'green')
cprint(f'[{_get_time()}] [INFO] {message}', 'green')
else:
print(f'[INFO] {message}')
print(f'[{_get_time("%Y-%m-%d %H:%M:%S")}] [INFO] {message}')
def print_warning(message, *, errcode=0, initial_error=''):
@ -262,9 +267,9 @@ def print_warning(message, *, errcode=0, initial_error=''):
print(initial_error, file=sys.stderr)
if cfg.ISATTY:
cprint(f'[WARNING] {message}', 'yellow')
cprint(f'[{_get_time()}] [WARNING] {message}', 'yellow')
else:
print(f'[WARNING] {message}')
print(f'[{_get_time("%Y-%m-%d %H:%M:%S")}] [WARNING] {message}')
def print_critical(message, *, errcode=0, initial_error=''):
@ -275,19 +280,20 @@ def print_critical(message, *, errcode=0, initial_error=''):
print(initial_error, file=sys.stderr)
if cfg.ISATTY:
cprint(f'[CRITICAL] {message}', 'white', 'on_red', attrs=['bold'])
cprint(f'[{_get_time()}] [CRITICAL] {message}', 'white', 'on_red', attrs=['bold'])
else:
print(f'[CRITICAL] {message}')
print(f'[{_get_time("%Y-%m-%d %H:%M:%S")}] [CRITICAL] {message}')
def print_secret(message, *, secret=''):
if cfg.ISATTY:
cprint(
'[SECRET] {} {}'.format(
'[{}] [SECRET] {} {}'.format(
_get_time(),
colored(message, 'white', attrs=['bold']),
colored(secret, 'white', 'on_grey', attrs=['bold'])
),
'white', attrs=['bold']
)
else:
print(f'[SECRET] {message} {secret}')
print(f'[{_get_time("%Y-%m-%d %H:%M:%S")}] [SECRET] {message} {secret}')

View File

@ -541,12 +541,12 @@ def _represent_events(events_to_show, columns, table_data, title, repres):
event_table = _build_single_table(USBEvents.TableClass, table_data, title)
# Display as table
if repres['smart'] and event_table.ok or repres['table']:
if (repres['smart'] and event_table.ok or repres['table']) and cfg.ISATTY:
print_info('Representation: Table')
print('\n' + event_table.table)
# Display as list
elif repres['smart'] and not event_table.ok or repres['list']:
elif (repres['smart'] and not event_table.ok or repres['list']) or not cfg.ISATTY:
if not event_table.ok:
print_warning('Terminal window is too small to display table properly')
print_warning('Representation: List')

View File

@ -36,7 +36,6 @@ START = time.time()
def tick(msg, fmt='%H:%M:%S', taken=None):
#fmt = '%Y-%m-%d %H:%M:%S'
now = time.strftime(fmt, time.localtime())
print('%s %s' % (msg, now))
if taken:
@ -47,10 +46,10 @@ def tick(msg, fmt='%H:%M:%S', taken=None):
def final():
end = time.time()
taken = end - START
tick('[*] Shutted down at', taken=taken)
tick('[*] Shutted down at', fmt='%Y-%m-%d %H:%M:%S', taken=taken)
def begin():
if not cfg.QUIET:
atexit.register(final)
tick('[*] Started at')
tick('[*] Started at', fmt='%Y-%m-%d %H:%M:%S')

View File

@ -60,7 +60,8 @@ def main():
args = parser.parse_args()
if 'quiet' in args and not args.quiet:
print(BANNER + '\n')
if cfg.ISATTY:
print(BANNER + '\n')
else:
cfg.QUIET = True