1
0
Fork 0
mirror of https://github.com/restic/restic.git synced 2024-12-28 02:38:33 +00:00

Install log buffer for global logs

This hides logged message from the net/http library and only shows them
in case an error occurs.
This commit is contained in:
Alexander Neumann 2017-05-01 10:09:24 +02:00
parent c5ae5524ff
commit 0096eca7fe

View file

@ -1,7 +1,10 @@
package main
import (
"bufio"
"bytes"
"fmt"
"log"
"os"
"restic"
"restic/debug"
@ -44,6 +47,14 @@ directories in an encrypted repository stored on different backends.
},
}
var logBuffer = bytes.NewBuffer(nil)
func init() {
// install custom global logger into a buffer, if an error occurs
// we can show the logs
log.SetOutput(logBuffer)
}
func main() {
debug.Log("main %#v", os.Args)
err := cmdRoot.Execute()
@ -55,6 +66,14 @@ func main() {
fmt.Fprintf(os.Stderr, "%v\n", err)
case err != nil:
fmt.Fprintf(os.Stderr, "%+v\n", err)
if logBuffer.Len() > 0 {
fmt.Fprintf(os.Stderr, "also, the following messages were logged by a library:\n")
sc := bufio.NewScanner(logBuffer)
for sc.Scan() {
fmt.Fprintln(os.Stderr, sc.Text())
}
}
}
var exitCode int