cmd/stress: prefix logs with start date/time

Currently, stress logs are named "/tmp/go-stress-" plus a random
number. This makes it really annoying to tease apart failures from
different stress runs.

This CL changes the default prefix to

  "/tmp/go-stress-<ISO 8601 date/time>-<random number>"

where the date/time part is when the stress command started.  This
naming clusters logs by stress invocation, making it easy to tease
apart invocations, even if there are multiple stresses running
concurrently or they are run back-to-back.

This also provides a flag to override this prefix.

Change-Id: I043e5ee7168ba6db4e2355e39b147071edbc6864
Reviewed-on: https://go-review.googlesource.com/c/150047
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
diff --git a/cmd/stress/stress.go b/cmd/stress/stress.go
index ee68917..b1dda15 100644
--- a/cmd/stress/stress.go
+++ b/cmd/stress/stress.go
@@ -20,6 +20,7 @@
 	"io/ioutil"
 	"os"
 	"os/exec"
+	"path/filepath"
 	"regexp"
 	"runtime"
 	"syscall"
@@ -32,6 +33,7 @@
 	flagKill    = flag.Bool("kill", true, "kill timed out processes if true, otherwise just print pid (to attach with gdb)")
 	flagFailure = flag.String("failure", "", "fail only if output matches `regexp`")
 	flagIgnore  = flag.String("ignore", "", "ignore failure if output matches `regexp`")
+	flagOutput  = flag.String("o", defaultPrefix(), "output failure logs to `path` plus a unique suffix")
 )
 
 func init() {
@@ -47,6 +49,11 @@
 	}
 }
 
+func defaultPrefix() string {
+	date := time.Now().Format("go-stress-20060102T150405-")
+	return filepath.Join(os.TempDir(), date)
+}
+
 func main() {
 	flag.Parse()
 	if *flagP <= 0 || *flagTimeout <= 0 || len(flag.Args()) == 0 {
@@ -115,7 +122,8 @@
 				continue
 			}
 			fails++
-			f, err := ioutil.TempFile("", "go-stress")
+			dir, path := filepath.Split(*flagOutput)
+			f, err := ioutil.TempFile(dir, path)
 			if err != nil {
 				fmt.Printf("failed to create temp file: %v\n", err)
 				os.Exit(1)