1
0
forked from ScoDoc/ScoDoc
ScoDoc-Front/app/scodoc/notes_log.py

131 lines
3.6 KiB
Python
Raw Normal View History

2020-09-26 16:19:37 +02:00
# -*- mode: python -*-
# -*- coding: utf-8 -*-
2021-02-02 14:49:49 +01:00
import pdb
import sys
import os
import re
import inspect
import time
2020-09-26 16:19:37 +02:00
import traceback
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email.header import Header
2020-09-26 16:19:37 +02:00
2021-02-02 14:49:49 +01:00
from email.MIMEMultipart import ( # pylint: disable=no-name-in-module,import-error
MIMEMultipart,
)
from email.MIMEText import MIMEText # pylint: disable=no-name-in-module,import-error
from email.MIMEBase import MIMEBase # pylint: disable=no-name-in-module,import-error
from email.Header import Header # pylint: disable=no-name-in-module,import-error
from email import Encoders # pylint: disable=no-name-in-module,import-error
2021-06-13 18:29:53 +02:00
2020-09-26 16:19:37 +02:00
# Simple & stupid file logguer, used only to debug
# (logging to SQL is done in scolog)
LOG_FILENAME = "notes.log" # empty to disable logging
DEFAULT_LOG_DIR = "/tmp" # clients should call set_log_directory to change this
ALARM_DESTINATION = "emmanuel.viennet@univ-paris13.fr" # XXX a mettre en preference
class _logguer:
def __init__(self):
self.file = None
self.directory = None
self.set_log_directory(DEFAULT_LOG_DIR)
def set_log_directory(self, directory):
if self.directory != directory and self.file:
# changing directory when a log is already open: close it
self.file.close()
self.file = None
self.directory = directory
def _open(self):
if LOG_FILENAME:
path = os.path.join(self.directory, LOG_FILENAME)
self.file = open(path, "a")
2020-12-26 00:11:55 +01:00
self("new _logguer (%s)" % path)
2020-09-26 16:19:37 +02:00
else:
self.file = None # logging disabled
def __call__(self, msg):
if not self.file:
self._open()
if self.file:
dept = retreive_dept()
if dept:
dept = " (%s)" % dept
self.file.write(
"[%s]%s %s\n" % (time.strftime("%a %b %d %H:%M:%S %Y"), dept, msg)
)
# if not dept:
# import traceback
# traceback.print_stack(file=self.file) # hunt missing REQUESTS
self.file.flush()
log = _logguer()
def retreive_request(skip=0):
"""Try to retreive a REQUEST variable in caller stack.
This is a hack, used only in log functions.
"""
def search(frame):
if frame.f_locals.has_key("REQUEST"):
return frame.f_locals["REQUEST"]
if frame.f_back:
return search(frame.f_back)
else:
return None
frame = inspect.currentframe()
if frame: # not supported by all pythons
startframe = frame
while skip and startframe.f_back:
startframe = startframe.f_back
return search(startframe)
else:
return None
def retreive_dept():
"""Try to retreive departement (from REQUEST URL)"""
REQUEST = retreive_request()
if not REQUEST:
return ""
try:
url = REQUEST.URL
m = re.match(r"^.*ScoDoc/(\w+).*$", url)
2020-09-26 16:19:37 +02:00
return m.group(1)
except:
return ""
# Alarms by email:
def sendAlarm(context, subj, txt):
import sco_utils
2021-06-14 18:08:52 +02:00
import mails
2020-09-26 16:19:37 +02:00
msg = MIMEMultipart()
subj = Header(subj, sco_utils.SCO_ENCODING)
msg["Subject"] = subj
2021-06-13 19:12:20 +02:00
msg["From"] = sco_preferences.get_preference(context, "email_from_addr")
2020-09-26 16:19:37 +02:00
msg["To"] = ALARM_DESTINATION
msg.epilogue = ""
txt = MIMEText(txt, "plain", sco_utils.SCO_ENCODING)
msg.attach(txt)
2021-06-13 18:29:53 +02:00
mails.sendEmail(context, msg)
2020-09-26 16:19:37 +02:00
# Debug: log call stack
def logCallStack():
log("Call stack:\n" + "\n".join(x.strip() for x in traceback.format_stack()[:-1]))