blob: f848e2210449cc2cd66a2886d74e5b63e6b0389e [file] [log] [blame]
Renato Botelho do Coutoead1e532019-10-31 13:31:07 -05001#!/usr/bin/env python3
Klement Sekera277b89c2016-10-28 13:20:27 +02002
3import sys
4import os
5import logging
6
Klement Sekerab23ffd72021-05-31 16:08:53 +02007from config import config
8
Klement Sekera277b89c2016-10-28 13:20:27 +02009""" @var formatting delimiter consisting of '=' characters """
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020010double_line_delim = "=" * 78
Klement Sekera277b89c2016-10-28 13:20:27 +020011""" @var formatting delimiter consisting of '-' characters """
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020012single_line_delim = "-" * 78
Klement Sekera277b89c2016-10-28 13:20:27 +020013
14
15def colorize(msg, color):
Klement Sekera558ceab2021-04-08 19:37:41 +020016 return f"{color}{msg}{COLOR_RESET}"
Klement Sekera277b89c2016-10-28 13:20:27 +020017
18
19class ColorFormatter(logging.Formatter):
Klement Sekera277b89c2016-10-28 13:20:27 +020020 def init(self, fmt=None, datefmt=None):
21 super(ColorFormatter, self).__init__(fmt, datefmt)
22
23 def format(self, record):
24 message = super(ColorFormatter, self).format(record)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020025 if hasattr(record, "color"):
Klement Sekera277b89c2016-10-28 13:20:27 +020026 message = colorize(message, record.color)
27 return message
Klement Sekerab23ffd72021-05-31 16:08:53 +020028
Klement Sekera277b89c2016-10-28 13:20:27 +020029
30# 40 = ERROR, 30 = WARNING, 20 = INFO, 10 = DEBUG, 0 = NOTSET (all messages)
Klement Sekerab23ffd72021-05-31 16:08:53 +020031if config.verbose >= 2:
Klement Sekera277b89c2016-10-28 13:20:27 +020032 log_level = 10
Klement Sekerab23ffd72021-05-31 16:08:53 +020033elif config.verbose == 1:
Klement Sekera277b89c2016-10-28 13:20:27 +020034 log_level = 20
35else:
36 log_level = 40
37
Klement Sekera6c7440c2016-12-23 09:16:39 +010038handler = logging.StreamHandler(sys.stdout)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020039color_formatter = ColorFormatter(
40 fmt="%(asctime)s,%(msecs)03d %(message)s", datefmt="%H:%M:%S"
41)
juraj.linkes184870a2018-07-16 14:22:01 +020042handler.setFormatter(color_formatter)
Klement Sekera6c7440c2016-12-23 09:16:39 +010043handler.setLevel(log_level)
44
45global_logger = logging.getLogger()
46global_logger.addHandler(handler)
47
Klement Sekera277b89c2016-10-28 13:20:27 +020048scapy_logger = logging.getLogger("scapy.runtime")
49scapy_logger.setLevel(logging.ERROR)
50
51
juraj.linkesdfb5f2a2018-11-09 11:58:54 +010052def get_logger(name):
Klement Sekera277b89c2016-10-28 13:20:27 +020053 logger = logging.getLogger(name)
Klement Sekera6c7440c2016-12-23 09:16:39 +010054 logger.setLevel(logging.DEBUG)
Klement Sekera277b89c2016-10-28 13:20:27 +020055 return logger
56
juraj.linkes184870a2018-07-16 14:22:01 +020057
58def get_parallel_logger(stream):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020059 logger = logging.getLogger("parallel_logger_{!s}".format(stream))
juraj.linkes184870a2018-07-16 14:22:01 +020060 logger.propagate = False
juraj.linkes0219b8d2018-08-24 16:16:28 +020061 logger.setLevel(logging.DEBUG)
juraj.linkes184870a2018-07-16 14:22:01 +020062 handler = logging.StreamHandler(stream)
63 handler.setFormatter(color_formatter)
64 handler.setLevel(log_level)
65 logger.addHandler(handler)
66 return logger
67
Klement Sekerab23ffd72021-05-31 16:08:53 +020068
Klement Sekera277b89c2016-10-28 13:20:27 +020069# Static variables to store color formatting strings.
70#
71# These variables (RED, GREEN, YELLOW and LPURPLE) are used to configure
72# the color of the text to be printed in the terminal. Variable COLOR_RESET
73# is used to revert the text color to the default one.
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020074if hasattr(sys.stdout, "isatty") and sys.stdout.isatty():
75 RED = "\033[91m"
76 GREEN = "\033[92m"
77 YELLOW = "\033[93m"
78 LPURPLE = "\033[94m"
79 COLOR_RESET = "\033[0m"
Klement Sekera277b89c2016-10-28 13:20:27 +020080else:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020081 RED = ""
82 GREEN = ""
83 YELLOW = ""
84 LPURPLE = ""
85 COLOR_RESET = ""