Skip to content

Commit

Permalink
Log exceptions (#73)
Browse files Browse the repository at this point in the history
* minor

* adding exception logging

* cleanup

* mentioning in docs
  • Loading branch information
philbucher authored May 23, 2020
1 parent 25c43f1 commit 8a40c15
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
1 change: 0 additions & 1 deletion create_kratos_input_tui.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ def __init__(self, salome_mesh, mesh_description, model_part_name=""):
else:
err_msg = 'Type of argument "salome_mesh" not permitted: {}\n'.format(type(salome_mesh))
err_msg += 'No mesh can be retrieved from this input!'.format(type(salome_mesh))
logger.error(err_msg)
raise Exception(err_msg)

mesh_interface = MeshInterface(mesh_identifier)
Expand Down
1 change: 0 additions & 1 deletion documentation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ List of contents:
- [User Guide](user_guide.md)
- [Developer Guide](developer_guide.md)
- [Standalone usage for creating mdpa files](standalone_usage.md)
-
2 changes: 1 addition & 1 deletion documentation/user_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ The plugin offers a wide range of usecases:


## Logging
The plugin uses the [Python loggin module](https://docs.python.org/3/library/logging.html), the logs are placed in important parts of the code. Logging is done to the console as well as to a file (`kratos_salome_plugin.log`).
The plugin uses the [Python loggin module](https://docs.python.org/3/library/logging.html), the logs are placed in important parts of the code. Logging is done to the console as well as to a file (`kratos_salome_plugin.log`). Beside the regular logs also exceptions are being logged.\
In case problems with the plugin occur it is very helpful to provide the this log-file.
17 changes: 16 additions & 1 deletion kratos_salome_plugin/plugin_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

# python imports
import logging
import os
import os, sys
from logging.handlers import RotatingFileHandler

# plugin imports
Expand Down Expand Up @@ -93,3 +93,18 @@ def InitializeLogging(logging_level=logging.DEBUG):
fh_formatter = logging.Formatter("[%(asctime)s] [%(levelname)-8s] %(name)s : %(message)s", "%Y-%m-%d %H:%M:%S")
fh.setFormatter(fh_formatter)
root_logger.addHandler(fh)

def handle_unhandled_exception(exc_type, exc_value, exc_traceback):
"""Handler for unhandled exceptions that will write to the logs
taken from: https://www.scrygroup.com/tutorial/2018-02-06/python-excepthook-logging/
TODO:
- check if this also works properly in GUI
- might need some modifications for multiprocessing/threading (see link)
"""
if issubclass(exc_type, KeyboardInterrupt):
# call the default excepthook saved at __excepthook__
sys.__excepthook__(exc_type, exc_value, exc_traceback)
return
root_logger.error("Unhandled exception", exc_info=(exc_type, exc_value, exc_traceback))

sys.excepthook = handle_unhandled_exception

0 comments on commit 8a40c15

Please sign in to comment.