Skip to content

Commit 90184db

Browse files
committed
Fixed excepthook problems
1 parent 21f5df9 commit 90184db

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

skm_pyutils/py_log.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"""
22
Logging related functions.
3-
4-
TODO test the except hooks more.
53
"""
64
import datetime
75
import logging
@@ -32,9 +30,7 @@ def log_exception(ex, more_info="", location=None):
3230
3331
"""
3432
if location is None:
35-
default_loc = os.path.join(
36-
os.path.expanduser("~"), ".skm_python", "caught_errors.txt"
37-
)
33+
default_loc = get_default_log_loc("caught_errors.txt")
3834
else:
3935
default_loc = location
4036

@@ -53,27 +49,25 @@ def log_exception(ex, more_info="", location=None):
5349

5450
def default_excepthook(exc_type, exc_value, exc_traceback):
5551
"""Any uncaught exceptions will be logged from here."""
56-
default_loc = os.path.join(
57-
os.path.expanduser("~"), ".skm_python", "uncaught_errors.txt"
58-
)
52+
default_loc = get_default_log_loc("uncaught_errors.txt")
5953

60-
this_logger = logging.getLogger(__name__)
54+
file_logger = logging.getLogger(__name__)
55+
file_logger.propagate = False
6156
handler = logging.FileHandler(default_loc)
62-
this_logger.addHandler(handler)
57+
file_logger.addHandler(handler)
6358

6459
# Don't catch CTRL+C exceptions
6560
if issubclass(exc_type, KeyboardInterrupt):
6661
sys.__excepthook__(exc_type, exc_value, exc_traceback)
6762
return
6863

6964
now = datetime.datetime.now()
70-
this_logger.critical(
65+
file_logger.critical(
7166
"\n----------Uncaught Exception at {}----------".format(now),
7267
exc_info=(exc_type, exc_value, exc_traceback),
7368
)
7469

75-
sys.stdout.write = default_write
76-
print("A fatal error occurred in this Python program")
70+
print("\nA fatal error occurred in this Python program")
7771
print(
7872
"The error info was: {}".format(
7973
"".join(
@@ -83,6 +77,8 @@ def default_excepthook(exc_type, exc_value, exc_traceback):
8377
)
8478
print("Please report this to {} and provide the file {}".format("us", default_loc))
8579

80+
sys.exit(-1)
81+
8682

8783
def override_excepthook(excepthook=None):
8884
"""
@@ -104,3 +100,9 @@ def override_excepthook(excepthook=None):
104100
if excepthook is None:
105101
excepthook = default_excepthook
106102
sys.excepthook = excepthook
103+
104+
105+
def get_default_log_loc(name):
106+
default_loc = os.path.join(os.path.expanduser("~"), ".skm_python", name)
107+
108+
return default_loc

0 commit comments

Comments
 (0)