Improve formatting of log messages made from message box content
This commit is contained in:
+14
-3
@@ -25,6 +25,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
import re
|
||||||
|
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@@ -41,6 +42,8 @@ from novelwriter.core.spellcheck import NWSpellEnchant
|
|||||||
from novelwriter.enum import nwChange, nwItemClass
|
from novelwriter.enum import nwChange, nwItemClass
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
|
from collections.abc import Callable
|
||||||
|
|
||||||
from novelwriter.core.project import NWProject
|
from novelwriter.core.project import NWProject
|
||||||
from novelwriter.core.status import T_StatusKind
|
from novelwriter.core.status import T_StatusKind
|
||||||
from novelwriter.gui.theme import GuiTheme
|
from novelwriter.gui.theme import GuiTheme
|
||||||
@@ -50,6 +53,8 @@ logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
NWWidget = TypeVar("NWWidget", bound=QWidget)
|
NWWidget = TypeVar("NWWidget", bound=QWidget)
|
||||||
|
|
||||||
|
RX_HTML = re.compile(r"<.*?>")
|
||||||
|
|
||||||
|
|
||||||
class SharedData(QObject):
|
class SharedData(QObject):
|
||||||
|
|
||||||
@@ -382,7 +387,7 @@ class SharedData(QObject):
|
|||||||
alert.setAlertType(_GuiAlert.INFO, False)
|
alert.setAlertType(_GuiAlert.INFO, False)
|
||||||
self._lastAlert = alert.logMessage
|
self._lastAlert = alert.logMessage
|
||||||
if log:
|
if log:
|
||||||
logger.info(self._lastAlert, stacklevel=2)
|
self._logMessage(self._lastAlert, logger.info)
|
||||||
alert.exec()
|
alert.exec()
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -393,7 +398,7 @@ class SharedData(QObject):
|
|||||||
alert.setAlertType(_GuiAlert.WARN, False)
|
alert.setAlertType(_GuiAlert.WARN, False)
|
||||||
self._lastAlert = alert.logMessage
|
self._lastAlert = alert.logMessage
|
||||||
if log:
|
if log:
|
||||||
logger.warning(self._lastAlert, stacklevel=2)
|
self._logMessage(self._lastAlert, logger.warning)
|
||||||
alert.exec()
|
alert.exec()
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -407,7 +412,7 @@ class SharedData(QObject):
|
|||||||
alert.setException(exc)
|
alert.setException(exc)
|
||||||
self._lastAlert = alert.logMessage
|
self._lastAlert = alert.logMessage
|
||||||
if log:
|
if log:
|
||||||
logger.error(self._lastAlert, stacklevel=2)
|
self._logMessage(self._lastAlert, logger.error)
|
||||||
alert.exec()
|
alert.exec()
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -425,6 +430,12 @@ class SharedData(QObject):
|
|||||||
# Internal Functions
|
# Internal Functions
|
||||||
##
|
##
|
||||||
|
|
||||||
|
def _logMessage(self, message: str, log: Callable) -> None:
|
||||||
|
"""Print message to log."""
|
||||||
|
for text in message.split("<br>"):
|
||||||
|
log(RX_HTML.sub("", text), stacklevel=3)
|
||||||
|
return
|
||||||
|
|
||||||
def _resetProject(self) -> None:
|
def _resetProject(self) -> None:
|
||||||
"""Create a new project and spell checking instance."""
|
"""Create a new project and spell checking instance."""
|
||||||
from novelwriter.core.project import NWProject
|
from novelwriter.core.project import NWProject
|
||||||
|
|||||||
Reference in New Issue
Block a user