blob: 9701aecc04a1e2cb33a22bd6807ae38687c4e3f5 [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 Sekera52e84f32017-01-13 07:25:25 +010010double_line_delim = '=' * 78
Klement Sekera277b89c2016-10-28 13:20:27 +020011""" @var formatting delimiter consisting of '-' characters """
Klement Sekera52e84f32017-01-13 07:25:25 +010012single_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):
20
21 def init(self, fmt=None, datefmt=None):
22 super(ColorFormatter, self).__init__(fmt, datefmt)
23
24 def format(self, record):
25 message = super(ColorFormatter, self).format(record)
26 if hasattr(record, 'color'):
27 message = colorize(message, record.color)
28 return message
Klement Sekerab23ffd72021-05-31 16:08:53 +020029
Klement Sekera277b89c2016-10-28 13:20:27 +020030
31# 40 = ERROR, 30 = WARNING, 20 = INFO, 10 = DEBUG, 0 = NOTSET (all messages)
Klement Sekerab23ffd72021-05-31 16:08:53 +020032if config.verbose >= 2:
Klement Sekera277b89c2016-10-28 13:20:27 +020033 log_level = 10
Klement Sekerab23ffd72021-05-31 16:08:53 +020034elif config.verbose == 1:
Klement Sekera277b89c2016-10-28 13:20:27 +020035 log_level = 20
36else:
37 log_level = 40
38
Klement Sekera6c7440c2016-12-23 09:16:39 +010039handler = logging.StreamHandler(sys.stdout)
juraj.linkes184870a2018-07-16 14:22:01 +020040color_formatter = ColorFormatter(fmt='%(asctime)s,%(msecs)03d %(message)s',
41 datefmt="%H:%M:%S")
42handler.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):
Paul Vinciguerraea2450f2019-03-06 08:23:58 -080059 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.
74if 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'
80else:
81 RED = ''
82 GREEN = ''
83 YELLOW = ''
84 LPURPLE = ''
85 COLOR_RESET = ''