Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import sys |
| 4 | import os |
| 5 | import logging |
| 6 | |
| 7 | """ @var formatting delimiter consisting of '=' characters """ |
Klement Sekera | 52e84f3 | 2017-01-13 07:25:25 +0100 | [diff] [blame] | 8 | double_line_delim = '=' * 78 |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 9 | """ @var formatting delimiter consisting of '-' characters """ |
Klement Sekera | 52e84f3 | 2017-01-13 07:25:25 +0100 | [diff] [blame] | 10 | single_line_delim = '-' * 78 |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 11 | |
| 12 | |
| 13 | def colorize(msg, color): |
| 14 | return color + msg + COLOR_RESET |
| 15 | |
| 16 | |
| 17 | class 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 Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 27 | try: |
| 28 | verbose = int(os.getenv("V", 0)) |
| 29 | except: |
| 30 | verbose = 0 |
| 31 | |
| 32 | # 40 = ERROR, 30 = WARNING, 20 = INFO, 10 = DEBUG, 0 = NOTSET (all messages) |
| 33 | if verbose >= 2: |
| 34 | log_level = 10 |
| 35 | elif verbose == 1: |
| 36 | log_level = 20 |
| 37 | else: |
| 38 | log_level = 40 |
| 39 | |
Klement Sekera | 6c7440c | 2016-12-23 09:16:39 +0100 | [diff] [blame] | 40 | handler = logging.StreamHandler(sys.stdout) |
juraj.linkes | 184870a | 2018-07-16 14:22:01 +0200 | [diff] [blame] | 41 | color_formatter = ColorFormatter(fmt='%(asctime)s,%(msecs)03d %(message)s', |
| 42 | datefmt="%H:%M:%S") |
| 43 | handler.setFormatter(color_formatter) |
Klement Sekera | 6c7440c | 2016-12-23 09:16:39 +0100 | [diff] [blame] | 44 | handler.setLevel(log_level) |
| 45 | |
| 46 | global_logger = logging.getLogger() |
| 47 | global_logger.addHandler(handler) |
| 48 | |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 49 | scapy_logger = logging.getLogger("scapy.runtime") |
| 50 | scapy_logger.setLevel(logging.ERROR) |
| 51 | |
| 52 | |
| 53 | def getLogger(name): |
| 54 | logger = logging.getLogger(name) |
Klement Sekera | 6c7440c | 2016-12-23 09:16:39 +0100 | [diff] [blame] | 55 | logger.setLevel(logging.DEBUG) |
Klement Sekera | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 56 | return logger |
| 57 | |
juraj.linkes | 184870a | 2018-07-16 14:22:01 +0200 | [diff] [blame] | 58 | |
| 59 | def get_parallel_logger(stream): |
| 60 | logger = logging.getLogger('parallel_logger_{}'.format(stream)) |
| 61 | logger.propagate = False |
| 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 | 277b89c | 2016-10-28 13:20:27 +0200 | [diff] [blame] | 68 | # Static variables to store color formatting strings. |
| 69 | # |
| 70 | # These variables (RED, GREEN, YELLOW and LPURPLE) are used to configure |
| 71 | # the color of the text to be printed in the terminal. Variable COLOR_RESET |
| 72 | # is used to revert the text color to the default one. |
| 73 | if hasattr(sys.stdout, 'isatty') and sys.stdout.isatty(): |
| 74 | RED = '\033[91m' |
| 75 | GREEN = '\033[92m' |
| 76 | YELLOW = '\033[93m' |
| 77 | LPURPLE = '\033[94m' |
| 78 | COLOR_RESET = '\033[0m' |
| 79 | else: |
| 80 | RED = '' |
| 81 | GREEN = '' |
| 82 | YELLOW = '' |
| 83 | LPURPLE = '' |
| 84 | COLOR_RESET = '' |