vitess.io/vitess@v0.16.2/go/vt/wrangler/log_recorder.go (about)

     1  /*
     2  Copyright 2020 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package wrangler
    18  
    19  import (
    20  	"sort"
    21  )
    22  
    23  // LogRecorder is used to collect logs for a specific purpose.
    24  // Not thread-safe since it is expected to be generated in repeatable sequence
    25  type LogRecorder struct {
    26  	logs []string
    27  }
    28  
    29  // NewLogRecorder creates a new instance of LogRecorder
    30  func NewLogRecorder() *LogRecorder {
    31  	lr := LogRecorder{}
    32  	return &lr
    33  }
    34  
    35  // Log records a new log message
    36  func (lr *LogRecorder) Log(log string) {
    37  	lr.logs = append(lr.logs, log)
    38  	//fmt.Printf("DR: %s\n", log)
    39  }
    40  
    41  // LogSlice sorts a given slice using natural sort, so that the result is predictable.
    42  // Useful when logging arrays or maps where order of objects can vary
    43  func (lr *LogRecorder) LogSlice(logs []string) {
    44  	sort.Strings(logs)
    45  	for _, log := range logs {
    46  		lr.Log(log)
    47  	}
    48  }
    49  
    50  // GetLogs returns all recorded logs in sequence
    51  func (lr *LogRecorder) GetLogs() []string {
    52  	return lr.logs
    53  }