send full log

This commit is contained in:
boyska 2024-11-26 00:44:47 +01:00
parent edd6660710
commit f0bcfc2f4d

View file

@ -17,7 +17,7 @@ import contextlib
import requests
def setup_logging():
def setup_logging() -> Path:
logdir = Path("~/.local/share/carichello/").expanduser()
logdir.mkdir(exist_ok=True, parents=True)
today = datetime.datetime.now().strftime("%Y-%m-%d_%H:%M")
@ -32,9 +32,10 @@ def setup_logging():
level=logging.DEBUG,
)
logfile.close()
return Path(logfile.name)
setup_logging()
LOGFILE: Path = setup_logging()
LOG = logging.getLogger()
@ -272,7 +273,38 @@ class Carichello:
def set_clipboard(self, text: str):
subprocess.run(["xsel", "-bi"], input=text.encode("utf8"))
def run(self) -> int:
try:
ret = self._run()
except Exception:
self._send_log(-1)
raise
else:
if ret != 0:
self._send_log(ret)
return ret
def send_mail(self, subject: str, text: str):
mail_to = self.config.get("mail", {}).get("to", None)
hostname = subprocess.check_output(["hostname", "-f"], encoding="utf8").strip()
if mail_to:
try:
subprocess.run(
["mail", "-s", subject, mail_to],
input=f"{hostname}\n\n{text}",
encoding="utf8",
)
return True
except subprocess.CalledProcessError:
LOG.error("error sending email")
return False
def _send_log(self, ret: int):
contents = LOGFILE.read_text()
self.send_mail("Carichello log ERROR", contents)
def _run(self) -> int:
LOG.info("start")
self.args = self.parser.parse_args()
with self.args.config.open() as buf:
@ -357,17 +389,7 @@ class Carichello:
return self.error_exception(exc)
LOG.info("ready: %s", url)
self.set_clipboard(url)
mail_to = self.config.get('mail', {}).get('to', None)
if mail_to:
hostname = subprocess.check_output(['hostname', '-f'], encoding='utf8').strip()
try:
subprocess.run(
['mail', '-s', 'Nuovo file caricato', mail_to],
input=f"{hostname}\n\n{url}",
encoding='utf8',
)
except subprocess.CalledProcessError:
LOG.error("error sending email")
self.send_mail("Nuovo file caricato", url)
subprocess.run(
[
"zenity",