Add clearcut metrics tool to log start event

- Import asuite_cc_client lib to log start and exit event.
- By default silence root logger's stream handler since 3p libs
  (asuite_cc_client) may initial root logger no matter what level
  we're using.
- Remove workaround for oauth2client unexpceted warning message.

Bug: 131658799
Bug: 112803893
Test: atest acloud_test --host &
      m acloud & any acloud-dev commands & check the clearcut dashboard

Change-Id: I84ade2863d2f29b1122729f5109f665b6c4e4ac4
diff --git a/public/acloud_main.py b/public/acloud_main.py
index 25a23e0..55b5735 100644
--- a/public/acloud_main.py
+++ b/public/acloud_main.py
@@ -51,6 +51,7 @@
 import logging
 import platform
 import sys
+import traceback
 
 # TODO: Remove this once we switch over to embedded launcher.
 # Exit out if python version is < 2.7.13 due to b/120883119.
@@ -72,13 +73,14 @@
         print("  POSIXLY_CORRECT=1 port -N install python27")
     sys.exit(1)
 
-# Needed to silence oauth2client.
-# This is a workaround to get rid of below warning message:
+# By Default silence root logger's stream handler since 3p lib may initial
+# root logger no matter what level we're using. The acloud logger behavior will
+# be defined in _SetupLogging(). This also could workaround to get rid of below
+# oauth2client warning:
 # 'No handlers could be found for logger "oauth2client.contrib.multistore_file'
-# TODO(b/112803893): Remove this code once bug is fixed.
-OAUTH2_LOGGER = logging.getLogger('oauth2client.contrib.multistore_file')
-OAUTH2_LOGGER.setLevel(logging.CRITICAL)
-OAUTH2_LOGGER.addHandler(logging.FileHandler("/dev/null"))
+DEFAULT_STREAM_HANDLER = logging.StreamHandler()
+DEFAULT_STREAM_HANDLER.setLevel(logging.CRITICAL)
+logging.getLogger().addHandler(DEFAULT_STREAM_HANDLER)
 
 # pylint: disable=wrong-import-position
 from acloud import errors
@@ -86,6 +88,7 @@
 from acloud.create import create_args
 from acloud.delete import delete
 from acloud.delete import delete_args
+from acloud.internal import constants
 from acloud.reconnect import reconnect
 from acloud.reconnect import reconnect_args
 from acloud.list import list as list_instances
@@ -341,7 +344,6 @@
     # Check access.
     # device_driver.CheckAccess(cfg)
 
-    metrics.LogUsage()
     report = None
     if args.which == create_args.CMD_CREATE:
         create.Run(args)
@@ -393,4 +395,19 @@
 
 
 if __name__ == "__main__":
-    main(sys.argv[1:])
+    EXIT_CODE = None
+    EXCEPTION_STACKTRACE = None
+    EXCEPTION_LOG = None
+    metrics.LogUsage(sys.argv[1:])
+    try:
+        EXIT_CODE = main(sys.argv[1:])
+    except Exception as e:
+        EXIT_CODE = constants.EXIT_BY_ERROR
+        EXCEPTION_STACKTRACE = traceback.format_exc()
+        EXCEPTION_LOG = str(e)
+        raise
+    finally:
+        # Log Exit event here to calculate the consuming time.
+        metrics.LogExitEvent(EXIT_CODE,
+                             stacktrace=EXCEPTION_STACKTRACE,
+                             logs=EXCEPTION_LOG)