Back to index

system-config-printer  1.3.9+20120706
debug.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 
00003 ## Copyright (C) 2008, 2010 Red Hat, Inc.
00004 ## Authors:
00005 ##  Tim Waugh <twaugh@redhat.com>
00006 
00007 ## This program is free software; you can redistribute it and/or modify
00008 ## it under the terms of the GNU General Public License as published by
00009 ## the Free Software Foundation; either version 2 of the License, or
00010 ## (at your option) any later version.
00011 
00012 ## This program is distributed in the hope that it will be useful,
00013 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 ## GNU General Public License for more details.
00016 
00017 ## You should have received a copy of the GNU General Public License
00018 ## along with this program; if not, write to the Free Software
00019 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00020 
00021 import sys
00022 import traceback
00023 
00024 _debug=False
00025 def debugprint (x):
00026     if _debug:
00027         try:
00028             sys.stderr.write (x + "\n")
00029             sys.stderr.flush ()
00030         except:
00031             pass
00032 
00033 def get_debugging ():
00034     return _debug
00035 
00036 def set_debugging (d):
00037     global _debug
00038     _debug = d
00039 
00040 def fatalException (exitcode=1):
00041     nonfatalException (type="fatal", end="Exiting")
00042     sys.exit (exitcode)
00043 
00044 def nonfatalException (type="non-fatal", end="Continuing anyway.."):
00045     d = get_debugging ()
00046     set_debugging (True)
00047     debugprint ("Caught %s exception.  Traceback:" % type)
00048     (type, value, tb) = sys.exc_info ()
00049     extxt = traceback.format_exception_only (type, value)
00050     for line in traceback.format_tb(tb):
00051         debugprint (line.strip ())
00052     debugprint (extxt[0].strip ())
00053     debugprint (end)
00054     set_debugging (d)