blob: 38610b7ee8b3241a603371eb83d4200097309f63 [file] [log] [blame]
Klement Sekera277b89c2016-10-28 13:20:27 +02001#!/usr/bin/env python
2
3import sys
4import os
5import logging
6
7""" @var formatting delimiter consisting of '=' characters """
8double_line_delim = '=' * 70
9""" @var formatting delimiter consisting of '-' characters """
10single_line_delim = '-' * 70
11
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
27
28handler = logging.StreamHandler(sys.stdout)
29handler.setFormatter(ColorFormatter())
30
31global_logger = logging.getLogger()
32global_logger.addHandler(handler)
33try:
34 verbose = int(os.getenv("V", 0))
35except:
36 verbose = 0
37
38# 40 = ERROR, 30 = WARNING, 20 = INFO, 10 = DEBUG, 0 = NOTSET (all messages)
39if verbose >= 2:
40 log_level = 10
41elif verbose == 1:
42 log_level = 20
43else:
44 log_level = 40
45
46scapy_logger = logging.getLogger("scapy.runtime")
47scapy_logger.setLevel(logging.ERROR)
48
49
50def getLogger(name):
51 logger = logging.getLogger(name)
52 logger.setLevel(log_level)
53 return logger
54
55# Static variables to store color formatting strings.
56#
57# These variables (RED, GREEN, YELLOW and LPURPLE) are used to configure
58# the color of the text to be printed in the terminal. Variable COLOR_RESET
59# is used to revert the text color to the default one.
60if hasattr(sys.stdout, 'isatty') and sys.stdout.isatty():
61 RED = '\033[91m'
62 GREEN = '\033[92m'
63 YELLOW = '\033[93m'
64 LPURPLE = '\033[94m'
65 COLOR_RESET = '\033[0m'
66else:
67 RED = ''
68 GREEN = ''
69 YELLOW = ''
70 LPURPLE = ''
71 COLOR_RESET = ''