9068: Fix inconsistent receiver names.
[arvados.git] / services / crunch-run / logging.go
index 3860484747d62841704adce50987b10ca4fcf73e..20928dbef769b0d4dd419ec0f8693541c93ba369 100644 (file)
@@ -21,7 +21,7 @@ type Timestamper func(t time.Time) string
 // ThrottledLogger.buf -> ThrottledLogger.flusher -> goWriter ->
 // ArvLogWriter.Write -> CollectionFileWriter.Write | Api.Create
 //
-// For stdout/stderr CopyReaderToLog additionally runs as a goroutine to pull
+// For stdout/stderr ReadWriteLines additionally runs as a goroutine to pull
 // data from the stdout/stderr Reader and send to the Logger.
 
 // ThrottledLogger accepts writes, prepends a timestamp to each line of the
@@ -35,6 +35,7 @@ type ThrottledLogger struct {
        stop        bool
        flusherDone chan bool
        Timestamper
+       Immediate *log.Logger
 }
 
 // RFC3339Fixed is a fixed-width version of RFC3339 with microsecond precision,
@@ -59,6 +60,9 @@ func (tl *ThrottledLogger) Write(p []byte) (n int, err error) {
        sc := bufio.NewScanner(bytes.NewBuffer(p))
        for sc.Scan() {
                _, err = fmt.Fprintf(tl.buf, "%s %s\n", now, sc.Text())
+               if tl.Immediate != nil {
+                       tl.Immediate.Printf("%s %s\n", now, sc.Text())
+               }
        }
        return len(p), err
 }
@@ -116,9 +120,9 @@ const (
        MaxLogLine = 1 << 12
 )
 
-// CopyReaderToLog reads from a Reader and prints to a Logger, with long line
-// splitting.
-func CopyReaderToLog(in io.Reader, logger *log.Logger, done chan<- bool) {
+// ReadWriteLines reads lines from a reader and writes to a Writer, with long
+// line splitting.
+func ReadWriteLines(in io.Reader, writer io.Writer, done chan<- bool) {
        reader := bufio.NewReaderSize(in, MaxLogLine)
        var prefix string
        for {
@@ -126,13 +130,19 @@ func CopyReaderToLog(in io.Reader, logger *log.Logger, done chan<- bool) {
                if err == io.EOF {
                        break
                } else if err != nil {
-                       logger.Print("error reading container log:", err)
+                       writer.Write([]byte(fmt.Sprintln("error reading container log:", err)))
                }
                var suffix string
                if isPrefix {
-                       suffix = "[...]"
+                       suffix = "[...]\n"
                }
-               logger.Print(prefix, string(line), suffix)
+
+               if prefix == "" && suffix == "" {
+                       writer.Write(line)
+               } else {
+                       writer.Write([]byte(fmt.Sprint(prefix, string(line), suffix)))
+               }
+
                // Set up prefix for following line
                if isPrefix {
                        prefix = "[...]"
@@ -174,9 +184,10 @@ func (arvlog *ArvLogWriter) Write(p []byte) (n int, err error) {
        }
 
        // write to API
-       lr := arvadosclient.Dict{"object_uuid": arvlog.UUID,
-               "event_type": arvlog.loggingStream,
-               "properties": map[string]string{"text": string(p)}}
+       lr := arvadosclient.Dict{"log": arvadosclient.Dict{
+               "object_uuid": arvlog.UUID,
+               "event_type":  arvlog.loggingStream,
+               "properties":  map[string]string{"text": string(p)}}}
        err2 := arvlog.ArvClient.Create("logs", lr, nil)
 
        if err1 != nil || err2 != nil {