Renato Botelho do Couto | ead1e53 | 2019-10-31 13:31:07 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 2 | |
| 3 | import sys |
| 4 | import os |
| 5 | import logging |
| 6 | |
Klement Sekera | b23ffd7 | 2021-05-31 16:08:53 +0200 | [diff] [blame] | 7 | from config import config |
| 8 | |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 9 | """ @var formatting delimiter consisting of '=' characters """ |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 10 | double_line_delim = "=" * 78 |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 11 | """ @var formatting delimiter consisting of '-' characters """ |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 12 | single_line_delim = "-" * 78 |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 13 | |
| 14 | |
| 15 | def colorize(msg, color): |
Klement Sekera | 558ceab | 2021-04-08 19:37:41 +0200 | [diff] [blame] | 16 | return f"{color}{msg}{COLOR_RESET}" |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 17 | |
| 18 | |
| 19 | class ColorFormatter(logging.Formatter): |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 20 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 25 | if hasattr(record, "color"): |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 26 | message = colorize(message, record.color) |
| 27 | return message |
Klement Sekera | b23ffd7 | 2021-05-31 16:08:53 +0200 | [diff] [blame] | 28 | |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 29 | |
| 30 | # 40 = ERROR, 30 = WARNING, 20 = INFO, 10 = DEBUG, 0 = NOTSET (all messages) |
Klement Sekera | b23ffd7 | 2021-05-31 16:08:53 +0200 | [diff] [blame] | 31 | if config.verbose >= 2: |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 32 | log_level = 10 |
Klement Sekera | b23ffd7 | 2021-05-31 16:08:53 +0200 | [diff] [blame] | 33 | elif config.verbose == 1: |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 34 | log_level = 20 |
| 35 | else: |
| 36 | log_level = 40 |
| 37 | |
Klement Sekera | 6c7440c | 2016-12-23 09:16:39 +0100 | [diff] [blame] | 38 | handler = logging.StreamHandler(sys.stdout) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 39 | color_formatter = ColorFormatter( |
| 40 | fmt="%(asctime)s,%(msecs)03d %(message)s", datefmt="%H:%M:%S" |
| 41 | ) |
juraj.linkes | 184870a | 2018-07-16 14:22:01 +0200 | [diff] [blame] | 42 | handler.setFormatter(color_formatter) |
Klement Sekera | 6c7440c | 2016-12-23 09:16:39 +0100 | [diff] [blame] | 43 | handler.setLevel(log_level) |
| 44 | |
| 45 | global_logger = logging.getLogger() |
| 46 | global_logger.addHandler(handler) |
| 47 | |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 48 | scapy_logger = logging.getLogger("scapy.runtime") |
| 49 | scapy_logger.setLevel(logging.ERROR) |
| 50 | |
| 51 | |
juraj.linkes | dfb5f2a | 2018-11-09 11:58:54 +0100 | [diff] [blame] | 52 | def get_logger(name): |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 53 | logger = logging.getLogger(name) |
Klement Sekera | 6c7440c | 2016-12-23 09:16:39 +0100 | [diff] [blame] | 54 | logger.setLevel(logging.DEBUG) |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 55 | return logger |
| 56 | |
juraj.linkes | 184870a | 2018-07-16 14:22:01 +0200 | [diff] [blame] | 57 | |
| 58 | def get_parallel_logger(stream): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 59 | logger = logging.getLogger("parallel_logger_{!s}".format(stream)) |
juraj.linkes | 184870a | 2018-07-16 14:22:01 +0200 | [diff] [blame] | 60 | logger.propagate = False |
juraj.linkes | 0219b8d | 2018-08-24 16:16:28 +0200 | [diff] [blame] | 61 | logger.setLevel(logging.DEBUG) |
juraj.linkes | 184870a | 2018-07-16 14:22:01 +0200 | [diff] [blame] | 62 | handler = logging.StreamHandler(stream) |
| 63 | handler.setFormatter(color_formatter) |
| 64 | handler.setLevel(log_level) |
| 65 | logger.addHandler(handler) |
| 66 | return logger |
| 67 | |
Klement Sekera | b23ffd7 | 2021-05-31 16:08:53 +0200 | [diff] [blame] | 68 | |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 69 | # 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 74 | if 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 Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 80 | else: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 81 | RED = "" |
| 82 | GREEN = "" |
| 83 | YELLOW = "" |
| 84 | LPURPLE = "" |
| 85 | COLOR_RESET = "" |