blob: b0fe037e1ba35e14d9ea8fd89a57a7e5aab8832a [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
7""" @var formatting delimiter consisting of '=' characters """
Klement Sekera52e84f32017-01-13 07:25:25 +01008double_line_delim = '=' * 78
Klement Sekera277b89c2016-10-28 13:20:27 +02009""" @var formatting delimiter consisting of '-' characters """
Klement Sekera52e84f32017-01-13 07:25:25 +010010single_line_delim = '-' * 78
Klement Sekera277b89c2016-10-28 13:20:27 +020011
12
13def colorize(msg, color):
14 return color + msg + COLOR_RESET
15
16
17class ColorFormatter(logging.Formatter):
18
19 def init(self, fmt=None, datefmt=None):
20 super(ColorFormatter, self).__init__(fmt, datefmt)
21
22 def format(self, record):
23 message = super(ColorFormatter, self).format(record)
24 if hasattr(record, 'color'):
25 message = colorize(message, record.color)
26 return message
Klement Sekera277b89c2016-10-28 13:20:27 +020027try:
28 verbose = int(os.getenv("V", 0))
29except:
30 verbose = 0
31
32# 40 = ERROR, 30 = WARNING, 20 = INFO, 10 = DEBUG, 0 = NOTSET (all messages)
33if verbose >= 2:
34 log_level = 10
35elif verbose == 1:
36 log_level = 20
37else:
38 log_level = 40
39
Klement Sekera6c7440c2016-12-23 09:16:39 +010040handler = logging.StreamHandler(sys.stdout)
juraj.linkes184870a2018-07-16 14:22:01 +020041color_formatter = ColorFormatter(fmt='%(asctime)s,%(msecs)03d %(message)s',
42 datefmt="%H:%M:%S")
43handler.setFormatter(color_formatter)
Klement Sekera6c7440c2016-12-23 09:16:39 +010044handler.setLevel(log_level)
45
46global_logger = logging.getLogger()
47global_logger.addHandler(handler)
48
Klement Sekera277b89c2016-10-28 13:20:27 +020049scapy_logger = logging.getLogger("scapy.runtime")
50scapy_logger.setLevel(logging.ERROR)
51
52
juraj.linkesdfb5f2a2018-11-09 11:58:54 +010053def get_logger(name):
Klement Sekera277b89c2016-10-28 13:20:27 +020054 logger = logging.getLogger(name)
Klement Sekera6c7440c2016-12-23 09:16:39 +010055 logger.setLevel(logging.DEBUG)
Klement Sekera277b89c2016-10-28 13:20:27 +020056 return logger
57
juraj.linkes184870a2018-07-16 14:22:01 +020058
59def get_parallel_logger(stream):
Paul Vinciguerraea2450f2019-03-06 08:23:58 -080060 logger = logging.getLogger('parallel_logger_{!s}'.format(stream))
juraj.linkes184870a2018-07-16 14:22:01 +020061 logger.propagate = False
juraj.linkes0219b8d2018-08-24 16:16:28 +020062 logger.setLevel(logging.DEBUG)
juraj.linkes184870a2018-07-16 14:22:01 +020063 handler = logging.StreamHandler(stream)
64 handler.setFormatter(color_formatter)
65 handler.setLevel(log_level)
66 logger.addHandler(handler)
67 return logger
68
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 = ''