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 ( |
| 18 | "android/soong/ui/logger" |
| 19 | "compress/gzip" |
| 20 | "fmt" |
| 21 | "io" |
| 22 | "strings" |
| 23 | ) |
| 24 | |
| 25 | type verboseLog struct { |
| 26 | w io.WriteCloser |
| 27 | } |
| 28 | |
| 29 | func NewVerboseLog(log logger.Logger, filename string) StatusOutput { |
| 30 | if !strings.HasSuffix(filename, ".gz") { |
| 31 | filename += ".gz" |
| 32 | } |
| 33 | |
| 34 | f, err := logger.CreateFileWithRotation(filename, 5) |
| 35 | if err != nil { |
| 36 | log.Println("Failed to create verbose log file:", err) |
| 37 | return nil |
| 38 | } |
| 39 | |
| 40 | w := gzip.NewWriter(f) |
| 41 | |
| 42 | return &verboseLog{ |
| 43 | w: w, |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func (v *verboseLog) StartAction(action *Action, counts Counts) {} |
| 48 | |
| 49 | func (v *verboseLog) FinishAction(result ActionResult, counts Counts) { |
| 50 | cmd := result.Command |
| 51 | if cmd == "" { |
| 52 | cmd = result.Description |
| 53 | } |
| 54 | |
| 55 | fmt.Fprintf(v.w, "[%d/%d] %s\n", counts.FinishedActions, counts.TotalActions, cmd) |
| 56 | |
| 57 | if result.Error != nil { |
| 58 | fmt.Fprintf(v.w, "FAILED: %s\n", strings.Join(result.Outputs, " ")) |
| 59 | } |
| 60 | |
| 61 | if result.Output != "" { |
| 62 | fmt.Fprintln(v.w, result.Output) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | func (v *verboseLog) Flush() { |
| 67 | v.w.Close() |
| 68 | } |
| 69 | |
| 70 | func (v *verboseLog) Message(level MsgLevel, message string) { |
| 71 | fmt.Fprintf(v.w, "%s%s\n", level.Prefix(), message) |
| 72 | } |
| 73 | |
Colin Cross | e0df1a3 | 2019-06-09 19:40:08 -0700 | [diff] [blame] | 74 | func (v *verboseLog) Write(p []byte) (int, error) { |
| 75 | fmt.Fprint(v.w, string(p)) |
| 76 | return len(p), nil |
| 77 | } |
| 78 | |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 79 | type errorLog struct { |
| 80 | w io.WriteCloser |
| 81 | |
| 82 | empty bool |
| 83 | } |
| 84 | |
| 85 | func NewErrorLog(log logger.Logger, filename string) StatusOutput { |
| 86 | f, err := logger.CreateFileWithRotation(filename, 5) |
| 87 | if err != nil { |
| 88 | log.Println("Failed to create error log file:", err) |
| 89 | return nil |
| 90 | } |
| 91 | |
| 92 | return &errorLog{ |
| 93 | w: f, |
| 94 | empty: true, |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | func (e *errorLog) StartAction(action *Action, counts Counts) {} |
| 99 | |
| 100 | func (e *errorLog) FinishAction(result ActionResult, counts Counts) { |
| 101 | if result.Error == nil { |
| 102 | return |
| 103 | } |
| 104 | |
| 105 | cmd := result.Command |
| 106 | if cmd == "" { |
| 107 | cmd = result.Description |
| 108 | } |
| 109 | |
| 110 | if !e.empty { |
| 111 | fmt.Fprintf(e.w, "\n\n") |
| 112 | } |
| 113 | e.empty = false |
| 114 | |
| 115 | fmt.Fprintf(e.w, "FAILED: %s\n", result.Description) |
| 116 | if len(result.Outputs) > 0 { |
| 117 | fmt.Fprintf(e.w, "Outputs: %s\n", strings.Join(result.Outputs, " ")) |
| 118 | } |
| 119 | fmt.Fprintf(e.w, "Error: %s\n", result.Error) |
| 120 | if result.Command != "" { |
| 121 | fmt.Fprintf(e.w, "Command: %s\n", result.Command) |
| 122 | } |
| 123 | fmt.Fprintf(e.w, "Output:\n%s\n", result.Output) |
| 124 | } |
| 125 | |
| 126 | func (e *errorLog) Flush() { |
| 127 | e.w.Close() |
| 128 | } |
| 129 | |
| 130 | func (e *errorLog) Message(level MsgLevel, message string) { |
| 131 | if level < ErrorLvl { |
| 132 | return |
| 133 | } |
| 134 | |
| 135 | if !e.empty { |
| 136 | fmt.Fprintf(e.w, "\n\n") |
| 137 | } |
| 138 | e.empty = false |
| 139 | |
| 140 | fmt.Fprintf(e.w, "error: %s\n", message) |
| 141 | } |
Colin Cross | e0df1a3 | 2019-06-09 19:40:08 -0700 | [diff] [blame] | 142 | |
| 143 | func (e *errorLog) Write(p []byte) (int, error) { |
| 144 | fmt.Fprint(e.w, string(p)) |
| 145 | return len(p), nil |
| 146 | } |