-
Notifications
You must be signed in to change notification settings - Fork 8
/
writer.go
170 lines (148 loc) · 3.67 KB
/
writer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package logs
import (
"fmt"
"io"
"sync"
"time"
"github.com/rs/zerolog/diode"
"github.com/ARM-software/golang-utils/utils/commonerrors"
)
type MultipleWritersWithSource struct {
mu sync.RWMutex
writers []WriterWithSource
}
func (w *MultipleWritersWithSource) GetWriters() ([]WriterWithSource, error) {
w.mu.RLock()
defer w.mu.RUnlock()
return w.writers, nil
}
func (w *MultipleWritersWithSource) AddWriters(writers ...WriterWithSource) error {
w.mu.Lock()
defer w.mu.Unlock()
w.writers = append(w.writers, writers...)
return nil
}
func (w *MultipleWritersWithSource) Write(p []byte) (n int, err error) {
writers, err := w.GetWriters()
if err != nil {
return
}
for _, writer := range writers {
n, _ = writer.Write(p)
}
return
}
func (w *MultipleWritersWithSource) SetSource(source string) (err error) {
writers, err := w.GetWriters()
if err != nil {
return
}
for _, writer := range writers {
err = writer.SetSource(source)
}
return
}
func (w *MultipleWritersWithSource) Close() (err error) {
writers, err := w.GetWriters()
if err != nil {
return
}
for _, writer := range writers {
err1 := writer.Close()
if err1 != nil {
err = err1
}
}
return
}
func NewMultipleWritersWithSource(writers ...WriterWithSource) (writer *MultipleWritersWithSource, err error) {
writer = &MultipleWritersWithSource{}
err = writer.AddWriters(writers...)
return
}
// CreateMultipleWritersWithSource creates a compound writer with source.
//
// Deprecated: Use NewMultipleWritersWithSource instead
func CreateMultipleWritersWithSource(writers ...WriterWithSource) (writer *MultipleWritersWithSource, err error) {
return NewMultipleWritersWithSource(writers...)
}
type DiodeWriter struct {
WriterWithSource
diodeWriter io.Writer
slowWriter WriterWithSource
}
func (w *DiodeWriter) Write(p []byte) (n int, err error) {
return w.diodeWriter.Write(p)
}
func (w *DiodeWriter) Close() error {
err := w.slowWriter.Close()
if err != nil {
return err
}
if diodeCloser, ok := w.diodeWriter.(io.Closer); ok {
return diodeCloser.Close()
}
return err
}
func (w *DiodeWriter) SetSource(source string) error {
return w.slowWriter.SetSource(source)
}
func NewDiodeWriterForSlowWriter(slowWriter WriterWithSource, ringBufferSize int, pollInterval time.Duration, droppedMessagesLogger Loggers) WriterWithSource {
return &DiodeWriter{diodeWriter: diode.NewWriter(slowWriter, ringBufferSize, pollInterval, func(missed int) {
if droppedMessagesLogger != nil {
droppedMessagesLogger.LogError(fmt.Sprintf("Logger dropped %d messages", missed))
}
}),
slowWriter: slowWriter,
}
}
type infoWriter struct {
loggers Loggers
}
func (w *infoWriter) Write(p []byte) (n int, err error) {
if w.loggers == nil {
err = commonerrors.ErrNoLogger
return
}
n = len(p)
w.loggers.Log(string(p))
return
}
// NewInfoWriterFromLoggers returns a io.Writer from a Loggers by only returning INFO messages
func NewInfoWriterFromLoggers(l Loggers) (w io.Writer, err error) {
if l == nil {
err = commonerrors.ErrNoLogger
return
}
w = &infoWriter{
loggers: l,
}
return
}
type errWriter struct {
loggers Loggers
}
func (w *errWriter) Write(p []byte) (n int, err error) {
if w.loggers == nil {
err = commonerrors.ErrNoLogger
return
}
n = len(p)
w.loggers.LogError(string(p))
return
}
// NewErrorWriterFromLoggers returns a io.Writer from a Loggers by only returning ERROR messages
func NewErrorWriterFromLoggers(l Loggers) (w io.Writer, err error) {
if l == nil {
err = commonerrors.ErrNoLogger
return
}
w = &errWriter{
loggers: l,
}
return
}