releasetools: Switch to Python logging.

Converts the following files to Python logging.

add_img_to_target_files.py
blockimgdiff.py
build_image.py
check_ota_package_signature.py
common.py
img_from_target_files.py
make_recovery_patch.py
ota_from_target_files.py
sparse_img.py
verity_utils.py

This separates logging outputs from normal outputs, and allows easier
switching between different verbosity levels. It also supports adjusting
logging config via environment variable (LOGGING_CONFIG).

Test: `m dist`
Test: `python -m unittest discover build/make/tools/releasetools`
Change-Id: Idfc3f7aff83e03f41f3c9b5067d64fd595d5192d
diff --git a/tools/releasetools/build_image.py b/tools/releasetools/build_image.py
index 43c91da..4a013c2 100755
--- a/tools/releasetools/build_image.py
+++ b/tools/releasetools/build_image.py
@@ -26,6 +26,7 @@
 
 from __future__ import print_function
 
+import logging
 import os
 import os.path
 import re
@@ -35,6 +36,8 @@
 import common
 import verity_utils
 
+logger = logging.getLogger(__name__)
+
 OPTIONS = common.OPTIONS
 BLOCK_SIZE = common.BLOCK_SIZE
 BYTES_IN_MB = 1024 * 1024
@@ -228,8 +231,8 @@
       "partition_size" not in prop_dict):
     # If partition_size is not defined, use output of `du' + reserved_size.
     size = GetDiskUsage(in_dir)
-    if OPTIONS.verbose:
-      print("The tree size of %s is %d MB." % (in_dir, size // BYTES_IN_MB))
+    logger.info(
+        "The tree size of %s is %d MB.", in_dir, size // BYTES_IN_MB)
     size += int(prop_dict.get("partition_reserved_size", 0))
     # Round this up to a multiple of 4K so that avbtool works
     size = common.RoundUpTo4K(size)
@@ -241,8 +244,8 @@
           lambda x: verity_utils.AVBCalcMaxImageSize(
               avbtool, avb_footer_type, x, avb_signing_args))
     prop_dict["partition_size"] = str(size)
-    if OPTIONS.verbose:
-      print("Allocating %d MB for %s." % (size // BYTES_IN_MB, out_file))
+    logger.info(
+        "Allocating %d MB for %s.", size // BYTES_IN_MB, out_file)
 
   prop_dict["image_size"] = prop_dict["partition_size"]
 
@@ -350,8 +353,8 @@
       du_str = "{} bytes ({} MB)".format(du, du // BYTES_IN_MB)
     # Suppress any errors from GetDiskUsage() to avoid hiding the real errors
     # from common.RunAndCheckOutput().
-    except Exception as e:  # pylint: disable=broad-except
-      print(e, file=sys.stderr)
+    except Exception:  # pylint: disable=broad-except
+      logger.exception("Failed to compute disk usage with du")
       du_str = "unknown"
     print(
         "Out of space? The tree size of {} is {}, with reserved space of {} "
@@ -664,6 +667,8 @@
     print(__doc__)
     sys.exit(1)
 
+  common.InitLogging()
+
   in_dir = argv[0]
   glob_dict_file = argv[1]
   out_file = argv[2]
@@ -697,7 +702,7 @@
     elif image_filename == "product_services.img":
       mount_point = "product_services"
     else:
-      print("error: unknown image file name ", image_filename, file=sys.stderr)
+      logger.error("Unknown image file name %s", image_filename)
       sys.exit(1)
 
     image_properties = ImagePropFromGlobalDict(glob_dict, mount_point)
@@ -705,14 +710,14 @@
   try:
     BuildImage(in_dir, image_properties, out_file, target_out)
   except:
-    print("Error: Failed to build {} from {}".format(out_file, in_dir),
-          file=sys.stderr)
+    logger.error("Failed to build %s from %s", out_file, in_dir)
     raise
 
   if prop_file_out:
     glob_dict_out = GlobalDictFromImageProp(image_properties, mount_point)
     SaveGlobalDict(prop_file_out, glob_dict_out)
 
+
 if __name__ == '__main__':
   try:
     main(sys.argv[1:])