22202: Change delete process to navigate to parent project on success when viewing...
[arvados.git] / lib / service / log.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package service
6
7 import (
8         "bytes"
9         "io"
10 )
11
12 type LogPrefixer struct {
13         io.Writer
14         Prefix []byte
15         did    bool
16 }
17
18 func (lp *LogPrefixer) Write(p []byte) (int, error) {
19         if len(p) == 0 {
20                 return 0, nil
21         }
22         var out []byte
23         if !lp.did {
24                 out = append(out, lp.Prefix...)
25         }
26         lp.did = p[len(p)-1] != '\n'
27         out = append(out, bytes.Replace(p[:len(p)-1], []byte("\n"), append([]byte("\n"), lp.Prefix...), -1)...)
28         out = append(out, p[len(p)-1])
29         _, err := lp.Writer.Write(out)
30         if err != nil {
31                 return 0, err
32         }
33         return len(p), nil
34 }