2020-09-26 16:19:37 +02:00
|
|
|
# -*- mode: python -*-
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2021-02-02 14:49:49 +01:00
|
|
|
import os
|
|
|
|
import time
|
2020-09-26 16:19:37 +02:00
|
|
|
import traceback
|
2021-07-31 18:01:10 +02:00
|
|
|
|
|
|
|
from flask import g, current_app
|
2020-09-26 16:19:37 +02:00
|
|
|
|
2021-08-26 23:43:54 +02:00
|
|
|
from app import email
|
|
|
|
|
2021-07-12 15:13:10 +02:00
|
|
|
"""Simple & stupid file logguer, used only to debug
|
|
|
|
(logging to SQL is done in scolog)
|
|
|
|
"""
|
2020-09-26 16:19:37 +02:00
|
|
|
|
|
|
|
LOG_FILENAME = "notes.log" # empty to disable logging
|
2021-08-25 18:15:03 +02:00
|
|
|
DEFAULT_LOG_DIR = (
|
|
|
|
"/opt/scodoc-data/log" # clients should call set_log_directory to change this
|
|
|
|
)
|
2020-09-26 16:19:37 +02:00
|
|
|
|
2021-08-26 23:43:54 +02:00
|
|
|
ALARM_DESTINATION = "emmanuel@scodoc.org"
|
2020-09-26 16:19:37 +02:00
|
|
|
|
|
|
|
|
2021-07-09 23:31:16 +02:00
|
|
|
class _logguer(object):
|
2020-09-26 16:19:37 +02:00
|
|
|
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:
|
2021-07-31 18:01:10 +02:00
|
|
|
try:
|
|
|
|
dept = getattr(g, "scodoc_dept", "")
|
|
|
|
except RuntimeError:
|
|
|
|
# Flask Working outside of application context.
|
|
|
|
dept = ""
|
2020-09-26 16:19:37 +02:00
|
|
|
if dept:
|
|
|
|
dept = " (%s)" % dept
|
2021-07-12 15:13:10 +02:00
|
|
|
msg = dept + " " + msg
|
|
|
|
self.file.write("[%s]%s\n" % (time.strftime("%a %b %d %H:%M:%S %Y"), msg))
|
|
|
|
if current_app:
|
|
|
|
current_app.logger.info(msg)
|
2020-09-26 16:19:37 +02:00
|
|
|
|
|
|
|
self.file.flush()
|
|
|
|
|
|
|
|
|
|
|
|
log = _logguer()
|
|
|
|
|
|
|
|
|
|
|
|
# Alarms by email:
|
2021-08-26 23:43:54 +02:00
|
|
|
def sendAlarm(subject, txt):
|
|
|
|
from app.scodoc import sco_preferences
|
|
|
|
|
|
|
|
sender = sco_preferences.get_preference("email_from_addr")
|
|
|
|
email.send_email(subject, sender, [ALARM_DESTINATION], txt)
|
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]))
|