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 15package status 16 17import ( 18 "android/soong/ui/logger" 19 "compress/gzip" 20 "fmt" 21 "io" 22 "strings" 23) 24 25type verboseLog struct { 26 w io.WriteCloser 27} 28 29func 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 47func (v *verboseLog) StartAction(action *Action, counts Counts) {} 48 49func (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 66func (v *verboseLog) Flush() { 67 v.w.Close() 68} 69 70func (v *verboseLog) Message(level MsgLevel, message string) { 71 fmt.Fprintf(v.w, "%s%s\n", level.Prefix(), message) 72} 73 74type errorLog struct { 75 w io.WriteCloser 76 77 empty bool 78} 79 80func NewErrorLog(log logger.Logger, filename string) StatusOutput { 81 f, err := logger.CreateFileWithRotation(filename, 5) 82 if err != nil { 83 log.Println("Failed to create error log file:", err) 84 return nil 85 } 86 87 return &errorLog{ 88 w: f, 89 empty: true, 90 } 91} 92 93func (e *errorLog) StartAction(action *Action, counts Counts) {} 94 95func (e *errorLog) FinishAction(result ActionResult, counts Counts) { 96 if result.Error == nil { 97 return 98 } 99 100 cmd := result.Command 101 if cmd == "" { 102 cmd = result.Description 103 } 104 105 if !e.empty { 106 fmt.Fprintf(e.w, "\n\n") 107 } 108 e.empty = false 109 110 fmt.Fprintf(e.w, "FAILED: %s\n", result.Description) 111 if len(result.Outputs) > 0 { 112 fmt.Fprintf(e.w, "Outputs: %s\n", strings.Join(result.Outputs, " ")) 113 } 114 fmt.Fprintf(e.w, "Error: %s\n", result.Error) 115 if result.Command != "" { 116 fmt.Fprintf(e.w, "Command: %s\n", result.Command) 117 } 118 fmt.Fprintf(e.w, "Output:\n%s\n", result.Output) 119} 120 121func (e *errorLog) Flush() { 122 e.w.Close() 123} 124 125func (e *errorLog) Message(level MsgLevel, message string) { 126 if level < ErrorLvl { 127 return 128 } 129 130 if !e.empty { 131 fmt.Fprintf(e.w, "\n\n") 132 } 133 e.empty = false 134 135 fmt.Fprintf(e.w, "error: %s\n", message) 136} 137