summaryrefslogtreecommitdiffstats
path: root/custodia/log.py
diff options
context:
space:
mode:
authorSimo Sorce <simo@redhat.com>2015-06-05 10:56:29 -0400
committerSimo Sorce <simo@redhat.com>2015-06-05 10:56:29 -0400
commiteff99ee7a065e6e122dbd7cee136a2651073d224 (patch)
tree40761c5cc8ac6ff0cc270b4eb0faa3e62beda368 /custodia/log.py
parentf68ec64138d5b259788f25b54deec12e80a95ec7 (diff)
downloadcustodia-eff99ee7a065e6e122dbd7cee136a2651073d224.tar.gz
custodia-eff99ee7a065e6e122dbd7cee136a2651073d224.tar.xz
custodia-eff99ee7a065e6e122dbd7cee136a2651073d224.zip
Add basic debugging capabilities
If debug is set to True, then custodia's own Exception handlers will print a stack trace to standard output to aid debugging. Signed-off-by: Simo Sorce <simo@redhat.com>
Diffstat (limited to 'custodia/log.py')
-rw-r--r--custodia/log.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/custodia/log.py b/custodia/log.py
new file mode 100644
index 0000000..12a6ba7
--- /dev/null
+++ b/custodia/log.py
@@ -0,0 +1,31 @@
+# Copyright (C) 2015 Custodia Project Contributors - see LICENSE file
+
+import io
+import sys
+import traceback
+import time
+
+
+DEBUG = False
+
+
+def stacktrace():
+ with io.BytesIO() as f:
+ _, _, tb = sys.exc_info()
+ traceback.print_tb(tb, None, file=f)
+ del tb
+ return f.getvalue()
+
+
+def error(msg, head=None):
+ if head is not None:
+ t = time.gmtime(time.time())
+ head = '%04d/%02d/%02d %02d:%02d:%02d' % (
+ t[0], t[1], t[2], t[3], t[4], t[5])
+ sys.stderr.write('[%s] %s\n' % (head, msg))
+
+
+def debug(msg):
+ if DEBUG:
+ error(msg, 'DEBUG')
+ sys.stderr.write(stacktrace())