-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This patch changes the tools so that they automatically generate a log file that contains all the details of the execution. The console will now only contain the more relevant messages. For example, the 'build' tool will only present the following messages in the console: [INFO] Log file is '/projects/ovirt-containers/build.log' [INFO] Loading project file '/projects/ovirt-containers/project.conf' [INFO] Building image 'ovirt/base:master' [INFO] Building image 'ovirt/engine:master' [INFO] Building image 'ovirt/engine-database:master' [INFO] Building image 'ovirt/engine-spice-proxy:master' [INFO] Building image 'ovirt/vdsc:master' [INFO] Building image 'ovirt/vdsc-syslog:master' [INFO] Tool finished successfully The rest of the details, including the commands executed and their output, will be available in the 'build.log' file. In addition the messages presented in the console will have a touch of color. The [INFO] prefix will be green and the [ERROR] prefix will be red. Change-Id: I85a6887693df9771cc5f3810db62d6371e34b121 Signed-off-by: Juan Hernandez <[email protected]>
- Loading branch information
Showing
9 changed files
with
226 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
/* | ||
Copyright (c) 2017 Red Hat, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
// This file contains functions useful for generating log files. | ||
|
||
package log | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// The log file. | ||
// | ||
var path string | ||
var file *os.File | ||
|
||
// The writers for the different levels. | ||
// | ||
var errorWriter io.Writer | ||
var infoWriter io.Writer | ||
var debugWriter io.Writer | ||
|
||
// prefixWriter implements a writer that adds a prefix to each line. | ||
// | ||
type prefixWriter struct { | ||
prefix string | ||
stream io.Writer | ||
start bool | ||
} | ||
|
||
// newPrefixWriter creates a new prefix writer that adds the given | ||
// prefix and writes the modified lines to the given stream. | ||
// | ||
func newPrefixWriter(stream io.Writer, prefix string) io.Writer { | ||
p := new(prefixWriter) | ||
p.prefix = prefix | ||
p.stream = stream | ||
p.start = true | ||
return p | ||
} | ||
|
||
func (p *prefixWriter) Write(data []byte) (count int, err error) { | ||
buffer := new(bytes.Buffer) | ||
buffer.Grow(len(data)) | ||
for _, char := range data { | ||
if p.start { | ||
buffer.WriteString(p.prefix) | ||
p.start = false | ||
} | ||
buffer.WriteByte(char) | ||
if char == 10 { | ||
p.start = true | ||
} | ||
} | ||
count = len(data) | ||
_, err = p.stream.Write(buffer.Bytes()) | ||
return | ||
} | ||
|
||
// newLogWriter creates a log writer that will write to the given file | ||
// and console. Each line will have the prefix added, in the given | ||
// color. The file and the console can be nil. | ||
// | ||
func newLogWriter(file, console io.Writer, prefix, color string) io.Writer { | ||
writers := make([]io.Writer, 0) | ||
if file != nil { | ||
plain := fmt.Sprintf("[%s] ", prefix) | ||
file = newPrefixWriter(file, plain) | ||
writers = append(writers, file) | ||
} | ||
if console != nil { | ||
colored := fmt.Sprintf("\033[%sm[%s]\033[m ", color, prefix) | ||
console = newPrefixWriter(console, colored) | ||
writers = append(writers, console) | ||
} | ||
return io.MultiWriter(writers...) | ||
} | ||
|
||
// Open creates a log file with the given name and the .log extension, | ||
// and configures the log to write to it. | ||
// | ||
func Open(name string) error { | ||
var err error | ||
|
||
path, err = filepath.Abs(name + ".log") | ||
if err != nil { | ||
return err | ||
} | ||
file, err = os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0664) | ||
if err != nil { | ||
return err | ||
} | ||
errorWriter = newLogWriter(file, os.Stderr, "ERROR", "0;31") | ||
infoWriter = newLogWriter(file, os.Stdout, "INFO", "0;32") | ||
debugWriter = newLogWriter(file, nil, "DEBUG", "0;34") | ||
return nil | ||
} | ||
|
||
// Close closes the log file. | ||
// | ||
func Close() error { | ||
return file.Close() | ||
} | ||
|
||
// Path the returns the path of the log file. | ||
// | ||
func Path() string { | ||
return path | ||
} | ||
|
||
// Info sends an informative message to the log file and to the standard | ||
// ouptut of the process. | ||
// | ||
func Info(format string, args ...interface{}) { | ||
write(infoWriter, format, args...) | ||
} | ||
|
||
// Debug sends a debug message to the log file. | ||
// | ||
func Debug(format string, args ...interface{}) { | ||
write(debugWriter, format, args...) | ||
} | ||
|
||
// Error sends an error message to the log file and to the standard | ||
// error stream of the process. | ||
// | ||
func Error(format string, args ...interface{}) { | ||
write(errorWriter, format, args...) | ||
} | ||
|
||
func write(writer io.Writer, format string, args ...interface{}) { | ||
message := fmt.Sprintf(format, args...) | ||
writer.Write([]byte(message)) | ||
writer.Write([]byte("\n")) | ||
} | ||
|
||
// InfoWriter returns the writer that writes informative messages to the | ||
// log file and to the standard output of the process. | ||
// | ||
func InfoWriter() io.Writer { | ||
return infoWriter | ||
} | ||
|
||
// DebugWriter returns the writer that writes debug messages to the log | ||
// file. | ||
// | ||
func DebugWriter() io.Writer { | ||
return debugWriter | ||
} | ||
|
||
// ErrorWriter returns the writer that writes error messages to the log | ||
// file and to the standard error stream of the process. | ||
// file. | ||
// | ||
func ErrorWriter() io.Writer { | ||
return errorWriter | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.