mirror of
https://github.com/Jylpah/blitz-tools.git
synced 2026-10-23 19:19:53 +02:00
Minor update to logging funcs
This commit is contained in:
1 parent
fb69851f61
commit
6764810a9a
1 file changed
+55
-54
+55
-54
@@ -7,44 +7,45 @@ from bs4 import BeautifulSoup
|
|||||||
|
|
||||||
MAX_RETRIES= 3
|
MAX_RETRIES= 3
|
||||||
SLEEP = 3
|
SLEEP = 3
|
||||||
_debug = False
|
|
||||||
_verbose = False
|
LOG_LEVELS = { 'silent': 0, 'normal': 1, 'verbose': 2, 'debug': 3 }
|
||||||
_silent = False
|
SILENT = 0
|
||||||
|
NORMAL = 1
|
||||||
|
VERBOSE = 2
|
||||||
|
DEBUG = 3
|
||||||
|
_log_level = 1
|
||||||
|
|
||||||
|
## Progress dots
|
||||||
_progress_N = 100
|
_progress_N = 100
|
||||||
_progress_i = 0
|
_progress_i = 0
|
||||||
|
|
||||||
UMASK= os.umask(0)
|
UMASK= os.umask(0)
|
||||||
os.umask(UMASK)
|
os.umask(UMASK)
|
||||||
|
|
||||||
|
|
||||||
def set_debug(debug: bool):
|
def set_debug(debug: bool):
|
||||||
global _debug, _verbose, _silent
|
global _log_level
|
||||||
if debug != None:
|
_log_level = DEBUG
|
||||||
_debug = debug
|
|
||||||
if _debug:
|
|
||||||
_verbose = True
|
|
||||||
_silent = False
|
|
||||||
|
|
||||||
|
|
||||||
def set_verbose(verbose: bool):
|
def set_verbose(verbose: bool):
|
||||||
global _verbose, _silent
|
global _log_level
|
||||||
if verbose != None:
|
_log_level = VERBOSE
|
||||||
_verbose = verbose
|
|
||||||
if _verbose:
|
|
||||||
_silent = False
|
|
||||||
|
|
||||||
|
|
||||||
def set_silent(silent: bool):
|
def set_silent(silent: bool):
|
||||||
global _debug, _verbose, _silent
|
global _log_level
|
||||||
if silent != None:
|
_log_level = SILENT
|
||||||
_silent = silent
|
|
||||||
if _silent:
|
|
||||||
_verbose = False
|
def set_log_level(level = NORMAL):
|
||||||
_debug = False
|
global _log_level
|
||||||
|
_log_level = level
|
||||||
|
|
||||||
|
|
||||||
def verbose(msg = "", id = None):
|
def verbose(msg = "", id = None):
|
||||||
"""Print a message"""
|
"""Print a message"""
|
||||||
if _verbose:
|
if _log_level >= VERBOSE:
|
||||||
if id == None:
|
if id == None:
|
||||||
print(msg)
|
print(msg)
|
||||||
else:
|
else:
|
||||||
@@ -54,47 +55,17 @@ def verbose(msg = "", id = None):
|
|||||||
|
|
||||||
def verbose_std(msg = "", id = None):
|
def verbose_std(msg = "", id = None):
|
||||||
"""Print a message"""
|
"""Print a message"""
|
||||||
if not _silent:
|
if _log_level >= NORMAL:
|
||||||
if id == None:
|
if id == None:
|
||||||
print(msg)
|
print(msg)
|
||||||
else:
|
else:
|
||||||
print('[' + str(id) + ']: ' + msg)
|
print('[' + str(id) + ']: ' + msg)
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def print_progress(force = False):
|
|
||||||
"""Print progress dots"""
|
|
||||||
global _progress_N, _progress_i
|
|
||||||
if not _silent and (force or not _debug):
|
|
||||||
_progress_i = (_progress_i + 1) % _progress_N
|
|
||||||
if _progress_i == 0:
|
|
||||||
print('.', end='', flush=True)
|
|
||||||
|
|
||||||
def set_progress_step(n: int):
|
|
||||||
"""Set the frquency of the progress dots. The bigger 'n', the fewer dots"""
|
|
||||||
global _progress_N
|
|
||||||
if n > 0:
|
|
||||||
_progress_N = n
|
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
def wait(sec : int):
|
|
||||||
for i in range(0, sec):
|
|
||||||
i=i ## to get rid of the warning...
|
|
||||||
time.sleep(1)
|
|
||||||
print_progress(True)
|
|
||||||
print('', flush=True)
|
|
||||||
|
|
||||||
|
|
||||||
def print_new_line(force = False):
|
|
||||||
if not _silent and (force or not _debug):
|
|
||||||
print('', flush=True)
|
|
||||||
|
|
||||||
|
|
||||||
def debug(msg = "", n = None):
|
def debug(msg = "", n = None):
|
||||||
"""print a conditional debug message"""
|
"""print a conditional debug message"""
|
||||||
if _debug:
|
if _log_level >= DEBUG:
|
||||||
curframe = inspect.currentframe()
|
curframe = inspect.currentframe()
|
||||||
calframe = inspect.getouterframes(curframe, 2)
|
calframe = inspect.getouterframes(curframe, 2)
|
||||||
caller = calframe[1][3]
|
caller = calframe[1][3]
|
||||||
@@ -120,6 +91,36 @@ def error(msg = "", exception = None, id = None):
|
|||||||
print('ERROR: ' + caller + '()' + '[' + str(id) + ']: ' + msg + exception_msg)
|
print('ERROR: ' + caller + '()' + '[' + str(id) + ']: ' + msg + exception_msg)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def print_progress(force = False):
|
||||||
|
"""Print progress dots"""
|
||||||
|
global _progress_N, _progress_i
|
||||||
|
if (_log_level > SILENT) and ( force or (_log_level < DEBUG ) ):
|
||||||
|
_progress_i = (_progress_i + 1) % _progress_N
|
||||||
|
if _progress_i == 0:
|
||||||
|
print('.', end='', flush=True)
|
||||||
|
|
||||||
|
def set_progress_step(n: int):
|
||||||
|
"""Set the frquency of the progress dots. The bigger 'n', the fewer dots"""
|
||||||
|
global _progress_N
|
||||||
|
if n > 0:
|
||||||
|
_progress_N = n
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
def wait(sec : int):
|
||||||
|
for i in range(0, sec):
|
||||||
|
i=i ## to get rid of the warning...
|
||||||
|
time.sleep(1)
|
||||||
|
print_progress(True)
|
||||||
|
print('', flush=True)
|
||||||
|
|
||||||
|
|
||||||
|
def print_new_line(force = False):
|
||||||
|
if (_log_level > SILENT) and ( force or (_log_level < DEBUG ) ):
|
||||||
|
print('', flush=True)
|
||||||
|
|
||||||
|
|
||||||
def NOW() -> int:
|
def NOW() -> int:
|
||||||
return int(time.time())
|
return int(time.time())
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user