Add file logging for macOS.

When Gramps is launched from macOS's LaunchServices it doesn't have
a sys.stderr attached so the default stream logger goes to /dev/null.
Use a FileHandler in tht case, writing the log to $TMPDIR/gramps-pid.log.

This will help particularly in analyzing crashes where python shuts down
as there's no crash report in that case.
This commit is contained in:
John Ralls 2021-05-04 20:42:04 -07:00
parent f91d3e94ab
commit c66fc6423d

View File

@ -127,15 +127,32 @@ if win():
elif not os.path.isdir(HOME_DIR):
os.makedirs(HOME_DIR)
sys.stdout = sys.stderr = open(logfile, "w", encoding='utf-8')
stderrh = logging.StreamHandler(sys.stderr)
stderrh.setFormatter(form)
stderrh.setLevel(logging.DEBUG)
# macOS sets stderr to /dev/null when running without a terminal,
# e.g. if Gramps.app is lauched by double-clicking on it in
# finder. Write to a file instead.
if mac() and not sys.stdin.isatty():
from tempfile import gettempdir
# Setup the base level logger, this one gets
# everything.
l = logging.getLogger()
l.setLevel(logging.WARNING)
l.addHandler(stderrh)
log_file_name = 'gramps-' + str(os.getpid()) + '.log'
log_file_path = os.path.join(gettempdir(), log_file_name)
log_file_handler = logging.FileHandler(log_file_path, mode='a',
encoding='utf-8')
log_file_handler.setFormatter(form)
log_file_handler.setLevel(logging.DEBUG)
logger = logging.getLogger()
logger.setLevel(logging.WARNING)
logger.addHandler(log_file_handler)
else:
stderrh = logging.StreamHandler(sys.stderr)
stderrh.setFormatter(form)
stderrh.setLevel(logging.DEBUG)
# Setup the base level logger, this one gets
# everything.
l = logging.getLogger()
l.setLevel(logging.WARNING)
l.addHandler(stderrh)
def exc_hook(err_type, value, t_b):