sanitycheck: add --log-file to log everything to a file too

Currently CI plays tricks with `tee` to capture the output and post
process it. This makes it more complex to handle and easier for it to
fail in ways it should not.

So add a simple log capture option sanity check that mirrors the
stdout output, removing color encoding for errors.

Change-Id: I6de0b6cfe4da9c289f537979545dddbcd49cf834
Signed-off-by: Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
diff --git a/scripts/sanitycheck b/scripts/sanitycheck
index 5c9cd5a..c31620d 100755
--- a/scripts/sanitycheck
+++ b/scripts/sanitycheck
@@ -226,12 +226,20 @@
 class ExecutionError(MakeError):
     pass
 
+log_file = None
+
 # Debug Functions
 def info(what):
     sys.stdout.write(what + "\n")
+    if log_file:
+        log_file.write(what + "\n")
+        log_file.flush()
 
 def error(what):
     sys.stderr.write(COLOR_RED + what + COLOR_NORMAL + "\n")
+    if log_file:
+        log_file(what + "\n")
+        log_file.flush()
 
 def debug(what):
     if VERBOSE >= 1:
@@ -1174,7 +1182,10 @@
          (COLOR_RED, goal.name, COLOR_NORMAL));
     if INLINE_LOGS:
         with open(goal.get_error_log()) as fp:
-            sys.stdout.write(fp.read())
+            data = fp.read()
+            sys.stdout.write(data)
+            if log_file:
+                log_file.write(data)
     else:
         info("\tsee: " + COLOR_YELLOW + goal.get_error_log() + COLOR_NORMAL)
 
@@ -1641,6 +1652,8 @@
     parser.add_argument("-i", "--inline-logs", action="store_true",
             help="Upon test failure, print relevant log data to stdout "
                  "instead of just a path to it")
+    parser.add_argument("--log-file", metavar="FILENAME", action="store",
+            help="log also to file")
     parser.add_argument("-m", "--last-metrics", action="store_true",
             help="Instead of comparing metrics from the last --release, "
                  "compare with the results of the previous sanity check "
@@ -1701,7 +1714,10 @@
     if INLINE_LOGS:
         info("{:-^100}".format(filename))
         with open(filename) as fp:
-            sys.stdout.write(fp.read())
+            data = fp.read()
+            sys.stdout.write(data)
+            if log_file:
+                log_file.write(data)
         info("{:-^100}".format(filename))
     else:
         info("\tsee: " + COLOR_YELLOW + filename + COLOR_NORMAL)
@@ -1784,7 +1800,7 @@
 
 def main():
     start_time = time.time()
-    global VERBOSE, INLINE_LOGS, CPU_COUNTS
+    global VERBOSE, INLINE_LOGS, CPU_COUNTS, log_file
     args = parse_arguments()
     toolchain = os.environ.get("ZEPHYR_GCC_VARIANT", None)
 
@@ -1795,6 +1811,8 @@
 
     VERBOSE += args.verbose
     INLINE_LOGS = args.inline_logs
+    if args.log_file:
+        log_file = open(args.log_file, "w")
     if args.jobs:
         CPU_COUNTS = args.jobs
 
@@ -1891,7 +1909,8 @@
         ts.testcase_report(LAST_SANITY)
     if args.release:
         ts.testcase_report(RELEASE_DATA)
-
+    if log_file:
+        log_file.close()
     if failed or (warnings and args.warnings_as_errors):
         sys.exit(1)