|
18 | 18 | You should have received a copy of the GNU General Public License |
19 | 19 | along with MAP Client. If not, see <http://www.gnu.org/licenses/>.. |
20 | 20 | """ |
| 21 | +import atexit |
21 | 22 | import json |
22 | 23 | import multiprocessing |
23 | 24 | import os |
24 | 25 | import shutil |
25 | 26 | import sys |
26 | 27 | import ctypes |
27 | 28 | import argparse |
| 29 | +import faulthandler |
28 | 30 |
|
29 | 31 | import locale |
30 | 32 |
|
31 | 33 | import logging |
| 34 | +from datetime import datetime |
32 | 35 | from logging import handlers |
33 | 36 | from tempfile import TemporaryDirectory |
34 | 37 | from zipfile import ZipFile |
@@ -64,7 +67,27 @@ def get_app_path(): |
64 | 67 | return os.path.dirname(os.path.abspath(__file__)) |
65 | 68 |
|
66 | 69 |
|
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): |
68 | 91 | """ |
69 | 92 | Initialise logger settings and information formatting |
70 | 93 | """ |
@@ -103,7 +126,8 @@ def _prepare_application(): |
103 | 126 |
|
104 | 127 | info.set_applications_settings(app) |
105 | 128 | log_path = get_log_location() |
106 | | - initialise_logger(log_path) |
| 129 | + _initialise_fault_handling(log_path) |
| 130 | + _initialise_logger(log_path) |
107 | 131 |
|
108 | 132 | return app |
109 | 133 |
|
|
0 commit comments