summaryrefslogtreecommitdiff
path: root/Tools/c-analyzer/c_common/logging.py
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2020-10-22 18:42:51 -0600
committerGitHub <noreply@github.com>2020-10-22 18:42:51 -0600
commit345cd37abe324ad4f60f80e2c3133b8849e54e9b (patch)
tree5d965e662dca9dcac19e7eddd63a3d9d0b816fed /Tools/c-analyzer/c_common/logging.py
parentec388cfb4ede56dace2bb78851ff6f38fa2a6abe (diff)
downloadcpython-git-345cd37abe324ad4f60f80e2c3133b8849e54e9b.tar.gz
bpo-36876: Fix the C analyzer tool. (GH-22841)
The original tool wasn't working right and it was simpler to create a new one, partially re-using some of the old code. At this point the tool runs properly on the master. (Try: ./python Tools/c-analyzer/c-analyzer.py analyze.) It take ~40 seconds on my machine to analyze the full CPython code base. Note that we'll need to iron out some OS-specific stuff (e.g. preprocessor). We're okay though since this tool isn't used yet in our workflow. We will also need to verify the analysis results in detail before activating the check in CI, though I'm pretty sure it's close. https://bugs.python.org/issue36876
Diffstat (limited to 'Tools/c-analyzer/c_common/logging.py')
-rw-r--r--Tools/c-analyzer/c_common/logging.py63
1 files changed, 63 insertions, 0 deletions
diff --git a/Tools/c-analyzer/c_common/logging.py b/Tools/c-analyzer/c_common/logging.py
new file mode 100644
index 0000000000..12398f7e38
--- /dev/null
+++ b/Tools/c-analyzer/c_common/logging.py
@@ -0,0 +1,63 @@
+import logging
+import sys
+
+
+VERBOSITY = 3
+
+
+# The root logger for the whole top-level package:
+_logger = logging.getLogger(__name__.rpartition('.')[0])
+
+
+def configure_logger(logger, verbosity=VERBOSITY, *,
+ logfile=None,
+ maxlevel=logging.CRITICAL,
+ ):
+ level = max(1, # 0 disables it, so we use the next lowest.
+ min(maxlevel,
+ maxlevel - verbosity * 10))
+ logger.setLevel(level)
+ #logger.propagate = False
+
+ if not logger.handlers:
+ if logfile:
+ handler = logging.FileHandler(logfile)
+ else:
+ handler = logging.StreamHandler(sys.stdout)
+ handler.setLevel(level)
+ #handler.setFormatter(logging.Formatter())
+ logger.addHandler(handler)
+
+ # In case the provided logger is in a sub-package...
+ if logger is not _logger:
+ configure_logger(
+ _logger,
+ verbosity,
+ logfile=logfile,
+ maxlevel=maxlevel,
+ )
+
+
+def hide_emit_errors():
+ """Ignore errors while emitting log entries.
+
+ Rather than printing a message desribing the error, we show nothing.
+ """
+ # For now we simply ignore all exceptions. If we wanted to ignore
+ # specific ones (e.g. BrokenPipeError) then we would need to use
+ # a Handler subclass with a custom handleError() method.
+ orig = logging.raiseExceptions
+ logging.raiseExceptions = False
+ def restore():
+ logging.raiseExceptions = orig
+ return restore
+
+
+class Printer:
+ def __init__(self, verbosity=VERBOSITY):
+ self.verbosity = verbosity
+
+ def info(self, *args, **kwargs):
+ if self.verbosity < 3:
+ return
+ print(*args, **kwargs)