blob: cca542b452706395ef2a83de17d6118173f7c69e [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
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)
41handler.setFormatter(ColorFormatter(fmt='%(asctime)s,%(msecs)03d %(message)s',
42 datefmt="%H:%M:%S"))
43handler.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
52def getLogger(name):
53 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
57# Static variables to store color formatting strings.
58#
59# These variables (RED, GREEN, YELLOW and LPURPLE) are used to configure
60# the color of the text to be printed in the terminal. Variable COLOR_RESET
61# is used to revert the text color to the default one.
62if hasattr(sys.stdout, 'isatty') and sys.stdout.isatty():
63 RED = '\033[91m'
64 GREEN = '\033[92m'
65 YELLOW = '\033[93m'
66 LPURPLE = '\033[94m'
67 COLOR_RESET = '\033[0m'
68else:
69 RED = ''
70 GREEN = ''
71 YELLOW = ''
72 LPURPLE = ''
73 COLOR_RESET = ''