lntest: add AddToLog method for node
This commit adds a new method, AddToLog, that can be used to add strings to a test node's log file.
This commit is contained in:
parent
9a76b3ee58
commit
87dee4b15a
@ -163,6 +163,7 @@ type HarnessNode struct {
|
|||||||
|
|
||||||
cmd *exec.Cmd
|
cmd *exec.Cmd
|
||||||
pidFile string
|
pidFile string
|
||||||
|
logFile *os.File
|
||||||
|
|
||||||
// processExit is a channel that's closed once it's detected that the
|
// processExit is a channel that's closed once it's detected that the
|
||||||
// process this instance of HarnessNode is bound to has exited.
|
// process this instance of HarnessNode is bound to has exited.
|
||||||
@ -231,10 +232,10 @@ func (hn *HarnessNode) start(lndError chan<- error) error {
|
|||||||
// If the logoutput flag is passed, redirect output from the nodes to
|
// If the logoutput flag is passed, redirect output from the nodes to
|
||||||
// log files.
|
// log files.
|
||||||
if *logOutput {
|
if *logOutput {
|
||||||
logFile := fmt.Sprintf("output%d.log", hn.NodeID)
|
fileName := fmt.Sprintf("output%d.log", hn.NodeID)
|
||||||
|
|
||||||
// Create file if not exists, otherwise append.
|
// Create file if not exists, otherwise append.
|
||||||
file, err := os.OpenFile(logFile,
|
file, err := os.OpenFile(fileName,
|
||||||
os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
|
os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -246,6 +247,10 @@ func (hn *HarnessNode) start(lndError chan<- error) error {
|
|||||||
|
|
||||||
// Pass the node's stdout only to the file.
|
// Pass the node's stdout only to the file.
|
||||||
hn.cmd.Stdout = file
|
hn.cmd.Stdout = file
|
||||||
|
|
||||||
|
// Let the node keep a reference to this file, such
|
||||||
|
// that we can add to it if necessary.
|
||||||
|
hn.logFile = file
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := hn.cmd.Start(); err != nil {
|
if err := hn.cmd.Start(); err != nil {
|
||||||
@ -304,6 +309,19 @@ func (hn *HarnessNode) start(lndError chan<- error) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddToLog adds a line of choice to the node's logfile. This is useful
|
||||||
|
// to interleave test output with output from the node.
|
||||||
|
func (hn *HarnessNode) AddToLog(line string) error {
|
||||||
|
// If this node was not set up with a log file, just return early.
|
||||||
|
if hn.logFile == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if _, err := hn.logFile.WriteString(line); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// writePidFile writes the process ID of the running lnd process to a .pid file.
|
// writePidFile writes the process ID of the running lnd process to a .pid file.
|
||||||
func (hn *HarnessNode) writePidFile() error {
|
func (hn *HarnessNode) writePidFile() error {
|
||||||
filePath := filepath.Join(hn.cfg.BaseDir, fmt.Sprintf("%v.pid", hn.NodeID))
|
filePath := filepath.Join(hn.cfg.BaseDir, fmt.Sprintf("%v.pid", hn.NodeID))
|
||||||
|
Loading…
Reference in New Issue
Block a user