-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpleLogger.py
52 lines (46 loc) · 2.09 KB
/
simpleLogger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Written by: Anthony Kiesel
# URL: https://github.com/kieselai/bashForEach.py
from colorama import init, Fore, Back
# Local imports
import formatter
init(autoreset=True)
class SimpleLogger:
""" Class Used to log messages to the terminal """
@staticmethod
def init(verboseLoggingEnabled):
""" Initialize the logger with this function
Parameters:
verboseLoggingEnabled (boolean): switches verbose messages on/off
"""
SimpleLogger.verboseLoggingEnabled = verboseLoggingEnabled
@staticmethod
def output(*messages, **namedArgs):
""" Print a message with optional colored output
Parameters:
*messages (string or list(string, ...)): messages to print
foreground (string): optional named argument containing a code to set the foreground
background (string): optional named argument containing a code to set the background
"""
(foreground, background) = (namedArgs.get("foreground", Fore.RESET), namedArgs.get("background", Back.RESET))
for msg in messages:
# Make msg into a list that is 1 level deep
msg = formatter.Formatter.FlattenList([msg])
# Print color codes as well as the messages provided
print(foreground + background + "".join(msg))
@staticmethod
def outputVerbose(*messages):
""" If verbose messages are enabled, print each message provided
Parameters:
*messages (string, or list(string)): messages to print
"""
if (SimpleLogger.verboseLoggingEnabled):
for m in messages: SimpleLogger.output(formatter.Formatter.CoerceToList(m), foreground=Fore.RED, background=Back.BLACK)
@staticmethod
def outputCommand(command):
""" Print the command that is to be executed
Parameters:
command (string): the command to be printed
"""
SimpleLogger.output(["\n> ", command], foreground=Fore.CYAN, background=Back.BLACK)
# Initialize SimpleLogger to disable verbose logging by default
SimpleLogger.verboseLoggingEnabled = False