Generate the build_error protobuf file on first build action error.

buld_error protobuf file is created the moment a build action failed.
This is for external system to detect immediately what caused the
build failure while monitoring the build_progress.pb file.

Bug: b/150401146
Test: * m nothing
      * Built successfully aosp_arm-eng and checked build_error file
        was not created.
      * Modified a cpp file to cause a build failed. Ran m and build
        failed. Checked immediately during the build that the
	build_error file was created.
      * m clean; m and during build, checked if build_error was
        removed.
Change-Id: I778616ae8cf242b49ad263b79c93321959112caa
diff --git a/ui/status/log.go b/ui/status/log.go
index db5c351..4a08acb 100644
--- a/ui/status/log.go
+++ b/ui/status/log.go
@@ -178,26 +178,17 @@
 		Artifacts:   result.Outputs,
 		Error:       proto.String(result.Error.Error()),
 	})
-}
 
-func (e *errorProtoLog) Flush() {
-	// Don't create the build error proto file if there is action errors.
-	if len(e.errorProto.ActionErrors) == 0 {
-		return
-	}
-
-	data, err := proto.Marshal(&e.errorProto)
-	if err != nil {
-		e.log.Printf("Failed to marshal build status proto: %v\n", err)
-		return
-	}
-
-	err = ioutil.WriteFile(e.filename, []byte(data), 0644)
+	err := writeToFile(&e.errorProto, e.filename)
 	if err != nil {
 		e.log.Printf("Failed to write file %s: %v\n", e.filename, err)
 	}
 }
 
+func (e *errorProtoLog) Flush() {
+	//Not required.
+}
+
 func (e *errorProtoLog) Message(level MsgLevel, message string) {
 	if level > ErrorLvl {
 		e.errorProto.ErrorMessages = append(e.errorProto.ErrorMessages, message)