2021-03-26 06:10:12 +08:00
|
|
|
import logging
|
2020-01-18 03:03:22 +08:00
|
|
|
import os
|
2021-03-26 06:10:12 +08:00
|
|
|
import time
|
2023-07-18 14:06:13 +08:00
|
|
|
import warnings
|
2021-03-26 04:30:09 +08:00
|
|
|
from pathlib import Path
|
|
|
|
from logging.handlers import BaseRotatingHandler
|
2020-01-18 03:03:22 +08:00
|
|
|
|
|
|
|
import zmq
|
|
|
|
|
2021-03-26 04:30:09 +08:00
|
|
|
from common.logging_extra import SwagLogger, SwagFormatter, SwagLogFileFormatter
|
2022-06-12 07:38:24 +08:00
|
|
|
from system.hardware import PC
|
2020-01-18 03:03:22 +08:00
|
|
|
|
2021-03-26 04:30:09 +08:00
|
|
|
if PC:
|
|
|
|
SWAGLOG_DIR = os.path.join(str(Path.home()), ".comma", "log")
|
|
|
|
else:
|
|
|
|
SWAGLOG_DIR = "/data/log/"
|
2020-04-03 02:28:20 +08:00
|
|
|
|
2021-03-26 04:30:09 +08:00
|
|
|
def get_file_handler():
|
|
|
|
Path(SWAGLOG_DIR).mkdir(parents=True, exist_ok=True)
|
|
|
|
base_filename = os.path.join(SWAGLOG_DIR, "swaglog")
|
|
|
|
handler = SwaglogRotatingFileHandler(base_filename)
|
|
|
|
return handler
|
2020-04-14 06:18:10 +08:00
|
|
|
|
2021-03-26 04:30:09 +08:00
|
|
|
class SwaglogRotatingFileHandler(BaseRotatingHandler):
|
|
|
|
def __init__(self, base_filename, interval=60, max_bytes=1024*256, backup_count=2500, encoding=None):
|
|
|
|
super().__init__(base_filename, mode="a", encoding=encoding, delay=True)
|
|
|
|
self.base_filename = base_filename
|
|
|
|
self.interval = interval # seconds
|
|
|
|
self.max_bytes = max_bytes
|
|
|
|
self.backup_count = backup_count
|
|
|
|
self.log_files = self.get_existing_logfiles()
|
|
|
|
log_indexes = [f.split(".")[-1] for f in self.log_files]
|
|
|
|
self.last_file_idx = max([int(i) for i in log_indexes if i.isdigit()] or [-1])
|
|
|
|
self.last_rollover = None
|
|
|
|
self.doRollover()
|
2020-04-14 06:18:10 +08:00
|
|
|
|
2021-03-26 04:30:09 +08:00
|
|
|
def _open(self):
|
2021-03-26 06:10:12 +08:00
|
|
|
self.last_rollover = time.monotonic()
|
2021-03-26 04:30:09 +08:00
|
|
|
self.last_file_idx += 1
|
|
|
|
next_filename = f"{self.base_filename}.{self.last_file_idx:010}"
|
|
|
|
stream = open(next_filename, self.mode, encoding=self.encoding)
|
|
|
|
self.log_files.insert(0, next_filename)
|
|
|
|
return stream
|
|
|
|
|
|
|
|
def get_existing_logfiles(self):
|
|
|
|
log_files = list()
|
|
|
|
base_dir = os.path.dirname(self.base_filename)
|
|
|
|
for fn in os.listdir(base_dir):
|
|
|
|
fp = os.path.join(base_dir, fn)
|
|
|
|
if fp.startswith(self.base_filename) and os.path.isfile(fp):
|
|
|
|
log_files.append(fp)
|
|
|
|
return sorted(log_files)
|
|
|
|
|
|
|
|
def shouldRollover(self, record):
|
|
|
|
size_exceeded = self.max_bytes > 0 and self.stream.tell() >= self.max_bytes
|
2021-03-26 06:10:12 +08:00
|
|
|
time_exceeded = self.interval > 0 and self.last_rollover + self.interval <= time.monotonic()
|
2021-03-26 04:30:09 +08:00
|
|
|
return size_exceeded or time_exceeded
|
|
|
|
|
|
|
|
def doRollover(self):
|
|
|
|
if self.stream:
|
|
|
|
self.stream.close()
|
|
|
|
self.stream = self._open()
|
|
|
|
|
|
|
|
if self.backup_count > 0:
|
|
|
|
while len(self.log_files) > self.backup_count:
|
|
|
|
to_delete = self.log_files.pop()
|
|
|
|
if os.path.exists(to_delete): # just being safe, should always exist
|
|
|
|
os.remove(to_delete)
|
|
|
|
|
|
|
|
class UnixDomainSocketHandler(logging.Handler):
|
2020-01-18 03:03:22 +08:00
|
|
|
def __init__(self, formatter):
|
|
|
|
logging.Handler.__init__(self)
|
|
|
|
self.setFormatter(formatter)
|
|
|
|
self.pid = None
|
|
|
|
|
2023-06-17 03:25:17 +08:00
|
|
|
self.zctx = None
|
|
|
|
self.sock = None
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
if self.sock is not None:
|
|
|
|
self.sock.close()
|
|
|
|
if self.zctx is not None:
|
|
|
|
self.zctx.term()
|
|
|
|
|
2020-01-18 03:03:22 +08:00
|
|
|
def connect(self):
|
|
|
|
self.zctx = zmq.Context()
|
|
|
|
self.sock = self.zctx.socket(zmq.PUSH)
|
|
|
|
self.sock.setsockopt(zmq.LINGER, 10)
|
|
|
|
self.sock.connect("ipc:///tmp/logmessage")
|
|
|
|
self.pid = os.getpid()
|
|
|
|
|
|
|
|
def emit(self, record):
|
|
|
|
if os.getpid() != self.pid:
|
2023-07-18 14:06:13 +08:00
|
|
|
# TODO suppresses warning about forking proc with zmq socket, fix root cause
|
|
|
|
warnings.filterwarnings("ignore", category=ResourceWarning, message="unclosed.*<zmq.*>")
|
2020-01-18 03:03:22 +08:00
|
|
|
self.connect()
|
|
|
|
|
|
|
|
msg = self.format(record).rstrip('\n')
|
|
|
|
# print("SEND".format(repr(msg)))
|
|
|
|
try:
|
|
|
|
s = chr(record.levelno)+msg
|
|
|
|
self.sock.send(s.encode('utf8'), zmq.NOBLOCK)
|
|
|
|
except zmq.error.Again:
|
|
|
|
# drop :/
|
|
|
|
pass
|
|
|
|
|
2020-04-03 02:28:20 +08:00
|
|
|
|
2021-03-26 04:30:09 +08:00
|
|
|
def add_file_handler(log):
|
|
|
|
"""
|
|
|
|
Function to add the file log handler to swaglog.
|
|
|
|
This can be used to store logs when logmessaged is not running.
|
|
|
|
"""
|
|
|
|
handler = get_file_handler()
|
|
|
|
handler.setFormatter(SwagLogFileFormatter(log))
|
2020-04-03 02:28:20 +08:00
|
|
|
log.addHandler(handler)
|
|
|
|
|
|
|
|
|
2020-01-18 03:03:22 +08:00
|
|
|
cloudlog = log = SwagLogger()
|
|
|
|
log.setLevel(logging.DEBUG)
|
|
|
|
|
2021-11-18 23:05:06 +08:00
|
|
|
|
2020-01-18 03:03:22 +08:00
|
|
|
outhandler = logging.StreamHandler()
|
2021-11-18 23:05:06 +08:00
|
|
|
|
|
|
|
print_level = os.environ.get('LOGPRINT', 'warning')
|
|
|
|
if print_level == 'debug':
|
|
|
|
outhandler.setLevel(logging.DEBUG)
|
|
|
|
elif print_level == 'info':
|
|
|
|
outhandler.setLevel(logging.INFO)
|
|
|
|
elif print_level == 'warning':
|
|
|
|
outhandler.setLevel(logging.WARNING)
|
|
|
|
|
2020-01-18 03:03:22 +08:00
|
|
|
log.addHandler(outhandler)
|
2021-03-26 04:30:09 +08:00
|
|
|
# logs are sent through IPC before writing to disk to prevent disk I/O blocking
|
|
|
|
log.addHandler(UnixDomainSocketHandler(SwagFormatter(log)))
|