From: Daniel P. Berrange <berrange(a)redhat.com>
---
imgcreate/creator.py | 5 ++-
imgcreate/debug.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++
imgcreate/live.py | 4 +-
imgcreate/yuminst.py | 3 +-
tools/image-creator | 13 ++++++++-
tools/livecd-creator | 14 +++++++++-
6 files changed, 94 insertions(+), 9 deletions(-)
create mode 100644 imgcreate/debug.py
diff --git a/imgcreate/creator.py b/imgcreate/creator.py
index c7b1046..0d22b56 100644
--- a/imgcreate/creator.py
+++ b/imgcreate/creator.py
@@ -22,6 +22,7 @@ import stat
import sys
import tempfile
import shutil
+import logging
import yum
import rpm
@@ -501,7 +502,7 @@ class ImageCreator(object):
(pkg, e))
for pkg in skipped_pkgs:
- print >> sys.stderr, "Skipping missing package '%s'" % (pkg,)
+ logging.info("Skipping missing package '%s'" % (pkg,))
def __select_groups(self, ayum):
skipped_groups = []
@@ -516,7 +517,7 @@ class ImageCreator(object):
skipped_groups.append(group)
for group in skipped_groups:
- print >> sys.stderr, "Skipping missing group '%s'" % (group.name,)
+ logging.info("Skipping missing group '%s'" % (group.name,))
def __deselect_packages(self, ayum):
for pkg in kickstart.get_excluded(self.ks,
diff --git a/imgcreate/debug.py b/imgcreate/debug.py
new file mode 100644
index 0000000..6d725e9
--- /dev/null
+++ b/imgcreate/debug.py
@@ -0,0 +1,64 @@
+#
+# debug.py: Helper routines for debugging
+#
+# Copyright 2008, Red Hat Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Library General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+#
+
+import logging
+import logging.handlers
+import optparse
+import sys
+
+def add_logging_options(parser):
+ logopt = optparse.OptionGroup(parser, "Debugging options",
+ "These options control the output of logging information during image creation")
+
+ # Logging related options
+ logopt.add_option("-d", "--debug", action="store_true", dest="debug",
+ help="Output debugging information")
+ logopt.add_option("-v", "--verbose", action="store_true", dest="verbose",
+ help="Output verbose progress information")
+ logopt.add_option("", "--logfile", type="string", default=None, dest="logfile",
+ help="Save debug information to FILENAME")
+
+ parser.add_option_group(logopt)
+
+
+def setup_logging(verbose=False, debug=False, logfile=None):
+
+ rootLogger = logging.getLogger()
+ if debug:
+ rootLogger.setLevel(logging.DEBUG)
+ elif verbose:
+ rootLogger.setLevel(logging.INFO)
+ else:
+ rootLogger.setLevel(logging.WARN)
+
+ dateFormat = "%a, %d %b %Y %H:%M:%S"
+ if logfile is not None:
+ fileHandler = logging.handlers.RotatingFileHandler(logfile, "a",
+ 1024*1024, 5)
+
+ fileFormat = "[%(asctime)s %(process)d] %(levelname)s (%(module)s:%(lineno)d) %(message)s"
+ fileHandler.setFormatter(logging.Formatter(fileFormat,
+ dateFormat))
+ rootLogger.addHandler(fileHandler)
+ else:
+ streamHandler = logging.StreamHandler(sys.stderr)
+ streamFormat = "%(levelname)-6s %(message)s"
+ streamHandler.setFormatter(logging.Formatter(streamFormat,
+ dateFormat))
+ rootLogger.addHandler(streamHandler)
diff --git a/imgcreate/live.py b/imgcreate/live.py
index bbb17ef..03a5466 100644
--- a/imgcreate/live.py
+++ b/imgcreate/live.py
@@ -21,6 +21,7 @@ import os.path
import glob
import shutil
import subprocess
+import logging
from imgcreate.errors import *
from imgcreate.fs import *
@@ -279,8 +280,7 @@ class LiveImageCreatorBase(LoopImageCreator):
elif os.path.exists("/usr/lib/anaconda-runtime/implantisomd5"):
implantisomd5 = "/usr/lib/anaconda-runtime/implantisomd5"
else:
- print >> sys.stderr, \
- "isomd5sum not installed; not setting up mediacheck"
+ logging.warn("isomd5sum not installed; not setting up mediacheck")
subprocess.call([implantisomd5, iso])
diff --git a/imgcreate/yuminst.py b/imgcreate/yuminst.py
index 4c9ae40..4725146 100644
--- a/imgcreate/yuminst.py
+++ b/imgcreate/yuminst.py
@@ -18,6 +18,7 @@
import os
import sys
+import logging
import yum
import rpmUtils
@@ -110,7 +111,7 @@ class LiveCDYum(yum.YumBase):
pkgs.remove(x)
self.tsInfo.conditionals[req] = pkgs
else:
- print >> sys.stderr, "No such package %s to remove" %(pkg,)
+ logging.warn("No such package %s to remove" %(pkg,))
def selectGroup(self, grp, include = pykickstart.parser.GROUP_DEFAULT):
yum.YumBase.selectGroup(self, grp)
diff --git a/tools/image-creator b/tools/image-creator
index aca9228..34ce253 100755
--- a/tools/image-creator
+++ b/tools/image-creator
@@ -21,8 +21,10 @@ import os
import sys
import shutil
import optparse
+import logging
import imgcreate
+import imgcreate.debug
def parse_options(args):
parser = optparse.OptionParser(usage = "%prog [--name=<name>] <kickstart>")
@@ -30,6 +32,9 @@ def parse_options(args):
parser.add_option("-n", "--name", type="string", dest="name",
help="Image name and filesystem label")
+ # options relating to logging
+ imgcreate.debug.add_logging_options(parser)
+
(options, args) = parser.parse_args()
if len(args) != 1:
@@ -45,10 +50,14 @@ def main():
print >> sys.stderr, "You must run image-creator as root"
return 1
+ imgcreate.debug.setup_logging(options.verbose,
+ options.debug,
+ options.logfile)
+
try:
ks = imgcreate.read_kickstart(kscfg)
except imgcreate.CreatorError, e:
- print >> sys.stderr, "Error loading kickstart file '%s' : %s" % (kscfg, e)
+ logging.error("Unable to load kickstart file '%s' : %s" % (kscfg, e))
return 1
if options.name:
@@ -61,7 +70,7 @@ def main():
try:
creator.create()
except imgcreate.CreatorError, e:
- print >> sys.stderr, "Error creating image : %s" % e
+ logging.error("Unable to create image : %s" % e)
return 1
finally:
creator.cleanup()
diff --git a/tools/livecd-creator b/tools/livecd-creator
index 7c08323..cacaff2 100755
--- a/tools/livecd-creator
+++ b/tools/livecd-creator
@@ -22,8 +22,10 @@ import os.path
import sys
import time
import optparse
+import logging
import imgcreate
+import imgcreate.debug
class Usage(Exception):
def __init__(self, msg = None, no_error = False):
@@ -53,6 +55,9 @@ def parse_options(args):
help="Cache directory to use (default: private cache")
parser.add_option_group(sysopt)
+ # options relating to logging
+ imgcreate.debug.add_logging_options(parser)
+
# debug options not recommended for "production" images
# Start a shell in the chroot for post-configuration.
parser.add_option("-l", "--shell", action="store_true", dest="give_shell",
@@ -63,6 +68,7 @@ def parse_options(args):
parser.add_option("", "--skip-minimize", action="store_true", dest="skip_minimize",
help=optparse.SUPPRESS_HELP)
+
(options, args) = parser.parse_args()
if not options.kscfg or not os.path.isfile(options.kscfg):
raise Usage("Kickstart config '%s' does not exist" %(options.kscfg,))
@@ -87,6 +93,10 @@ def main():
print >> out, msg
return ret
+ imgcreate.debug.setup_logging(options.verbose,
+ options.debug,
+ options.logfile)
+
if os.geteuid () != 0:
print >> sys.stderr, "You must run livecd-creator as root"
return 1
@@ -101,7 +111,7 @@ def main():
"livecd-",
maxlen = imgcreate.FSLABEL_MAXLEN)
- print "Using label '%s' and name '%s'" % (fs_label, name)
+ logging.info("Using label '%s' and name '%s'" % (fs_label, name))
ks = imgcreate.read_kickstart(options.kscfg)
@@ -121,7 +131,7 @@ def main():
creator.unmount()
creator.package()
except imgcreate.CreatorError, e:
- print >> sys.stderr, "Error creating Live CD : %s" % e
+ logging.error("Error creating Live CD : %s" % e)
return 1
finally:
creator.cleanup()
--
1.5.4.3