MCPcopy Create free account
hub / github.com/cloudnative-pg/cloudnative-pg / outputWriter

Function outputWriter

tests/utils/sternmultitailer/multitailer.go:129–171  ·  view source on GitHub ↗
(baseDir string, logReader io.Reader)

Source from the content-addressed store, hash-verified

127}
128
129func outputWriter(baseDir string, logReader io.Reader) {
130 r := bufio.NewReader(logReader)
131 openFilesMap := make(map[string]*os.File)
132 defer func() {
133 for k, file := range openFilesMap {
134 _ = file.Close()
135 delete(openFilesMap, k)
136 }
137 }()
138 for {
139 lineBytes, readErr := r.ReadBytes('\n')
140 // If we have a read error, skip the line
141 if readErr != nil && readErr != io.EOF {
142 fmt.Printf("could not read log line from pipe: %v\n", readErr)
143 continue
144 }
145
146 // If we have an EOF and the line is empty, I'm done
147 if readErr == io.EOF && len(lineBytes) == 0 {
148 break
149 }
150
151 // Otherwise, we have a line to process
152 var logLine stern.Log
153 err := json.Unmarshal(lineBytes, &logLine)
154 if err != nil {
155 fmt.Printf("could not unmarshal log line %v: %v\n", logLine, err)
156 continue
157 }
158
159 file, err := getLogFile(baseDir, logLine, openFilesMap)
160 if err != nil {
161 fmt.Printf("no file to write log line %v: %v\n", logLine, err)
162 continue
163 }
164
165 _, err = fmt.Fprintf(file, "%v\n", logLine.Message)
166 if err != nil {
167 fmt.Printf("could not write message to file %v: %v\n", file.Name(), err)
168 continue
169 }
170 }
171}
172
173// Get an open file for the log, or open a new one
174func getLogFile(baseDir string, log stern.Log, openFilesMap map[string]*os.File) (*os.File,

Callers 1

StreamLogsFunction · 0.85

Calls 3

getLogFileFunction · 0.85
CloseMethod · 0.65
NameMethod · 0.65

Tested by

no test coverage detected