Log status messages to file if requested.
Prior to this change, only log messages were written to the file.
Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/commit/657acd9b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/tree/657acd9b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/diff/657acd9b
Branch: refs/heads/develop
Commit: 657acd9b1cdb3e1a8863650e38678bf20c4ef018
Parents: 668b7f2
Author: Christopher Collins <ccollins476ad@gmail.com>
Authored: Thu Mar 10 15:29:15 2016 -0800
Committer: Christopher Collins <ccollins476ad@gmail.com>
Committed: Thu Mar 10 16:54:35 2016 -0800
----------------------------------------------------------------------
util/util.go | 19 +++++++++++++------
1 file changed, 13 insertions(+), 6 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/657acd9b/util/util.go
----------------------------------------------------------------------
diff --git a/util/util.go b/util/util.go
index 582a70f..d3a01c6 100644
--- a/util/util.go
+++ b/util/util.go
@@ -41,6 +41,7 @@ import (
)
var Verbosity int
+var logFile *os.File
func ParseEqualsPair(v string) (string, string, error) {
s := strings.Split(v, "=")
@@ -81,9 +82,14 @@ func FmtNewtError(format string, args ...interface{}) *NewtError {
// Print Silent, Quiet and Verbose aware status messages to stdout.
func StatusMessage(level int, message string, args ...interface{}) {
if Verbosity >= level {
- fmt.Printf(message, args...)
+ str := fmt.Sprintf(message, args...)
+ os.Stdout.WriteString(str)
+ os.Stdout.Sync()
+
+ if logFile != nil {
+ logFile.WriteString(str)
+ }
}
- os.Stdout.Sync()
}
// Print Silent, Quiet and Verbose aware status messages to stderr.
@@ -158,17 +164,18 @@ func Max(x, y int) int {
return y
}
-func initLog(level string, logFile string) error {
+func initLog(level string, logFilename string) error {
var writer io.Writer
- if logFile == "" {
+ if logFilename == "" {
writer = os.Stderr
} else {
- f, err := os.Create(logFile)
+ var err error
+ logFile, err = os.Create(logFilename)
if err != nil {
return NewNewtError(err.Error())
}
- writer = io.MultiWriter(os.Stderr, f)
+ writer = io.MultiWriter(os.Stderr, logFile)
}
filter := &logutils.LevelFilter{
|