Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 1 | // Copyright 2018 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package status |
| 16 | |
| 17 | import ( |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 18 | "compress/gzip" |
Patrice Arruda | 297ceba | 2019-06-06 16:44:37 -0700 | [diff] [blame] | 19 | "errors" |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 20 | "fmt" |
| 21 | "io" |
Patrice Arruda | 297ceba | 2019-06-06 16:44:37 -0700 | [diff] [blame] | 22 | "io/ioutil" |
Patrice Arruda | 36b5e31 | 2020-03-11 12:41:47 -0700 | [diff] [blame^] | 23 | "os" |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 24 | "strings" |
Patrice Arruda | 297ceba | 2019-06-06 16:44:37 -0700 | [diff] [blame] | 25 | |
| 26 | "github.com/golang/protobuf/proto" |
| 27 | |
| 28 | "android/soong/ui/logger" |
| 29 | "android/soong/ui/status/build_error_proto" |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | type verboseLog struct { |
| 33 | w io.WriteCloser |
| 34 | } |
| 35 | |
| 36 | func NewVerboseLog(log logger.Logger, filename string) StatusOutput { |
| 37 | if !strings.HasSuffix(filename, ".gz") { |
| 38 | filename += ".gz" |
| 39 | } |
| 40 | |
| 41 | f, err := logger.CreateFileWithRotation(filename, 5) |
| 42 | if err != nil { |
| 43 | log.Println("Failed to create verbose log file:", err) |
| 44 | return nil |
| 45 | } |
| 46 | |
| 47 | w := gzip.NewWriter(f) |
| 48 | |
| 49 | return &verboseLog{ |
| 50 | w: w, |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func (v *verboseLog) StartAction(action *Action, counts Counts) {} |
| 55 | |
| 56 | func (v *verboseLog) FinishAction(result ActionResult, counts Counts) { |
| 57 | cmd := result.Command |
| 58 | if cmd == "" { |
| 59 | cmd = result.Description |
| 60 | } |
| 61 | |
| 62 | fmt.Fprintf(v.w, "[%d/%d] %s\n", counts.FinishedActions, counts.TotalActions, cmd) |
| 63 | |
| 64 | if result.Error != nil { |
| 65 | fmt.Fprintf(v.w, "FAILED: %s\n", strings.Join(result.Outputs, " ")) |
| 66 | } |
| 67 | |
| 68 | if result.Output != "" { |
| 69 | fmt.Fprintln(v.w, result.Output) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func (v *verboseLog) Flush() { |
| 74 | v.w.Close() |
| 75 | } |
| 76 | |
| 77 | func (v *verboseLog) Message(level MsgLevel, message string) { |
| 78 | fmt.Fprintf(v.w, "%s%s\n", level.Prefix(), message) |
| 79 | } |
| 80 | |
Colin Cross | e0df1a3 | 2019-06-09 19:40:08 -0700 | [diff] [blame] | 81 | func (v *verboseLog) Write(p []byte) (int, error) { |
| 82 | fmt.Fprint(v.w, string(p)) |
| 83 | return len(p), nil |
| 84 | } |
| 85 | |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 86 | type errorLog struct { |
Patrice Arruda | 297ceba | 2019-06-06 16:44:37 -0700 | [diff] [blame] | 87 | w io.WriteCloser |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 88 | empty bool |
| 89 | } |
| 90 | |
| 91 | func NewErrorLog(log logger.Logger, filename string) StatusOutput { |
| 92 | f, err := logger.CreateFileWithRotation(filename, 5) |
| 93 | if err != nil { |
| 94 | log.Println("Failed to create error log file:", err) |
| 95 | return nil |
| 96 | } |
| 97 | |
| 98 | return &errorLog{ |
| 99 | w: f, |
| 100 | empty: true, |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | func (e *errorLog) StartAction(action *Action, counts Counts) {} |
| 105 | |
| 106 | func (e *errorLog) FinishAction(result ActionResult, counts Counts) { |
| 107 | if result.Error == nil { |
| 108 | return |
| 109 | } |
| 110 | |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 111 | if !e.empty { |
| 112 | fmt.Fprintf(e.w, "\n\n") |
| 113 | } |
| 114 | e.empty = false |
| 115 | |
| 116 | fmt.Fprintf(e.w, "FAILED: %s\n", result.Description) |
Patrice Arruda | 297ceba | 2019-06-06 16:44:37 -0700 | [diff] [blame] | 117 | |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 118 | if len(result.Outputs) > 0 { |
| 119 | fmt.Fprintf(e.w, "Outputs: %s\n", strings.Join(result.Outputs, " ")) |
| 120 | } |
Patrice Arruda | 297ceba | 2019-06-06 16:44:37 -0700 | [diff] [blame] | 121 | |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 122 | fmt.Fprintf(e.w, "Error: %s\n", result.Error) |
| 123 | if result.Command != "" { |
| 124 | fmt.Fprintf(e.w, "Command: %s\n", result.Command) |
| 125 | } |
| 126 | fmt.Fprintf(e.w, "Output:\n%s\n", result.Output) |
| 127 | } |
| 128 | |
| 129 | func (e *errorLog) Flush() { |
| 130 | e.w.Close() |
| 131 | } |
| 132 | |
| 133 | func (e *errorLog) Message(level MsgLevel, message string) { |
| 134 | if level < ErrorLvl { |
| 135 | return |
| 136 | } |
| 137 | |
| 138 | if !e.empty { |
| 139 | fmt.Fprintf(e.w, "\n\n") |
| 140 | } |
| 141 | e.empty = false |
| 142 | |
| 143 | fmt.Fprintf(e.w, "error: %s\n", message) |
| 144 | } |
Colin Cross | e0df1a3 | 2019-06-09 19:40:08 -0700 | [diff] [blame] | 145 | |
| 146 | func (e *errorLog) Write(p []byte) (int, error) { |
| 147 | fmt.Fprint(e.w, string(p)) |
| 148 | return len(p), nil |
| 149 | } |
Patrice Arruda | 297ceba | 2019-06-06 16:44:37 -0700 | [diff] [blame] | 150 | |
| 151 | type errorProtoLog struct { |
| 152 | errorProto soong_build_error_proto.BuildError |
| 153 | filename string |
| 154 | log logger.Logger |
| 155 | } |
| 156 | |
| 157 | func NewProtoErrorLog(log logger.Logger, filename string) StatusOutput { |
Patrice Arruda | 36b5e31 | 2020-03-11 12:41:47 -0700 | [diff] [blame^] | 158 | os.Remove(filename) |
Patrice Arruda | 297ceba | 2019-06-06 16:44:37 -0700 | [diff] [blame] | 159 | return &errorProtoLog{ |
| 160 | errorProto: soong_build_error_proto.BuildError{}, |
| 161 | filename: filename, |
| 162 | log: log, |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | func (e *errorProtoLog) StartAction(action *Action, counts Counts) {} |
| 167 | |
| 168 | func (e *errorProtoLog) FinishAction(result ActionResult, counts Counts) { |
| 169 | if result.Error == nil { |
| 170 | return |
| 171 | } |
| 172 | |
| 173 | e.errorProto.ActionErrors = append(e.errorProto.ActionErrors, &soong_build_error_proto.BuildActionError{ |
| 174 | Description: proto.String(result.Description), |
| 175 | Command: proto.String(result.Command), |
| 176 | Output: proto.String(result.Output), |
| 177 | Artifacts: result.Outputs, |
| 178 | Error: proto.String(result.Error.Error()), |
| 179 | }) |
| 180 | } |
| 181 | |
| 182 | func (e *errorProtoLog) Flush() { |
Patrice Arruda | 36b5e31 | 2020-03-11 12:41:47 -0700 | [diff] [blame^] | 183 | // Don't create the build error proto file if there is action errors. |
| 184 | if len(e.errorProto.ActionErrors) == 0 { |
| 185 | return |
| 186 | } |
| 187 | |
Patrice Arruda | 297ceba | 2019-06-06 16:44:37 -0700 | [diff] [blame] | 188 | data, err := proto.Marshal(&e.errorProto) |
| 189 | if err != nil { |
Colin Cross | ff27ce4 | 2019-11-26 16:20:03 -0800 | [diff] [blame] | 190 | e.log.Printf("Failed to marshal build status proto: %v\n", err) |
Patrice Arruda | 297ceba | 2019-06-06 16:44:37 -0700 | [diff] [blame] | 191 | return |
| 192 | } |
Patrice Arruda | 36b5e31 | 2020-03-11 12:41:47 -0700 | [diff] [blame^] | 193 | |
Patrice Arruda | 297ceba | 2019-06-06 16:44:37 -0700 | [diff] [blame] | 194 | err = ioutil.WriteFile(e.filename, []byte(data), 0644) |
| 195 | if err != nil { |
Colin Cross | ff27ce4 | 2019-11-26 16:20:03 -0800 | [diff] [blame] | 196 | e.log.Printf("Failed to write file %s: %v\n", e.filename, err) |
Patrice Arruda | 297ceba | 2019-06-06 16:44:37 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
| 200 | func (e *errorProtoLog) Message(level MsgLevel, message string) { |
| 201 | if level > ErrorLvl { |
| 202 | e.errorProto.ErrorMessages = append(e.errorProto.ErrorMessages, message) |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | func (e *errorProtoLog) Write(p []byte) (int, error) { |
| 207 | return 0, errors.New("not supported") |
| 208 | } |