Added get_log_filepath()
* Much safer method than what was in upload_debug_report()
This commit is contained in:
parent
f1d53e698b
commit
0757b9fe55
1 changed files with 26 additions and 13 deletions
|
|
@ -4,6 +4,7 @@
|
||||||
import itertools
|
import itertools
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import pathlib
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
|
@ -151,6 +152,24 @@ def clear_screen():
|
||||||
os.system('clear')
|
os.system('clear')
|
||||||
|
|
||||||
|
|
||||||
|
def get_log_filepath():
|
||||||
|
"""Get the log filepath from the root logger, returns pathlib.Path obj.
|
||||||
|
|
||||||
|
NOTE: This will use the first handler baseFilename it finds (if any)."""
|
||||||
|
# TODO: Test under all platforms
|
||||||
|
log_filepath = None
|
||||||
|
root_logger = logging.getLogger()
|
||||||
|
|
||||||
|
# Check handlers
|
||||||
|
for handler in root_logger.handlers:
|
||||||
|
if hasattr(handler, 'baseFilename'):
|
||||||
|
log_filepath = pathlib.Path(handler.baseFilename).resolve()
|
||||||
|
break
|
||||||
|
|
||||||
|
# Done
|
||||||
|
return log_filepath
|
||||||
|
|
||||||
|
|
||||||
def input_text(prompt='Enter text'):
|
def input_text(prompt='Enter text'):
|
||||||
"""Get text from user, returns string."""
|
"""Get text from user, returns string."""
|
||||||
prompt = str(prompt)
|
prompt = str(prompt)
|
||||||
|
|
@ -312,7 +331,6 @@ def strip_colors(string):
|
||||||
|
|
||||||
def upload_debug_report(report, reason='DEBUG'):
|
def upload_debug_report(report, reason='DEBUG'):
|
||||||
"""Upload debug report to CRASH_SERVER as specified in wk.cfg.main."""
|
"""Upload debug report to CRASH_SERVER as specified in wk.cfg.main."""
|
||||||
import pathlib
|
|
||||||
import requests
|
import requests
|
||||||
LOG.info('Uploading debug report to %s', CRASH_SERVER.get('Name', '?'))
|
LOG.info('Uploading debug report to %s', CRASH_SERVER.get('Name', '?'))
|
||||||
|
|
||||||
|
|
@ -324,18 +342,13 @@ def upload_debug_report(report, reason='DEBUG'):
|
||||||
raise UserWarning(msg)
|
raise UserWarning(msg)
|
||||||
|
|
||||||
# Set filename (based on the logging config if possible)
|
# Set filename (based on the logging config if possible)
|
||||||
## NOTE: This is using a gross assumption on the logging setup
|
filename = 'Unknown'
|
||||||
## That's why there's such a broad exception if something goes wrong
|
log_path = get_log_filepath()
|
||||||
## TODO: Test under all platforms
|
if log_path:
|
||||||
root_logger = logging.getLogger()
|
# Strip everything but the prefix
|
||||||
try:
|
filename = re.sub(r'^(.*)_(\d{4}-\d{2}-\d{2}.*)', r'\1', log_path.name)
|
||||||
filename = root_logger.handlers[0].baseFilename
|
filename = '{prefix}_{reason}_{datetime}.log'.format(
|
||||||
filename = pathlib.Path(filename).name
|
prefix=filename,
|
||||||
filename = re.sub(r'^(.*)_(\d{4}-\d{2}-\d{2}.*)', r'\1', filename)
|
|
||||||
except Exception: #pylint: disable=broad-except
|
|
||||||
filename = 'Unknown'
|
|
||||||
filename = '{base}_{reason}_{datetime}.log'.format(
|
|
||||||
base=filename,
|
|
||||||
reason=reason,
|
reason=reason,
|
||||||
datetime=time.strftime('%Y-%m-%d_%H%M%S%z'),
|
datetime=time.strftime('%Y-%m-%d_%H%M%S%z'),
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue