1 # Copyright (c) 2011 Neal H. Walfield
3 # This software is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
8 # This software is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 from __future__ import with_statement
28 original_excepthook = None
30 def my_excepthook(exctype, value, tb):
31 """Log uncaught exceptions."""
33 "Uncaught exception: %s"
34 % (''.join(traceback.format_exception(exctype, value, tb)),))
35 original_excepthook(exctype, value, tb)
37 def init(dot_directory, debug=False, max_logfiles=1, program_name=None):
39 if not os.path.isabs(dot_directory):
40 dot_directory = os.path.join(os.path.expanduser("~"), dot_directory)
42 logging_directory = os.path.join(dot_directory, "logging")
44 os.makedirs(logging_directory)
46 if e.errno != errno.EEXIST:
49 if program_name is None:
50 program_name = os.path.basename(sys.argv[0])
51 string.translate(program_name, string.maketrans(' .', '__'))
53 timestamp = time.strftime("%Y%m%d")
55 logfiles = glob.glob(os.path.join(logging_directory,
56 program_name + '-*.log'))
57 if len(logfiles) >= max_logfiles:
59 for f in logfiles[:-(max_logfiles+1)]:
60 print "Purging old log file %s" % (f,)
64 print "Removing %s: %s" % (f, str(e))
66 logfile = os.path.join(logging_directory,
67 program_name + '-' + timestamp + '.log')
69 print "Sending output to %s" % logfile
72 logger = logging.getLogger(__name__)
81 format=('%(asctime)s (pid: ' + str(os.getpid()) + ') '
82 + '%(levelname)-8s %(message)s'),
86 # Log uncaught exceptions.
87 global original_excepthook
88 original_excepthook = sys.excepthook
89 sys.excepthook = my_excepthook
92 filename = os.path.join(logging_directory, program_name + '.' + thing)
94 with open(filename, "r") as fhandle:
95 contents = fhandle.read()
97 if e.errno in (errno.ENOENT,):
101 logging.error("Reading %s: %s" % (filename, str(e)))
104 logging.error("std%s of last run: %s" % (thing, contents))
106 if fhandle is not None:
109 print "Redirecting std%s to %s" % (thing, filename)
110 return open(filename, "w", 0)
112 sys.stderr = redirect('err')
113 sys.stdout = redirect('out')