2018-10-14 02:01:35 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
# log.py - Output (stdout + logfile) functions
|
|
|
|
# Part of the Parallel Virtual Cluster (PVC) system
|
|
|
|
#
|
2020-01-08 19:38:02 -05:00
|
|
|
# Copyright (C) 2018-2020 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
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
#
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
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
|
|
|
|
fmt_red = '\033[91m'
|
|
|
|
fmt_green = '\033[92m'
|
|
|
|
fmt_yellow = '\033[93m'
|
2020-08-17 12:46:52 -04:00
|
|
|
fmt_blue = '\033[94m'
|
2018-10-14 02:01:35 -04:00
|
|
|
fmt_purple = '\033[95m'
|
2020-08-17 12:46:52 -04:00
|
|
|
fmt_cyan = '\033[96m'
|
|
|
|
fmt_white = '\033[97m'
|
2020-11-07 12:10:24 -05:00
|
|
|
fmt_bold = '\033[1m'
|
2018-10-14 02:01:35 -04:00
|
|
|
fmt_end = '\033[0m'
|
|
|
|
|
2019-07-10 21:39:25 -04:00
|
|
|
last_colour = ''
|
|
|
|
last_prompt = ''
|
|
|
|
|
|
|
|
# Format maps
|
|
|
|
format_map_colourized = {
|
2020-11-06 22:29:49 -05:00
|
|
|
# Colourized formatting with chevron prompts (log_colours = True)
|
2019-07-10 21:39:25 -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': '>>> ' },
|
2020-08-17 12:46:52 -04:00
|
|
|
'd': { 'colour': fmt_white, 'prompt': '>>> ' },
|
2019-07-10 21:39:25 -04:00
|
|
|
'x': { 'colour': last_colour, 'prompt': last_prompt }
|
|
|
|
}
|
|
|
|
format_map_textual = {
|
2020-11-06 22:29:49 -05:00
|
|
|
# Uncolourized formatting with text prompts (log_colours = False)
|
2019-07-10 21:39:25 -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: ' },
|
2020-08-17 12:46:52 -04:00
|
|
|
'd': { 'colour': '', 'prompt': 'debug: ' },
|
2019-07-10 21:39:25 -04:00
|
|
|
'x': { 'colour': '', 'prompt': last_prompt }
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-07-10 22:20:24 -04:00
|
|
|
if self.config['file_logging']:
|
2018-10-14 02:01:35 -04:00
|
|
|
self.logfile = self.config['log_directory'] + '/pvc.log'
|
|
|
|
# We open the logfile for the duration of our session, but have a hup function
|
|
|
|
self.writer = open(self.logfile, 'a', buffering=1)
|
2019-07-10 21:39:25 -04:00
|
|
|
|
|
|
|
self.last_colour = ''
|
|
|
|
self.last_prompt = ''
|
2019-06-25 22:31:04 -04:00
|
|
|
|
2018-10-14 02:01:35 -04:00
|
|
|
# Provide a hup function to close and reopen the writer
|
|
|
|
def hup(self):
|
|
|
|
self.writer.close()
|
|
|
|
self.writer = open(self.logfile, 'a', buffering=0)
|
|
|
|
|
|
|
|
# Output function
|
2019-07-10 21:39:25 -04:00
|
|
|
def out(self, message, state=None, prefix=''):
|
2018-10-14 02:01:35 -04:00
|
|
|
|
|
|
|
# Get the date
|
2019-07-10 21:39:25 -04:00
|
|
|
if self.config['log_dates']:
|
|
|
|
date = '{} - '.format(datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S.%f'))
|
|
|
|
else:
|
|
|
|
date = ''
|
|
|
|
|
|
|
|
# Get the format map
|
|
|
|
if self.config['log_colours']:
|
|
|
|
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
|
|
|
|
endc = ''
|
|
|
|
|
|
|
|
# Define an undefined state as 'x'; no date in these prompts
|
|
|
|
if not state:
|
|
|
|
state = 'x'
|
2018-10-14 02:01:35 -04:00
|
|
|
date = ''
|
2019-06-25 22:31:04 -04:00
|
|
|
|
2019-07-10 21:39:25 -04:00
|
|
|
# Get colour and prompt from the map
|
|
|
|
colour = format_map[state]['colour']
|
|
|
|
prompt = format_map[state]['prompt']
|
|
|
|
|
|
|
|
# Append space and separator to prefix
|
2018-10-14 02:01:35 -04:00
|
|
|
if prefix != '':
|
|
|
|
prefix = prefix + ' - '
|
2019-06-25 22:31:04 -04:00
|
|
|
|
2019-07-10 21:39:25 -04:00
|
|
|
# Assemble message string
|
2018-10-14 02:01:35 -04:00
|
|
|
message = colour + prompt + endc + date + prefix + message
|
2019-07-10 21:39:25 -04:00
|
|
|
|
|
|
|
# Log to stdout
|
|
|
|
if self.config['stdout_logging']:
|
|
|
|
print(message)
|
|
|
|
|
|
|
|
# Log to file
|
|
|
|
if self.config['file_logging']:
|
2018-10-14 02:01:35 -04:00
|
|
|
self.writer.write(message + '\n')
|
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
|