summaryrefslogtreecommitdiffstats
path: root/custodia/log.py
blob: 12a6ba78a75dcb5735817a4d27af79dc88db0314 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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())