send full log
This commit is contained in:
parent
edd6660710
commit
f0bcfc2f4d
1 changed files with 35 additions and 13 deletions
|
@ -17,7 +17,7 @@ import contextlib
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
|
|
||||||
def setup_logging():
|
def setup_logging() -> Path:
|
||||||
logdir = Path("~/.local/share/carichello/").expanduser()
|
logdir = Path("~/.local/share/carichello/").expanduser()
|
||||||
logdir.mkdir(exist_ok=True, parents=True)
|
logdir.mkdir(exist_ok=True, parents=True)
|
||||||
today = datetime.datetime.now().strftime("%Y-%m-%d_%H:%M")
|
today = datetime.datetime.now().strftime("%Y-%m-%d_%H:%M")
|
||||||
|
@ -32,9 +32,10 @@ def setup_logging():
|
||||||
level=logging.DEBUG,
|
level=logging.DEBUG,
|
||||||
)
|
)
|
||||||
logfile.close()
|
logfile.close()
|
||||||
|
return Path(logfile.name)
|
||||||
|
|
||||||
|
|
||||||
setup_logging()
|
LOGFILE: Path = setup_logging()
|
||||||
LOG = logging.getLogger()
|
LOG = logging.getLogger()
|
||||||
|
|
||||||
|
|
||||||
|
@ -272,7 +273,38 @@ class Carichello:
|
||||||
def set_clipboard(self, text: str):
|
def set_clipboard(self, text: str):
|
||||||
subprocess.run(["xsel", "-bi"], input=text.encode("utf8"))
|
subprocess.run(["xsel", "-bi"], input=text.encode("utf8"))
|
||||||
|
|
||||||
|
|
||||||
def run(self) -> int:
|
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")
|
LOG.info("start")
|
||||||
self.args = self.parser.parse_args()
|
self.args = self.parser.parse_args()
|
||||||
with self.args.config.open() as buf:
|
with self.args.config.open() as buf:
|
||||||
|
@ -357,17 +389,7 @@ class Carichello:
|
||||||
return self.error_exception(exc)
|
return self.error_exception(exc)
|
||||||
LOG.info("ready: %s", url)
|
LOG.info("ready: %s", url)
|
||||||
self.set_clipboard(url)
|
self.set_clipboard(url)
|
||||||
mail_to = self.config.get('mail', {}).get('to', None)
|
self.send_mail("Nuovo file caricato", url)
|
||||||
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")
|
|
||||||
subprocess.run(
|
subprocess.run(
|
||||||
[
|
[
|
||||||
"zenity",
|
"zenity",
|
||||||
|
|
Loading…
Reference in a new issue