wic: setup logging in the main wic module

Set up wic logger using standerd logging module.
This is going to replace custom msger module.

(From OE-Core rev: f7d9e33aa129d8ab98dd1971154c29c275d103b0)

Signed-off-by: Ed Bartosh <ed.bartosh@linux.intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
This commit is contained in:
Ed Bartosh 2017-02-14 18:48:54 +02:00 committed by Richard Purdie
parent e0839dd113
commit 58ff06f1e7
1 changed files with 44 additions and 33 deletions

View File

@ -58,6 +58,23 @@ from wic.utils.errors import WicError
from wic import engine from wic import engine
from wic import help as hlp from wic import help as hlp
def wic_logger():
"""Create and convfigure wic logger."""
logger = logging.getLogger('wic')
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
formatter = logging.Formatter('%(levelname)s: %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
return logger
logger = wic_logger()
def rootfs_dir_to_args(krootfs_dir): def rootfs_dir_to_args(krootfs_dir):
""" """
Get a rootfs_dir dict and serialize to string Get a rootfs_dir dict and serialize to string
@ -125,12 +142,12 @@ def wic_create_subcommand(args, usage_str):
(options, args) = parser.parse_args(args) (options, args) = parser.parse_args(args)
if len(args) != 1: if len(args) != 1:
logging.error("Wrong number of arguments, exiting\n") logger.error("Wrong number of arguments, exiting\n")
parser.print_help() parser.print_help()
sys.exit(1) sys.exit(1)
if options.build_rootfs and not bitbake_main: if options.build_rootfs and not bitbake_main:
logging.error("Can't build roofs as bitbake is not in the $PATH") logger.error("Can't build roofs as bitbake is not in the $PATH")
sys.exit(1) sys.exit(1)
if not options.image_name: if not options.image_name:
@ -142,8 +159,8 @@ def wic_create_subcommand(args, usage_str):
if not val: if not val:
missed.append(opt) missed.append(opt)
if missed: if missed:
print("The following build artifacts are not specified:") logger.error("The following build artifacts are not specified: %s",
print(" " + ", ".join(missed)) ", ".join(missed))
sys.exit(1) sys.exit(1)
if options.image_name: if options.image_name:
@ -154,23 +171,22 @@ def wic_create_subcommand(args, usage_str):
if options.vars_dir: if options.vars_dir:
BB_VARS.vars_dir = options.vars_dir BB_VARS.vars_dir = options.vars_dir
if options.build_check: if options.build_check and not engine.verify_build_env():
print("Checking basic build environment...") logger.error("Couldn't verify build environment, exiting\n")
if not engine.verify_build_env(): sys.exit(1)
print("Couldn't verify build environment, exiting\n")
sys.exit(1)
else:
print("Done.\n")
bootimg_dir = "" bootimg_dir = ""
if options.debug:
logger.setLevel(logging.DEBUG)
if options.image_name: if options.image_name:
if options.build_rootfs: if options.build_rootfs:
argv = ["bitbake", options.image_name] argv = ["bitbake", options.image_name]
if options.debug: if options.debug:
argv.append("--debug") argv.append("--debug")
print("Building rootfs...\n") logger.info("Building rootfs...\n")
if bitbake_main(BitBakeConfigParameters(argv), if bitbake_main(BitBakeConfigParameters(argv),
cookerdata.CookerConfiguration()): cookerdata.CookerConfiguration()):
sys.exit(1) sys.exit(1)
@ -181,18 +197,18 @@ def wic_create_subcommand(args, usage_str):
"wic-tools", cache=False) "wic-tools", cache=False)
else: else:
if options.build_rootfs: if options.build_rootfs:
print("Image name is not specified, exiting. (Use -e/--image-name to specify it)\n") logger.error("Image name is not specified, exiting. (Use -e/--image-name to specify it)\n")
sys.exit(1) sys.exit(1)
native_sysroot = options.native_sysroot native_sysroot = options.native_sysroot
if not native_sysroot or not os.path.isdir(native_sysroot): if not native_sysroot or not os.path.isdir(native_sysroot):
print("Building wic-tools...\n") logger.info("Building wic-tools...\n")
if bitbake_main(BitBakeConfigParameters("bitbake wic-tools".split()), if bitbake_main(BitBakeConfigParameters("bitbake wic-tools".split()),
cookerdata.CookerConfiguration()): cookerdata.CookerConfiguration()):
sys.exit(1) sys.exit(1)
native_sysroot = get_bitbake_var("RECIPE_SYSROOT_NATIVE", "wic-tools") native_sysroot = get_bitbake_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
if not native_sysroot: if not native_sysroot:
print("Unable to find the location of the native tools sysroot to use\n") logger.info("Unable to find the location of the native tools sysroot to use\n")
sys.exit(1) sys.exit(1)
wks_file = args[0] wks_file = args[0]
@ -200,7 +216,7 @@ def wic_create_subcommand(args, usage_str):
if not wks_file.endswith(".wks"): if not wks_file.endswith(".wks"):
wks_file = engine.find_canned_image(scripts_path, wks_file) wks_file = engine.find_canned_image(scripts_path, wks_file)
if not wks_file: if not wks_file:
print("No image named %s found, exiting. (Use 'wic list images' "\ logger.error("No image named %s found, exiting. (Use 'wic list images' "\
"to list available images, or specify a fully-qualified OE "\ "to list available images, or specify a fully-qualified OE "\
"kickstart (.wks) filename)\n" % args[0]) "kickstart (.wks) filename)\n" % args[0])
sys.exit(1) sys.exit(1)
@ -213,16 +229,16 @@ def wic_create_subcommand(args, usage_str):
kernel_dir = options.kernel_dir kernel_dir = options.kernel_dir
native_sysroot = options.native_sysroot native_sysroot = options.native_sysroot
if rootfs_dir and not os.path.isdir(rootfs_dir): if rootfs_dir and not os.path.isdir(rootfs_dir):
print("--roofs-dir (-r) not found, exiting\n") logger.error("--roofs-dir (-r) not found, exiting\n")
sys.exit(1) sys.exit(1)
if not os.path.isdir(bootimg_dir): if not os.path.isdir(bootimg_dir):
print("--bootimg-dir (-b) not found, exiting\n") logger.error("--bootimg-dir (-b) not found, exiting\n")
sys.exit(1) sys.exit(1)
if not os.path.isdir(kernel_dir): if not os.path.isdir(kernel_dir):
print("--kernel-dir (-k) not found, exiting\n") logger.error("--kernel-dir (-k) not found, exiting\n")
sys.exit(1) sys.exit(1)
if not os.path.isdir(native_sysroot): if not os.path.isdir(native_sysroot):
print("--native-sysroot (-n) not found, exiting\n") logger.error("--native-sysroot (-n) not found, exiting\n")
sys.exit(1) sys.exit(1)
else: else:
not_found = not_found_dir = "" not_found = not_found_dir = ""
@ -235,12 +251,11 @@ def wic_create_subcommand(args, usage_str):
if not_found: if not_found:
if not not_found_dir: if not not_found_dir:
not_found_dir = "Completely missing artifact - wrong image (.wks) used?" not_found_dir = "Completely missing artifact - wrong image (.wks) used?"
print("Build artifacts not found, exiting.") logger.error("Build artifacts not found, exiting.")
print(" (Please check that the build artifacts for the machine") logger.info(" (Please check that the build artifacts for the machine")
print(" selected in local.conf actually exist and that they") logger.info(" selected in local.conf actually exist and that they")
print(" are the correct artifacts for the image (.wks file)).\n") logger.info(" are the correct artifacts for the image (.wks file)).\n")
print("The artifact that couldn't be found was %s:\n %s" % \ logger.info("The artifact that couldn't be found was %s:\n %s", not_found, not_found_dir)
(not_found, not_found_dir))
sys.exit(1) sys.exit(1)
krootfs_dir = options.rootfs_dir krootfs_dir = options.rootfs_dir
@ -250,7 +265,7 @@ def wic_create_subcommand(args, usage_str):
rootfs_dir = rootfs_dir_to_args(krootfs_dir) rootfs_dir = rootfs_dir_to_args(krootfs_dir)
print("Creating image(s)...\n") logger.info("Creating image(s)...\n")
engine.wic_create(wks_file, rootfs_dir, bootimg_dir, kernel_dir, engine.wic_create(wks_file, rootfs_dir, bootimg_dir, kernel_dir,
native_sysroot, options) native_sysroot, options)
@ -264,7 +279,7 @@ def wic_list_subcommand(args, usage_str):
args = parser.parse_args(args)[1] args = parser.parse_args(args)[1]
if not engine.wic_list(args, scripts_path): if not engine.wic_list(args, scripts_path):
logging.error("Bad list arguments, exiting\n") logger.error("Bad list arguments, exiting\n")
parser.print_help() parser.print_help()
sys.exit(1) sys.exit(1)
@ -301,10 +316,6 @@ subcommands = {
} }
def start_logging(loglevel):
logging.basicConfig(filename='wic.log', filemode='w', level=loglevel)
def main(argv): def main(argv):
parser = optparse.OptionParser(version="wic version %s" % __version__, parser = optparse.OptionParser(version="wic version %s" % __version__,
usage=hlp.wic_usage) usage=hlp.wic_usage)
@ -326,6 +337,6 @@ if __name__ == "__main__":
try: try:
sys.exit(main(sys.argv[1:])) sys.exit(main(sys.argv[1:]))
except WicError as err: except WicError as err:
print("ERROR:", err, file=sys.stderr) logger.error(err)
sys.exit(1) sys.exit(1)