github.com/getgauge/gauge@v1.6.9/logger/customWriter.go (about) 1 /*---------------------------------------------------------------- 2 * Copyright (c) ThoughtWorks, Inc. 3 * Licensed under the Apache License, Version 2.0 4 * See LICENSE in the project root for license information. 5 *----------------------------------------------------------------*/ 6 7 package logger 8 9 import ( 10 "io" 11 "regexp" 12 "strings" 13 ) 14 15 const portPrefix = "Listening on port:" 16 17 type CustomWriter struct { 18 file io.Writer 19 port chan string 20 } 21 22 func (w CustomWriter) Write(p []byte) (n int, err error) { 23 line := string(p) 24 if strings.Contains(line, portPrefix) { 25 text := strings.ReplaceAll(line, "\r\n", "\n") 26 re := regexp.MustCompile(portPrefix + "([0-9]+)") 27 f := re.FindStringSubmatch(text) 28 if len(f) > 0 { 29 w.port <- f[1] 30 return len(p), nil 31 } 32 } 33 return w.file.Write(p) 34 } 35 36 func NewCustomWriter(portChan chan string, outFile io.Writer, id string, isErrorStream bool) CustomWriter { 37 return CustomWriter{ 38 port: portChan, 39 file: Writer{ 40 File: outFile, 41 LoggerID: id, 42 ShouldWriteToStdout: true, 43 isErrorStream: isErrorStream, 44 }, 45 } 46 }