11831e9bbSHanoh Haim## This file is part of Scapy
21831e9bbSHanoh Haim## See http://www.secdev.org/projects/scapy for more informations
31831e9bbSHanoh Haim## Copyright (C) Philippe Biondi <phil@secdev.org>
41831e9bbSHanoh Haim## This program is published under a GPLv2 license
51831e9bbSHanoh Haim
61831e9bbSHanoh Haim"""
71831e9bbSHanoh HaimLogging subsystem and basic exception class.
81831e9bbSHanoh Haim"""
91831e9bbSHanoh Haim
101831e9bbSHanoh Haim#############################
111831e9bbSHanoh Haim##### Logging subsystem #####
121831e9bbSHanoh Haim#############################
131831e9bbSHanoh Haim
141831e9bbSHanoh Haimclass Scapy_Exception(Exception):
151831e9bbSHanoh Haim    pass
161831e9bbSHanoh Haim
171831e9bbSHanoh Haimimport logging,traceback,time
181831e9bbSHanoh Haim
191831e9bbSHanoh Haimclass ScapyFreqFilter(logging.Filter):
201831e9bbSHanoh Haim    def __init__(self):
211831e9bbSHanoh Haim        logging.Filter.__init__(self)
221831e9bbSHanoh Haim        self.warning_table = {}
231831e9bbSHanoh Haim    def filter(self, record):
241831e9bbSHanoh Haim        from .config import conf
251831e9bbSHanoh Haim        wt = conf.warning_threshold
261831e9bbSHanoh Haim        if wt > 0:
271831e9bbSHanoh Haim            stk = traceback.extract_stack()
281831e9bbSHanoh Haim            caller=None
291831e9bbSHanoh Haim            for f,l,n,c in stk:
301831e9bbSHanoh Haim                if n == 'warning':
311831e9bbSHanoh Haim                    break
321831e9bbSHanoh Haim                caller = l
331831e9bbSHanoh Haim            tm,nb = self.warning_table.get(caller, (0,0))
341831e9bbSHanoh Haim            ltm = time.time()
351831e9bbSHanoh Haim            if ltm-tm > wt:
361831e9bbSHanoh Haim                tm = ltm
371831e9bbSHanoh Haim                nb = 0
381831e9bbSHanoh Haim            else:
391831e9bbSHanoh Haim                if nb < 2:
401831e9bbSHanoh Haim                    nb += 1
411831e9bbSHanoh Haim                    if nb == 2:
421831e9bbSHanoh Haim                        record.msg = "more "+record.msg
431831e9bbSHanoh Haim                else:
441831e9bbSHanoh Haim                    return 0
451831e9bbSHanoh Haim            self.warning_table[caller] = (tm,nb)
461831e9bbSHanoh Haim        return 1
471831e9bbSHanoh Haim
481831e9bbSHanoh Haimlog_scapy = logging.getLogger("scapy")
491831e9bbSHanoh Haimconsole_handler = logging.StreamHandler()
501831e9bbSHanoh Haimconsole_handler.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
511831e9bbSHanoh Haimlog_scapy.addHandler(console_handler)
521831e9bbSHanoh Haimlog_runtime = logging.getLogger("scapy.runtime")          # logs at runtime
531831e9bbSHanoh Haimlog_runtime.addFilter(ScapyFreqFilter())
541831e9bbSHanoh Haimlog_interactive = logging.getLogger("scapy.interactive")  # logs in interactive functions
551831e9bbSHanoh Haimlog_loading = logging.getLogger("scapy.loading")          # logs when loading scapy
561831e9bbSHanoh Haim
571831e9bbSHanoh Haim
581831e9bbSHanoh Haimdef warning(x):
591831e9bbSHanoh Haim    log_runtime.warning(x)
601831e9bbSHanoh Haim
61