-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- open txt editor when critical error occurs
- Loading branch information
1 parent
9c98f0d
commit 8fad701
Showing
4 changed files
with
108 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,11 @@ | |
import logging | ||
import os | ||
import platform | ||
import shlex | ||
import subprocess | ||
import sys | ||
import tempfile | ||
from pathlib import Path | ||
|
||
from bitcoin_safe import __version__ | ||
|
||
|
@@ -46,6 +50,43 @@ def remove_absolute_paths(line: str) -> str: | |
return line.replace(current_path, "") | ||
|
||
|
||
def get_system_info_as_text() -> str: | ||
if hasattr(platform, "freedesktop_os_release"): | ||
os_release_info = platform.freedesktop_os_release() | ||
distro_name = os_release_info.get("NAME", "Unknown") | ||
distro_version = os_release_info.get("VERSION", "") | ||
else: | ||
distro_name = "Unknown" | ||
distro_version = "" | ||
|
||
body = "\n\nSystem Info:\n" | ||
body += f"OS: {platform.platform()}\n" | ||
body += f"Distribution: {distro_name} {distro_version}\n" | ||
body += f"Python Version: {sys.version}\n" | ||
body += f"Bitcoin Safe Version: {__version__}\n\n" | ||
return body | ||
|
||
|
||
def text_error_report(error_report: str, file_path: Path | None = None) -> str: | ||
email = "[email protected]" | ||
subject = f"Error report - Bitcoin Safe Version: {__version__}" | ||
body = "" | ||
if file_path: | ||
body += f"You can see the full logfile at: {file_path}\n\n" | ||
|
||
body += f"Please email this to: {email}\n\n" | ||
body += f"{subject}\n\n" | ||
body += f"""Error: | ||
{error_report} | ||
""".replace( | ||
" ", "" | ||
) | ||
|
||
# Write additional system info if needed | ||
body += get_system_info_as_text() | ||
return body | ||
|
||
|
||
def mail_error_repot(error_report: str) -> None: | ||
email = "[email protected]" | ||
subject = f"Error report - Bitcoin Safe Version: {__version__}" | ||
|
@@ -55,11 +96,7 @@ def mail_error_repot(error_report: str) -> None: | |
" ", "" | ||
) | ||
|
||
# Write additional system info if needed | ||
body += "\n\nSystem Info:\n" | ||
body += f"OS: {platform.platform()}\n" | ||
body += f"Python Version: {sys.version}\n" | ||
body += f"Bitcoin Safe Version: {__version__}\n\n" | ||
body += get_system_info_as_text() | ||
return compose_email(email, subject, body) | ||
|
||
|
||
|
@@ -86,3 +123,30 @@ def emit(self, record) -> None: | |
|
||
message = str(self.format(record)) | ||
mail_error_repot(message) | ||
|
||
|
||
class OpenLogHandler(logging.Handler): | ||
def __init__(self, file_path: Path, level=logging.CRITICAL) -> None: | ||
super().__init__(level) | ||
self.file_path = file_path | ||
|
||
@staticmethod | ||
def xdg_open_file(filename: Path): | ||
system_name = platform.system() | ||
if system_name == "Windows": | ||
subprocess.call(shlex.split(f'start "" /max "{filename}"'), shell=True) | ||
elif system_name == "Darwin": # macOS | ||
subprocess.call(shlex.split(f'open "{filename}"')) | ||
elif system_name == "Linux": # Linux | ||
subprocess.call(shlex.split(f'xdg-open "{filename}"')) | ||
|
||
def emit(self, record) -> None: | ||
|
||
message = text_error_report(str(self.format(record)), file_path=self.file_path) | ||
|
||
# Create a temporary file with a message to the user | ||
with tempfile.NamedTemporaryFile(mode="w+", delete=False, suffix=".txt") as temp_file: | ||
temp_file.write(message) | ||
temp_file_path = temp_file.name | ||
|
||
self.xdg_open_file(Path(temp_file_path)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters