@@ -40,7 +40,7 @@ type grepOpt struct {
4040}
4141
4242func (o grepOpt ) isEmpty () bool {
43- return o .regexp == ""
43+ return strings . TrimSpace ( o .regexp ) == ""
4444}
4545
4646func init () {
@@ -227,6 +227,9 @@ func streamlogs(ctx context.Context, wg *sync.WaitGroup, input streamlogsInput)
227227 }
228228
229229 scanner := bufio .NewScanner (stdout )
230+ // to prevent 'token too long' error
231+ buf := make ([]byte , 0 , 64 * 1024 )
232+ scanner .Buffer (buf , 1024 * 1024 )
230233
231234 err = cmd .Start ()
232235 if err != nil {
@@ -235,26 +238,42 @@ func streamlogs(ctx context.Context, wg *sync.WaitGroup, input streamlogsInput)
235238
236239 grepOpt := input .grepOpt
237240 re := regexp .MustCompile (grepOpt .regexp )
241+ cTarget := input .colorf (target )
238242 remain := 0
239243 for scanner .Scan () {
240244 line := scanner .Text ()
241- match := re .FindAllString (line , - 1 )
242- matched := len (match ) > 0
243245
244- if matched {
245- remain = grepOpt .aCount
246+ if grepOpt .isEmpty () {
247+ printTargetLog (cTarget , line )
248+ continue
246249 }
247250
248- if ! grepOpt .isEmpty () && ! matched && remain < 0 {
251+ match := re .FindAllString (line , - 1 )
252+ matched := len (match ) > 0
253+ loggable := matched || remain > 0
254+
255+ if ! loggable {
249256 continue
250257 }
251258
252259 for _ , m := range match {
253260 line = strings .Replace (line , m , input .colorf (m ), 1 )
254261 }
255262
256- fmt .Printf ("[%s] %s\r \n " , input .colorf (target ), line )
257- remain --
263+ printTargetLog (cTarget , line )
264+
265+ if grepOpt .aCount == 0 {
266+ continue
267+ }
268+
269+ if matched {
270+ remain = grepOpt .aCount
271+ continue
272+ }
273+
274+ if remain -- ; remain == 0 {
275+ fmt .Printf (input .colorf ("--" ) + "\r \n " )
276+ }
258277 }
259278
260279 err = scanner .Err ()
@@ -285,6 +304,10 @@ func streamlogs(ctx context.Context, wg *sync.WaitGroup, input streamlogsInput)
285304 return
286305}
287306
307+ func printTargetLog (target string , log string ) {
308+ fmt .Printf ("[%s] %s\r \n " , target , log )
309+ }
310+
288311func terminateSession (svc gossm.SSMservice , sid string ) error {
289312 ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
290313 defer cancel ()
0 commit comments