Debian Codegraph @

debug.py

raw
1 #!/usr/bin/python3
2
3 ## Copyright (C) 2008, 2010 Red Hat, Inc.
4 ## Authors:
5 ## Tim Waugh <[email protected]>
6
7 ## This program is free software; you can redistribute it and/or modify
8 ## it under the terms of the GNU General Public License as published by
9 ## the Free Software Foundation; either version 2 of the License, or
10 ## (at your option) any later version.
11
12 ## This program is distributed in the hope that it will be useful,
13 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ## GNU General Public License for more details.
16
17 ## You should have received a copy of the GNU General Public License
18 ## along with this program; if not, write to the Free Software
19 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
21 import sys
22 import traceback
23
24 _debug=False
25 def debugprint (x):
26 if _debug:
27 try:
28 sys.stderr.write (x + "\n")
29 sys.stderr.flush ()
30 except:
31 pass
32
33 def get_debugging ():
34 return _debug
35
36 def set_debugging (d):
37 global _debug
38 _debug = d
39
40 def fatalException (exitcode=1):
41 nonfatalException (type="fatal", end="Exiting")
42 sys.exit (exitcode)
43
44 def nonfatalException (type="non-fatal", end="Continuing anyway.."):
45 d = get_debugging ()
46 set_debugging (True)
47 debugprint ("Caught %s exception. Traceback:" % type)
48 (type, value, tb) = sys.exc_info ()
49 extxt = traceback.format_exception_only (type, value)
50 for line in traceback.format_tb(tb):
51 debugprint (line.strip ())
52 debugprint (extxt[0].strip ())
53 debugprint (end)
54 set_debugging (d)