2018-10-14 02:01:35 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2021-07-18 17:03:57 -04:00
|
|
|
# log.py - PVC daemon logger functions
|
2018-10-14 02:01:35 -04:00
|
|
|
# Part of the Parallel Virtual Cluster (PVC) system
|
|
|
|
#
|
2022-10-06 11:55:27 -04:00
|
|
|
# Copyright (C) 2018-2022 Joshua M. Boniface <joshua@boniface.me>
|
2018-10-14 02:01:35 -04:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
2021-03-25 16:57:17 -04:00
|
|
|
# the Free Software Foundation, version 3.
|
2018-10-14 02:01:35 -04:00
|
|
|
#
|
|
|
|
# This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
###############################################################################
|
|
|
|
|
2021-07-18 17:03:57 -04:00
|
|
|
from collections import deque
|
|
|
|
from threading import Thread
|
|
|
|
from queue import Queue
|
|
|
|
from datetime import datetime
|
2021-07-19 11:45:58 -04:00
|
|
|
from time import sleep
|
2021-07-18 17:03:57 -04:00
|
|
|
|
|
|
|
from daemon_lib.zkhandler import ZKHandler
|
2018-10-14 02:01:35 -04:00
|
|
|
|
2020-11-07 14:45:24 -05:00
|
|
|
|
2018-10-14 02:01:35 -04:00
|
|
|
class Logger(object):
|
|
|
|
# Define a logger class for a daemon instance
|
|
|
|
# Keeps record of where to log, and is passed messages which are
|
|
|
|
# formatted in various ways based off secondary characteristics.
|
|
|
|
|
|
|
|
# ANSII colours for output
|
2021-11-06 03:02:43 -04:00
|
|
|
fmt_red = "\033[91m"
|
|
|
|
fmt_green = "\033[92m"
|
|
|
|
fmt_yellow = "\033[93m"
|
|
|
|
fmt_blue = "\033[94m"
|
|
|
|
fmt_purple = "\033[95m"
|
|
|
|
fmt_cyan = "\033[96m"
|
|
|
|
fmt_white = "\033[97m"
|
|
|
|
fmt_bold = "\033[1m"
|
|
|
|
fmt_end = "\033[0m"
|
|
|
|
|
|
|
|
last_colour = ""
|
|
|
|
last_prompt = ""
|
2019-07-10 21:39:25 -04:00
|
|
|
|
|
|
|
# Format maps
|
|
|
|
format_map_colourized = {
|
2020-11-06 22:29:49 -05:00
|
|
|
# Colourized formatting with chevron prompts (log_colours = True)
|
2021-11-06 03:02:43 -04:00
|
|
|
"o": {"colour": fmt_green, "prompt": ">>> "},
|
|
|
|
"e": {"colour": fmt_red, "prompt": ">>> "},
|
|
|
|
"w": {"colour": fmt_yellow, "prompt": ">>> "},
|
|
|
|
"t": {"colour": fmt_purple, "prompt": ">>> "},
|
|
|
|
"i": {"colour": fmt_blue, "prompt": ">>> "},
|
|
|
|
"s": {"colour": fmt_cyan, "prompt": ">>> "},
|
|
|
|
"d": {"colour": fmt_white, "prompt": ">>> "},
|
|
|
|
"x": {"colour": last_colour, "prompt": last_prompt},
|
2019-07-10 21:39:25 -04:00
|
|
|
}
|
|
|
|
format_map_textual = {
|
2020-11-06 22:29:49 -05:00
|
|
|
# Uncolourized formatting with text prompts (log_colours = False)
|
2021-11-06 03:02:43 -04:00
|
|
|
"o": {"colour": "", "prompt": "ok: "},
|
|
|
|
"e": {"colour": "", "prompt": "failed: "},
|
|
|
|
"w": {"colour": "", "prompt": "warning: "},
|
|
|
|
"t": {"colour": "", "prompt": "tick: "},
|
|
|
|
"i": {"colour": "", "prompt": "info: "},
|
|
|
|
"s": {"colour": "", "prompt": "system: "},
|
|
|
|
"d": {"colour": "", "prompt": "debug: "},
|
|
|
|
"x": {"colour": "", "prompt": last_prompt},
|
2019-07-10 21:39:25 -04:00
|
|
|
}
|
|
|
|
|
2018-10-14 02:01:35 -04:00
|
|
|
# Initialization of instance
|
|
|
|
def __init__(self, config):
|
|
|
|
self.config = config
|
2019-07-10 21:39:25 -04:00
|
|
|
|
2021-11-06 03:02:43 -04:00
|
|
|
if self.config["file_logging"]:
|
2023-11-29 14:53:01 -05:00
|
|
|
self.logfile = (
|
|
|
|
self.config["log_directory"] + "/" + self.config["daemon_name"] + ".log"
|
|
|
|
)
|
2018-10-14 02:01:35 -04:00
|
|
|
# We open the logfile for the duration of our session, but have a hup function
|
2021-11-06 03:02:43 -04:00
|
|
|
self.writer = open(self.logfile, "a", buffering=0)
|
2019-07-10 21:39:25 -04:00
|
|
|
|
2021-11-06 03:02:43 -04:00
|
|
|
self.last_colour = ""
|
|
|
|
self.last_prompt = ""
|
2019-06-25 22:31:04 -04:00
|
|
|
|
2021-11-06 03:02:43 -04:00
|
|
|
if self.config["zookeeper_logging"]:
|
2021-07-19 11:45:58 -04:00
|
|
|
self.zookeeper_queue = Queue()
|
|
|
|
self.zookeeper_logger = ZookeeperLogger(self.config, self.zookeeper_queue)
|
2021-07-18 17:03:57 -04:00
|
|
|
self.zookeeper_logger.start()
|
|
|
|
|
2018-10-14 02:01:35 -04:00
|
|
|
# Provide a hup function to close and reopen the writer
|
|
|
|
def hup(self):
|
|
|
|
self.writer.close()
|
2021-11-06 03:02:43 -04:00
|
|
|
self.writer = open(self.logfile, "a", buffering=0)
|
2018-10-14 02:01:35 -04:00
|
|
|
|
2021-07-18 19:53:00 -04:00
|
|
|
# Provide a termination function so all messages are flushed before terminating the main daemon
|
|
|
|
def terminate(self):
|
2021-11-06 03:02:43 -04:00
|
|
|
if self.config["file_logging"]:
|
2021-07-18 19:53:00 -04:00
|
|
|
self.writer.close()
|
2021-11-06 03:02:43 -04:00
|
|
|
if self.config["zookeeper_logging"]:
|
2023-11-29 14:50:19 -05:00
|
|
|
self.out("Waiting for Zookeeper message queue to drain", state="s")
|
2021-07-19 11:45:58 -04:00
|
|
|
|
|
|
|
tick_count = 0
|
|
|
|
while not self.zookeeper_queue.empty():
|
|
|
|
sleep(0.5)
|
|
|
|
tick_count += 1
|
|
|
|
if tick_count > 30:
|
|
|
|
break
|
|
|
|
|
2021-07-18 19:53:00 -04:00
|
|
|
self.zookeeper_logger.stop()
|
|
|
|
self.zookeeper_logger.join()
|
|
|
|
|
2018-10-14 02:01:35 -04:00
|
|
|
# Output function
|
2021-11-06 03:02:43 -04:00
|
|
|
def out(self, message, state=None, prefix=""):
|
2018-10-14 02:01:35 -04:00
|
|
|
# Get the date
|
2021-11-06 03:02:43 -04:00
|
|
|
if self.config["log_dates"]:
|
|
|
|
date = "{} ".format(datetime.now().strftime("%Y/%m/%d %H:%M:%S.%f"))
|
2019-07-10 21:39:25 -04:00
|
|
|
else:
|
2021-11-06 03:02:43 -04:00
|
|
|
date = ""
|
2019-07-10 21:39:25 -04:00
|
|
|
|
|
|
|
# Get the format map
|
2021-11-06 03:02:43 -04:00
|
|
|
if self.config["log_colours"]:
|
2019-07-10 21:39:25 -04:00
|
|
|
format_map = self.format_map_colourized
|
|
|
|
endc = Logger.fmt_end
|
2018-10-14 02:01:35 -04:00
|
|
|
else:
|
2019-07-10 21:39:25 -04:00
|
|
|
format_map = self.format_map_textual
|
2021-11-06 03:02:43 -04:00
|
|
|
endc = ""
|
2019-07-10 21:39:25 -04:00
|
|
|
|
|
|
|
# Define an undefined state as 'x'; no date in these prompts
|
|
|
|
if not state:
|
2021-11-06 03:02:43 -04:00
|
|
|
state = "x"
|
|
|
|
date = ""
|
2019-06-25 22:31:04 -04:00
|
|
|
|
2019-07-10 21:39:25 -04:00
|
|
|
# Get colour and prompt from the map
|
2021-11-06 03:02:43 -04:00
|
|
|
colour = format_map[state]["colour"]
|
|
|
|
prompt = format_map[state]["prompt"]
|
2019-07-10 21:39:25 -04:00
|
|
|
|
|
|
|
# Append space and separator to prefix
|
2021-11-06 03:02:43 -04:00
|
|
|
if prefix != "":
|
|
|
|
prefix = prefix + " - "
|
2019-06-25 22:31:04 -04:00
|
|
|
|
2019-07-10 21:39:25 -04:00
|
|
|
# Log to stdout
|
2021-11-06 03:02:43 -04:00
|
|
|
if self.config["stdout_logging"]:
|
2023-11-29 14:53:01 -05:00
|
|
|
# Assemble output string
|
|
|
|
output = colour + prompt + endc + date + prefix + message
|
|
|
|
print(output)
|
2019-07-10 21:39:25 -04:00
|
|
|
|
|
|
|
# Log to file
|
2021-11-06 03:02:43 -04:00
|
|
|
if self.config["file_logging"]:
|
2023-11-29 14:53:01 -05:00
|
|
|
# Assemble output string
|
|
|
|
output = colour + prompt + endc + date + prefix + message
|
|
|
|
self.writer.write(output + "\n")
|
2019-07-10 21:39:25 -04:00
|
|
|
|
2021-07-18 17:03:57 -04:00
|
|
|
# Log to Zookeeper
|
2021-11-06 03:02:43 -04:00
|
|
|
if self.config["zookeeper_logging"]:
|
2023-11-29 14:53:01 -05:00
|
|
|
# Set the daemon value (only used here as others do not overlap with different daemons)
|
|
|
|
daemon = f"{self.config['daemon_name']}: "
|
|
|
|
# Assemble output string
|
|
|
|
output = daemon + colour + prompt + endc + date + prefix + message
|
|
|
|
self.zookeeper_queue.put(output)
|
2021-07-18 17:03:57 -04:00
|
|
|
|
2019-07-10 21:39:25 -04:00
|
|
|
# Set last message variables
|
2018-10-14 02:01:35 -04:00
|
|
|
self.last_colour = colour
|
2019-07-10 21:39:25 -04:00
|
|
|
self.last_prompt = prompt
|
2021-07-18 17:03:57 -04:00
|
|
|
|
|
|
|
|
|
|
|
class ZookeeperLogger(Thread):
|
|
|
|
"""
|
|
|
|
Defines a threaded writer for Zookeeper locks. Threading prevents the blocking of other
|
|
|
|
daemon events while the records are written. They will be eventually-consistent
|
|
|
|
"""
|
2021-11-06 03:02:43 -04:00
|
|
|
|
2021-07-19 11:45:58 -04:00
|
|
|
def __init__(self, config, zookeeper_queue):
|
2021-07-18 17:03:57 -04:00
|
|
|
self.config = config
|
2021-11-06 03:02:43 -04:00
|
|
|
self.node = self.config["node"]
|
|
|
|
self.max_lines = self.config["node_log_lines"]
|
2021-07-19 11:45:58 -04:00
|
|
|
self.zookeeper_queue = zookeeper_queue
|
|
|
|
self.connected = False
|
2021-07-18 19:53:00 -04:00
|
|
|
self.running = False
|
2021-07-19 11:45:58 -04:00
|
|
|
self.zkhandler = None
|
2021-07-18 17:03:57 -04:00
|
|
|
Thread.__init__(self, args=(), kwargs=None)
|
|
|
|
|
|
|
|
def start_zkhandler(self):
|
|
|
|
# We must open our own dedicated Zookeeper instance because we can't guarantee one already exists when this starts
|
|
|
|
if self.zkhandler is not None:
|
|
|
|
try:
|
|
|
|
self.zkhandler.disconnect()
|
|
|
|
except Exception:
|
|
|
|
pass
|
2021-07-19 11:45:58 -04:00
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
self.zkhandler = ZKHandler(self.config, logger=None)
|
|
|
|
self.zkhandler.connect(persistent=True)
|
|
|
|
break
|
|
|
|
except Exception:
|
|
|
|
sleep(0.5)
|
|
|
|
continue
|
|
|
|
|
|
|
|
self.connected = True
|
|
|
|
|
|
|
|
# Ensure the root keys for this are instantiated
|
2021-11-06 03:02:43 -04:00
|
|
|
self.zkhandler.write([("base.logs", ""), (("logs", self.node), "")])
|
2021-07-18 17:03:57 -04:00
|
|
|
|
|
|
|
def run(self):
|
2023-11-29 12:10:27 -05:00
|
|
|
self.start_zkhandler()
|
2021-07-18 19:53:00 -04:00
|
|
|
self.running = True
|
2023-11-29 12:10:27 -05:00
|
|
|
|
2021-07-18 19:53:00 -04:00
|
|
|
while self.running:
|
|
|
|
# Get a new message
|
|
|
|
try:
|
2021-07-19 11:45:58 -04:00
|
|
|
message = self.zookeeper_queue.get(timeout=1)
|
2021-07-18 19:53:00 -04:00
|
|
|
if not message:
|
|
|
|
continue
|
|
|
|
except Exception:
|
|
|
|
continue
|
2021-07-18 17:03:57 -04:00
|
|
|
|
2021-11-06 03:02:43 -04:00
|
|
|
if not self.config["log_dates"]:
|
2021-07-18 17:03:57 -04:00
|
|
|
# We want to log dates here, even if the log_dates config is not set
|
2021-11-06 03:02:43 -04:00
|
|
|
date = "{} ".format(datetime.now().strftime("%Y/%m/%d %H:%M:%S.%f"))
|
2021-07-18 17:03:57 -04:00
|
|
|
else:
|
2021-11-06 03:02:43 -04:00
|
|
|
date = ""
|
2021-07-19 13:09:36 -04:00
|
|
|
|
2023-11-29 12:10:27 -05:00
|
|
|
try:
|
|
|
|
with self.zkhandler.writelock(("logs.messages", self.node)):
|
|
|
|
# Get the logs that are currently in Zookeeper and populate our deque
|
|
|
|
cur_logs = self.zkhandler.read(("logs.messages", self.node))
|
|
|
|
if cur_logs is None:
|
|
|
|
cur_logs = ""
|
|
|
|
|
|
|
|
logs = deque(cur_logs.split("\n"), self.max_lines - 1)
|
|
|
|
|
|
|
|
# Add the message to the deque
|
|
|
|
logs.append(f"{date}{message}")
|
|
|
|
|
2021-07-19 13:09:36 -04:00
|
|
|
# Write the updated messages into Zookeeper
|
2021-11-06 03:02:43 -04:00
|
|
|
self.zkhandler.write(
|
|
|
|
[(("logs.messages", self.node), "\n".join(logs))]
|
|
|
|
)
|
2023-11-29 12:10:27 -05:00
|
|
|
except Exception:
|
|
|
|
continue
|
|
|
|
|
2021-07-18 19:53:00 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self.running = False
|