aboutsummaryrefslogtreecommitdiff
path: root/pyecsca/misc
diff options
context:
space:
mode:
authorJ08nY2023-10-16 20:11:52 +0200
committerJ08nY2023-10-16 20:11:52 +0200
commita8d60bff6d4c70698cb4145cf1cc53cf215d4ad4 (patch)
tree38f3a278eed73168d59e85438eca6d65cc04f7b0 /pyecsca/misc
parent6e33ca6b07dd09f42949c5f9a3f24e2d87c852e8 (diff)
downloadpyecsca-a8d60bff6d4c70698cb4145cf1cc53cf215d4ad4.tar.gz
pyecsca-a8d60bff6d4c70698cb4145cf1cc53cf215d4ad4.tar.zst
pyecsca-a8d60bff6d4c70698cb4145cf1cc53cf215d4ad4.zip
Diffstat (limited to 'pyecsca/misc')
-rw-r--r--pyecsca/misc/cfg.py9
-rw-r--r--pyecsca/misc/utils.py28
2 files changed, 37 insertions, 0 deletions
diff --git a/pyecsca/misc/cfg.py b/pyecsca/misc/cfg.py
index ee9c95d..f73eea9 100644
--- a/pyecsca/misc/cfg.py
+++ b/pyecsca/misc/cfg.py
@@ -121,6 +121,12 @@ class ECConfig:
raise ValueError("Bad Mod implementaiton, can be one of 'python', 'gmp' or 'symbolic'.")
self._mod_implementation = value
+@public
+class LoggingConfig:
+ """Logging configuration."""
+
+ enabled: bool = True
+ """Whether logging is enabled."""
@public
class Config:
@@ -128,9 +134,12 @@ class Config:
ec: ECConfig
"""Configuration for the :py:mod:`pyecsca.ec` package."""
+ log: LoggingConfig
+ """Logging configuration."""
def __init__(self):
self.ec = ECConfig()
+ self.log = LoggingConfig()
_config: ContextVar[Config] = ContextVar("config", default=Config())
diff --git a/pyecsca/misc/utils.py b/pyecsca/misc/utils.py
index 1e9e9a0..010bbd2 100644
--- a/pyecsca/misc/utils.py
+++ b/pyecsca/misc/utils.py
@@ -1,6 +1,9 @@
"""Just some utilities I promise."""
+import sys
from ast import parse
+from pyecsca.misc.cfg import getconfig
+
def pexec(s):
return parse(s, mode="exec")
@@ -8,3 +11,28 @@ def pexec(s):
def peval(s):
return parse(s, mode="eval")
+
+
+def in_notebook() -> bool:
+ """Test whether we are executing in Jupyter notebook."""
+ try:
+ from IPython import get_ipython
+ if 'IPKernelApp' not in get_ipython().config: # pragma: no cover
+ return False
+ except ImportError:
+ return False
+ except AttributeError:
+ return False
+ return True
+
+
+def log(*args, **kwargs):
+ """Log a message."""
+ if in_notebook() and getconfig().log.enabled:
+ print(*args, **kwargs)
+
+
+def warn(*args, **kwargs):
+ """Log a message."""
+ if in_notebook() and getconfig().log.enabled:
+ print(*args, **kwargs, file=sys.stderr)