Skip to content

Commit b0c3a9f

Browse files
authored
Merge pull request #158 from hsorby/main
Improve use of faulthandler by using a log file.
2 parents e6e8175 + ba13b88 commit b0c3a9f

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

src/mapclient/application.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,20 @@
1818
You should have received a copy of the GNU General Public License
1919
along with MAP Client. If not, see <http://www.gnu.org/licenses/>..
2020
"""
21+
import atexit
2122
import json
2223
import multiprocessing
2324
import os
2425
import shutil
2526
import sys
2627
import ctypes
2728
import argparse
29+
import faulthandler
2830

2931
import locale
3032

3133
import logging
34+
from datetime import datetime
3235
from logging import handlers
3336
from tempfile import TemporaryDirectory
3437
from zipfile import ZipFile
@@ -64,7 +67,27 @@ def get_app_path():
6467
return os.path.dirname(os.path.abspath(__file__))
6568

6669

67-
def initialise_logger(log_path):
70+
def _cleanup_crash_log(file_handle, file_path):
71+
file_handle.close()
72+
73+
try:
74+
if os.path.getsize(file_path) == 0:
75+
os.remove(file_path)
76+
except (OSError, FileNotFoundError):
77+
pass
78+
79+
80+
def _initialise_fault_handling(log_path):
81+
pid = os.getpid()
82+
timestamp = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
83+
fault_log_path = os.path.join(os.path.dirname(log_path), f'crash_{timestamp}_{pid}.log')
84+
85+
log_file = open(fault_log_path, 'w')
86+
atexit.register(_cleanup_crash_log, file_handle=log_file, file_path=fault_log_path)
87+
faulthandler.enable(file=log_file)
88+
89+
90+
def _initialise_logger(log_path):
6891
"""
6992
Initialise logger settings and information formatting
7093
"""
@@ -103,7 +126,8 @@ def _prepare_application():
103126

104127
info.set_applications_settings(app)
105128
log_path = get_log_location()
106-
initialise_logger(log_path)
129+
_initialise_fault_handling(log_path)
130+
_initialise_logger(log_path)
107131

108132
return app
109133

0 commit comments

Comments
 (0)