2019-10-31 13:31:07 -05:00
|
|
|
#!/usr/bin/env python3
|
2016-10-28 13:20:27 +02:00
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
|
2021-05-31 16:08:53 +02:00
|
|
|
from config import config
|
|
|
|
|
2016-10-28 13:20:27 +02:00
|
|
|
""" @var formatting delimiter consisting of '=' characters """
|
2022-04-26 19:02:15 +02:00
|
|
|
double_line_delim = "=" * 78
|
2016-10-28 13:20:27 +02:00
|
|
|
""" @var formatting delimiter consisting of '-' characters """
|
2022-04-26 19:02:15 +02:00
|
|
|
single_line_delim = "-" * 78
|
2016-10-28 13:20:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
def colorize(msg, color):
|
2021-04-08 19:37:41 +02:00
|
|
|
return f"{color}{msg}{COLOR_RESET}"
|
2016-10-28 13:20:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ColorFormatter(logging.Formatter):
|
|
|
|
def init(self, fmt=None, datefmt=None):
|
|
|
|
super(ColorFormatter, self).__init__(fmt, datefmt)
|
|
|
|
|
|
|
|
def format(self, record):
|
|
|
|
message = super(ColorFormatter, self).format(record)
|
2022-04-26 19:02:15 +02:00
|
|
|
if hasattr(record, "color"):
|
2016-10-28 13:20:27 +02:00
|
|
|
message = colorize(message, record.color)
|
|
|
|
return message
|
2021-05-31 16:08:53 +02:00
|
|
|
|
2016-10-28 13:20:27 +02:00
|
|
|
|
|
|
|
# 40 = ERROR, 30 = WARNING, 20 = INFO, 10 = DEBUG, 0 = NOTSET (all messages)
|
2021-05-31 16:08:53 +02:00
|
|
|
if config.verbose >= 2:
|
2016-10-28 13:20:27 +02:00
|
|
|
log_level = 10
|
2021-05-31 16:08:53 +02:00
|
|
|
elif config.verbose == 1:
|
2016-10-28 13:20:27 +02:00
|
|
|
log_level = 20
|
|
|
|
else:
|
|
|
|
log_level = 40
|
|
|
|
|
2016-12-23 09:16:39 +01:00
|
|
|
handler = logging.StreamHandler(sys.stdout)
|
2022-04-26 19:02:15 +02:00
|
|
|
color_formatter = ColorFormatter(
|
|
|
|
fmt="%(asctime)s,%(msecs)03d %(message)s", datefmt="%H:%M:%S"
|
|
|
|
)
|
2018-07-16 14:22:01 +02:00
|
|
|
handler.setFormatter(color_formatter)
|
2016-12-23 09:16:39 +01:00
|
|
|
handler.setLevel(log_level)
|
|
|
|
|
|
|
|
global_logger = logging.getLogger()
|
|
|
|
global_logger.addHandler(handler)
|
|
|
|
|
2016-10-28 13:20:27 +02:00
|
|
|
scapy_logger = logging.getLogger("scapy.runtime")
|
|
|
|
scapy_logger.setLevel(logging.ERROR)
|
|
|
|
|
|
|
|
|
2018-11-09 11:58:54 +01:00
|
|
|
def get_logger(name):
|
2016-10-28 13:20:27 +02:00
|
|
|
logger = logging.getLogger(name)
|
2016-12-23 09:16:39 +01:00
|
|
|
logger.setLevel(logging.DEBUG)
|
2016-10-28 13:20:27 +02:00
|
|
|
return logger
|
|
|
|
|
2018-07-16 14:22:01 +02:00
|
|
|
|
|
|
|
def get_parallel_logger(stream):
|
2022-04-26 19:02:15 +02:00
|
|
|
logger = logging.getLogger("parallel_logger_{!s}".format(stream))
|
2018-07-16 14:22:01 +02:00
|
|
|
logger.propagate = False
|
2018-08-24 16:16:28 +02:00
|
|
|
logger.setLevel(logging.DEBUG)
|
2018-07-16 14:22:01 +02:00
|
|
|
handler = logging.StreamHandler(stream)
|
|
|
|
handler.setFormatter(color_formatter)
|
|
|
|
handler.setLevel(log_level)
|
|
|
|
logger.addHandler(handler)
|
|
|
|
return logger
|
|
|
|
|
2021-05-31 16:08:53 +02:00
|
|
|
|
2016-10-28 13:20:27 +02:00
|
|
|
# Static variables to store color formatting strings.
|
|
|
|
#
|
|
|
|
# These variables (RED, GREEN, YELLOW and LPURPLE) are used to configure
|
|
|
|
# the color of the text to be printed in the terminal. Variable COLOR_RESET
|
|
|
|
# is used to revert the text color to the default one.
|
2022-04-26 19:02:15 +02:00
|
|
|
if hasattr(sys.stdout, "isatty") and sys.stdout.isatty():
|
|
|
|
RED = "\033[91m"
|
|
|
|
GREEN = "\033[92m"
|
|
|
|
YELLOW = "\033[93m"
|
|
|
|
LPURPLE = "\033[94m"
|
|
|
|
COLOR_RESET = "\033[0m"
|
2016-10-28 13:20:27 +02:00
|
|
|
else:
|
2022-04-26 19:02:15 +02:00
|
|
|
RED = ""
|
|
|
|
GREEN = ""
|
|
|
|
YELLOW = ""
|
|
|
|
LPURPLE = ""
|
|
|
|
COLOR_RESET = ""
|