2011-10-11 13:59:26 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2013-08-18 23:38:28 +00:00
|
|
|
# 2010-2013 Nico Schottelius (nico-cdist at schottelius.org)
|
2019-11-11 23:40:58 +00:00
|
|
|
# 2019-2020 Steven Armstrong
|
2011-10-11 13:59:26 +00:00
|
|
|
#
|
|
|
|
# This file is part of cdist.
|
|
|
|
#
|
|
|
|
# cdist is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# cdist is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2019-11-11 23:40:58 +00:00
|
|
|
import datetime
|
2011-10-11 13:59:26 +00:00
|
|
|
import logging
|
2019-11-11 23:40:58 +00:00
|
|
|
import logging.handlers
|
2020-06-09 10:47:50 +00:00
|
|
|
import sys
|
2019-11-11 23:40:58 +00:00
|
|
|
import os
|
2020-06-09 10:47:50 +00:00
|
|
|
import asyncio
|
|
|
|
import contextlib
|
2019-11-11 23:40:58 +00:00
|
|
|
import pickle
|
|
|
|
import struct
|
|
|
|
import threading
|
2011-10-11 13:59:26 +00:00
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
|
2017-06-28 08:18:53 +00:00
|
|
|
# Define additional cdist logging levels.
|
|
|
|
logging.OFF = logging.CRITICAL + 10 # disable logging
|
|
|
|
logging.addLevelName(logging.OFF, 'OFF')
|
|
|
|
|
|
|
|
logging.VERBOSE = logging.INFO - 5
|
|
|
|
logging.addLevelName(logging.VERBOSE, 'VERBOSE')
|
|
|
|
|
|
|
|
|
|
|
|
def _verbose(msg, *args, **kwargs):
|
|
|
|
logging.log(logging.VERBOSE, msg, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
logging.verbose = _verbose
|
|
|
|
|
|
|
|
logging.TRACE = logging.DEBUG - 5
|
|
|
|
logging.addLevelName(logging.TRACE, 'TRACE')
|
|
|
|
|
|
|
|
|
|
|
|
def _trace(msg, *args, **kwargs):
|
|
|
|
logging.log(logging.TRACE, msg, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
logging.trace = _trace
|
|
|
|
|
|
|
|
|
2020-06-11 12:16:37 +00:00
|
|
|
class CdistFormatter(logging.Formatter):
|
|
|
|
USE_COLORS = False
|
2020-06-01 17:11:58 +00:00
|
|
|
RESET = '\033[0m'
|
|
|
|
COLOR_MAP = {
|
|
|
|
'ERROR': '\033[0;31m',
|
|
|
|
'WARNING': '\033[0;33m',
|
|
|
|
'INFO': '\033[0;94m',
|
|
|
|
'VERBOSE': '\033[0;34m',
|
|
|
|
'DEBUG': '\033[0;90m',
|
|
|
|
'TRACE': '\033[0;37m',
|
|
|
|
}
|
|
|
|
|
2020-06-03 19:45:04 +00:00
|
|
|
def __init__(self, fmt):
|
|
|
|
super().__init__(fmt=fmt)
|
2020-06-01 17:11:58 +00:00
|
|
|
|
|
|
|
def format(self, record):
|
|
|
|
msg = super().format(record)
|
2020-06-11 12:16:37 +00:00
|
|
|
if self.USE_COLORS:
|
|
|
|
color = self.COLOR_MAP.get(record.levelname)
|
|
|
|
if color:
|
|
|
|
msg = color + msg + self.RESET
|
2020-06-01 17:11:58 +00:00
|
|
|
return msg
|
|
|
|
|
|
|
|
|
2017-09-13 11:06:06 +00:00
|
|
|
class DefaultLog(logging.Logger):
|
2020-06-11 12:22:54 +00:00
|
|
|
FORMAT = '%(levelname)s: %(name)s: %(message)s'
|
2017-09-13 11:06:06 +00:00
|
|
|
|
|
|
|
class StdoutFilter(logging.Filter):
|
|
|
|
def filter(self, rec):
|
|
|
|
return rec.levelno != logging.ERROR
|
2011-10-11 13:59:26 +00:00
|
|
|
|
2017-09-13 11:06:06 +00:00
|
|
|
class StderrFilter(logging.Filter):
|
|
|
|
def filter(self, rec):
|
|
|
|
return rec.levelno == logging.ERROR
|
|
|
|
|
|
|
|
def __init__(self, name):
|
2013-08-18 23:38:28 +00:00
|
|
|
super().__init__(name)
|
2020-05-23 23:10:56 +00:00
|
|
|
self.propagate = False
|
2017-09-13 11:06:06 +00:00
|
|
|
|
2019-11-11 23:40:58 +00:00
|
|
|
if '__cdist_log_server_socket' in os.environ:
|
|
|
|
log_server_socket = os.environ['__cdist_log_server_socket']
|
2020-06-09 10:47:50 +00:00
|
|
|
socket_handler = logging.handlers.SocketHandler(log_server_socket,
|
|
|
|
None)
|
2019-11-11 23:40:58 +00:00
|
|
|
self.addHandler(socket_handler)
|
|
|
|
else:
|
|
|
|
formatter = CdistFormatter(self.FORMAT)
|
2017-09-13 11:06:06 +00:00
|
|
|
|
2019-11-11 23:40:58 +00:00
|
|
|
stdout_handler = logging.StreamHandler(sys.stdout)
|
|
|
|
stdout_handler.addFilter(self.StdoutFilter())
|
|
|
|
stdout_handler.setLevel(logging.TRACE)
|
|
|
|
stdout_handler.setFormatter(formatter)
|
2017-09-13 11:06:06 +00:00
|
|
|
|
2019-11-11 23:40:58 +00:00
|
|
|
stderr_handler = logging.StreamHandler(sys.stderr)
|
|
|
|
stderr_handler.addFilter(self.StderrFilter())
|
|
|
|
stderr_handler.setLevel(logging.ERROR)
|
|
|
|
stderr_handler.setFormatter(formatter)
|
2017-09-13 11:06:06 +00:00
|
|
|
|
2019-11-11 23:40:58 +00:00
|
|
|
self.addHandler(stdout_handler)
|
|
|
|
self.addHandler(stderr_handler)
|
2017-09-13 11:06:06 +00:00
|
|
|
|
2017-06-28 08:18:53 +00:00
|
|
|
def verbose(self, msg, *args, **kwargs):
|
|
|
|
self.log(logging.VERBOSE, msg, *args, **kwargs)
|
|
|
|
|
|
|
|
def trace(self, msg, *args, **kwargs):
|
|
|
|
self.log(logging.TRACE, msg, *args, **kwargs)
|
|
|
|
|
|
|
|
|
2018-09-23 18:34:42 +00:00
|
|
|
class TimestampingLog(DefaultLog):
|
|
|
|
|
|
|
|
def filter(self, record):
|
|
|
|
"""Add timestamp to messages"""
|
|
|
|
|
|
|
|
super().filter(record)
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
timestamp = now.strftime("%Y%m%d%H%M%S.%f")
|
|
|
|
record.msg = "[" + timestamp + "] " + str(record.msg)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2017-09-13 11:06:06 +00:00
|
|
|
class ParallelLog(DefaultLog):
|
2020-06-11 12:22:54 +00:00
|
|
|
FORMAT = '%(levelname)s: [%(process)d]: %(name)s: %(message)s'
|
2017-09-13 11:06:06 +00:00
|
|
|
|
|
|
|
|
2018-09-23 18:34:42 +00:00
|
|
|
class TimestampingParallelLog(TimestampingLog, ParallelLog):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-09-13 11:06:06 +00:00
|
|
|
def setupDefaultLogging():
|
|
|
|
del logging.getLogger().handlers[:]
|
|
|
|
logging.setLoggerClass(DefaultLog)
|
|
|
|
|
|
|
|
|
2018-09-23 18:34:42 +00:00
|
|
|
def setupTimestampingLogging():
|
|
|
|
del logging.getLogger().handlers[:]
|
|
|
|
logging.setLoggerClass(TimestampingLog)
|
|
|
|
|
|
|
|
|
|
|
|
def setupTimestampingParallelLogging():
|
|
|
|
del logging.getLogger().handlers[:]
|
|
|
|
logging.setLoggerClass(TimestampingParallelLog)
|
|
|
|
|
|
|
|
|
2017-09-13 11:06:06 +00:00
|
|
|
def setupParallelLogging():
|
|
|
|
del logging.getLogger().handlers[:]
|
|
|
|
logging.setLoggerClass(ParallelLog)
|
|
|
|
|
|
|
|
|
2019-11-11 23:40:58 +00:00
|
|
|
async def handle_log_client(reader, writer):
|
|
|
|
while True:
|
|
|
|
chunk = await reader.read(4)
|
|
|
|
if len(chunk) < 4:
|
|
|
|
return
|
|
|
|
|
|
|
|
data_size = struct.unpack('>L', chunk)[0]
|
|
|
|
data = await reader.read(data_size)
|
|
|
|
|
|
|
|
obj = pickle.loads(data)
|
|
|
|
record = logging.makeLogRecord(obj)
|
|
|
|
logger = logging.getLogger(record.name)
|
|
|
|
logger.handle(record)
|
|
|
|
|
|
|
|
|
|
|
|
def run_log_server(server_address):
|
|
|
|
# Get a new loop inside the current thread to run the log server.
|
|
|
|
loop = asyncio.new_event_loop()
|
2020-06-09 10:47:50 +00:00
|
|
|
loop.create_task(asyncio.start_unix_server(handle_log_client,
|
|
|
|
server_address))
|
2019-11-11 23:40:58 +00:00
|
|
|
loop.run_forever()
|
|
|
|
|
|
|
|
|
2020-06-09 10:47:50 +00:00
|
|
|
def setupLogServer(socket_dir, log=logging.getLogger(__name__)):
|
2019-11-11 23:40:58 +00:00
|
|
|
"""Run a asyncio based unix socket log server in a background thread.
|
|
|
|
"""
|
2020-06-09 10:47:50 +00:00
|
|
|
log_server_socket = os.path.join(socket_dir, 'log-server')
|
|
|
|
log.debug('Starting logging server on: %s', log_server_socket)
|
|
|
|
os.environ['__cdist_log_server_socket_export'] = log_server_socket
|
2019-11-11 23:40:58 +00:00
|
|
|
with contextlib.suppress(FileNotFoundError):
|
|
|
|
os.remove(log_server_socket)
|
|
|
|
t = threading.Thread(target=run_log_server, args=(log_server_socket,))
|
|
|
|
# Deamonizing the thread means we don't have to care about stoping it.
|
|
|
|
# It will die together with the main process.
|
|
|
|
t.daemon = True
|
|
|
|
t.start()
|
|
|
|
|
|
|
|
|
2017-09-13 11:06:06 +00:00
|
|
|
setupDefaultLogging()
|