code.vegaprotocol.io/vega@v0.79.0/core/integration/steps/debug_all_events.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package steps
    17  
    18  import (
    19  	"os"
    20  
    21  	"code.vegaprotocol.io/vega/core/integration/stubs"
    22  	"code.vegaprotocol.io/vega/logging"
    23  
    24  	"github.com/golang/protobuf/jsonpb"
    25  )
    26  
    27  func DebugAllEvents(broker *stubs.BrokerStub, log *logging.Logger) {
    28  	log.Info("DUMPING EVENTS")
    29  	data := broker.GetAllEventsSinceCleared()
    30  	for _, a := range data {
    31  		log.Info(a.Type().String())
    32  	}
    33  }
    34  
    35  func DebugLastNEvents(n int, broker *stubs.BrokerStub, log *logging.Logger) {
    36  	log.Infof("DUMPING LAST %d EVENTS", n)
    37  	data := broker.GetAllEvents()
    38  	for i := len(data) - n; i < len(data); i++ {
    39  		log.Info(data[i].Type().String())
    40  	}
    41  }
    42  
    43  func DebugAllEventsJSONFile(broker *stubs.BrokerStub, log *logging.Logger, fname string) error {
    44  	of, err := os.Create(fname)
    45  	if err != nil {
    46  		log.Error("Failed to create file", logging.Error(err))
    47  		return err
    48  	}
    49  
    50  	defer func() { _ = of.Close() }()
    51  	marshaler := jsonpb.Marshaler{
    52  		EnumsAsInts: false,
    53  	}
    54  	data := broker.GetAllEventsSinceCleared()
    55  	for _, a := range data {
    56  		evt := a.StreamMessage()
    57  		s, err := marshaler.MarshalToString(evt)
    58  		if err != nil {
    59  			log.Errorf("FAILED TO MARSHAL %#v", evt)
    60  			continue
    61  		}
    62  		if _, err := of.WriteString(s + "\n"); err != nil {
    63  			log.Errorf("Failed to write '%s': %v", s, err)
    64  		}
    65  	}
    66  
    67  	log.Infof("EVENTS WRITTEN TO FILE %s", fname)
    68  	return nil
    69  }