Universally redirect stdlog messages to glog
parent
d94d94718d
commit
abff28db7e
|
@ -424,7 +424,7 @@ func generateConfig(cmd *cobra.Command, k8sVersion string) (cfg.Config, error) {
|
||||||
repository := viper.GetString(imageRepository)
|
repository := viper.GetString(imageRepository)
|
||||||
mirrorCountry := strings.ToLower(viper.GetString(imageMirrorCountry))
|
mirrorCountry := strings.ToLower(viper.GetString(imageMirrorCountry))
|
||||||
if strings.ToLower(repository) == "auto" || mirrorCountry != "" {
|
if strings.ToLower(repository) == "auto" || mirrorCountry != "" {
|
||||||
console.OutStyle(console.Connectivity, "checking main repository and mirrors for images")
|
// console.OutStyle(console.Connectivity, "checking main repository and mirrors for images")
|
||||||
found, autoSelectedRepository, err := selectImageRepository(mirrorCountry, k8sVersion)
|
found, autoSelectedRepository, err := selectImageRepository(mirrorCountry, k8sVersion)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
exit.WithError("Failed to check main repository and mirrors for images for images", err)
|
exit.WithError("Failed to check main repository and mirrors for images for images", err)
|
||||||
|
|
|
@ -17,7 +17,11 @@ limitations under the License.
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
"github.com/pkg/profile"
|
"github.com/pkg/profile"
|
||||||
|
@ -32,7 +36,9 @@ import (
|
||||||
const minikubeEnableProfile = "MINIKUBE_ENABLE_PROFILING"
|
const minikubeEnableProfile = "MINIKUBE_ENABLE_PROFILING"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
captureStdLogMessages()
|
||||||
defer glog.Flush()
|
defer glog.Flush()
|
||||||
|
|
||||||
if os.Getenv(minikubeEnableProfile) == "1" {
|
if os.Getenv(minikubeEnableProfile) == "1" {
|
||||||
defer profile.Start(profile.TraceProfile).Stop()
|
defer profile.Start(profile.TraceProfile).Stop()
|
||||||
}
|
}
|
||||||
|
@ -44,3 +50,31 @@ func main() {
|
||||||
translate.DetermineLocale()
|
translate.DetermineLocale()
|
||||||
cmd.Execute()
|
cmd.Execute()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// captureStdLogMessages arranges for messages written to the Go "log" package's to appear in glog
|
||||||
|
func captureStdLogMessages() {
|
||||||
|
log.SetFlags(log.Lshortfile)
|
||||||
|
log.SetOutput(logBridge{})
|
||||||
|
}
|
||||||
|
|
||||||
|
type logBridge struct{}
|
||||||
|
|
||||||
|
// Write parses the standard logging line and passes its components to glog
|
||||||
|
func (lb logBridge) Write(b []byte) (n int, err error) {
|
||||||
|
// Split "d.go:23: message" into "d.go", "23", and "message".
|
||||||
|
parts := bytes.SplitN(b, []byte{':'}, 3)
|
||||||
|
if len(parts) != 3 || len(parts[0]) < 1 || len(parts[2]) < 1 {
|
||||||
|
glog.Errorf("bad log format: %s", b)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
file := string(parts[0])
|
||||||
|
text := string(parts[2][1:]) // skip leading space
|
||||||
|
line, err := strconv.Atoi(string(parts[1]))
|
||||||
|
if err != nil {
|
||||||
|
text = fmt.Sprintf("bad line number: %s", b)
|
||||||
|
line = 0
|
||||||
|
}
|
||||||
|
glog.Infof("stdlog: %s:%d %s", file, line, text)
|
||||||
|
return len(b), nil
|
||||||
|
}
|
||||||
|
|
|
@ -102,3 +102,4 @@ func GetKubeConfigPath() string {
|
||||||
}
|
}
|
||||||
return filepath.SplitList(kubeConfigEnv)[0]
|
return filepath.SplitList(kubeConfigEnv)[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,10 +17,7 @@ limitations under the License.
|
||||||
package machine
|
package machine
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
|
@ -285,23 +282,6 @@ func getDstPath(dst string) (string, error) {
|
||||||
|
|
||||||
// CacheImage caches an image
|
// CacheImage caches an image
|
||||||
func CacheImage(image, dst string) error {
|
func CacheImage(image, dst string) error {
|
||||||
// There are go-containerregistry calls here that result in
|
|
||||||
// ugly log messages getting printed to stdout. Capture
|
|
||||||
// stdout instead and writing it to info.
|
|
||||||
r, w, err := os.Pipe()
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrap(err, "opening writing buffer")
|
|
||||||
}
|
|
||||||
log.SetOutput(w)
|
|
||||||
defer func() {
|
|
||||||
log.SetOutput(os.Stdout)
|
|
||||||
var buf bytes.Buffer
|
|
||||||
if _, err := io.Copy(&buf, r); err != nil {
|
|
||||||
glog.Errorf("output copy failed: %v", err)
|
|
||||||
}
|
|
||||||
glog.Infof(buf.String())
|
|
||||||
}()
|
|
||||||
|
|
||||||
glog.Infof("Attempting to cache image: %s at %s\n", image, dst)
|
glog.Infof("Attempting to cache image: %s at %s\n", image, dst)
|
||||||
if _, err := os.Stat(dst); err == nil {
|
if _, err := os.Stat(dst); err == nil {
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Reference in New Issue